1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-13 07:41:39 +03:00

refactor: Move some code that updates pg_constraint to a separate function

This extracts common/duplicate code for different ALTER CONSTRAINT
variants into a common function.  We plan to add more variants that
would use the same code.

Author: Amul Sul <amul.sul@enterprisedb.com>
Discussion: https://www.postgresql.org/message-id/flat/CAAJ_b962c5AcYW9KUt_R_ER5qs3fUGbe4az-SP-vuwPS-w-AGA@mail.gmail.com
This commit is contained in:
Peter Eisentraut
2025-03-25 14:37:22 +01:00
parent f4b2a62ae3
commit a3280e2a49

View File

@ -402,6 +402,8 @@ static void ATExecAlterChildConstr(List **wqueue, ATAlterConstraint *cmdcon,
Relation conrel, Relation tgrel, Relation rel,
HeapTuple contuple, bool recurse, List **otherrelids,
LOCKMODE lockmode);
static void AlterConstrUpdateConstraintEntry(ATAlterConstraint *cmdcon, Relation conrel,
HeapTuple contuple);
static ObjectAddress ATExecValidateConstraint(List **wqueue,
Relation rel, char *constrName,
bool recurse, bool recursing, LOCKMODE lockmode);
@ -12093,23 +12095,9 @@ ATExecAlterConstraintInternal(List **wqueue, ATAlterConstraint *cmdcon,
(currcon->condeferrable != cmdcon->deferrable ||
currcon->condeferred != cmdcon->initdeferred))
{
HeapTuple copyTuple;
Form_pg_constraint copy_con;
copyTuple = heap_copytuple(contuple);
copy_con = (Form_pg_constraint) GETSTRUCT(copyTuple);
copy_con->condeferrable = cmdcon->deferrable;
copy_con->condeferred = cmdcon->initdeferred;
CatalogTupleUpdate(conrel, &copyTuple->t_self, copyTuple);
InvokeObjectPostAlterHook(ConstraintRelationId, currcon->oid, 0);
heap_freetuple(copyTuple);
AlterConstrUpdateConstraintEntry(cmdcon, conrel, contuple);
changed = true;
/* Make new constraint flags visible to others */
CacheInvalidateRelcache(rel);
/*
* Now we need to update the multiple entries in pg_trigger that
* implement the constraint.
@ -12140,27 +12128,14 @@ ATExecAlterConstraintInternal(List **wqueue, ATAlterConstraint *cmdcon,
AttrNumber colNum;
char *colName;
List *children;
HeapTuple copyTuple;
Form_pg_constraint copy_con;
/* The current implementation only works for NOT NULL constraints */
Assert(currcon->contype == CONSTRAINT_NOTNULL);
copyTuple = heap_copytuple(contuple);
copy_con = (Form_pg_constraint) GETSTRUCT(copyTuple);
copy_con->connoinherit = cmdcon->noinherit;
CatalogTupleUpdate(conrel, &copyTuple->t_self, copyTuple);
InvokeObjectPostAlterHook(ConstraintRelationId, currcon->oid, 0);
AlterConstrUpdateConstraintEntry(cmdcon, conrel, contuple);
CommandCounterIncrement();
heap_freetuple(copyTuple);
changed = true;
/* Make new constraint flags visible to others */
CacheInvalidateRelcache(rel);
/* Fetch the column number and name */
colNum = extractNotNullColumn(contuple);
colName = get_attname(currcon->conrelid, colNum, false);
@ -12320,6 +12295,39 @@ ATExecAlterChildConstr(List **wqueue, ATAlterConstraint *cmdcon,
systable_endscan(pscan);
}
/*
* Update the constraint entry for the given ATAlterConstraint command, and
* invoke the appropriate hooks.
*/
static void
AlterConstrUpdateConstraintEntry(ATAlterConstraint *cmdcon, Relation conrel,
HeapTuple contuple)
{
HeapTuple copyTuple;
Form_pg_constraint copy_con;
Assert(cmdcon->alterDeferrability || cmdcon->alterInheritability);
copyTuple = heap_copytuple(contuple);
copy_con = (Form_pg_constraint) GETSTRUCT(copyTuple);
if (cmdcon->alterDeferrability)
{
copy_con->condeferrable = cmdcon->deferrable;
copy_con->condeferred = cmdcon->initdeferred;
}
if (cmdcon->alterInheritability)
copy_con->connoinherit = cmdcon->noinherit;
CatalogTupleUpdate(conrel, &copyTuple->t_self, copyTuple);
InvokeObjectPostAlterHook(ConstraintRelationId, copy_con->oid, 0);
/* Make new constraint flags visible to others */
CacheInvalidateRelcacheByRelid(copy_con->conrelid);
heap_freetuple(copyTuple);
}
/*
* ALTER TABLE VALIDATE CONSTRAINT
*