diff options
author | Peter Eisentraut | 2025-03-25 13:37:22 +0000 |
---|---|---|
committer | Peter Eisentraut | 2025-03-25 13:37:22 +0000 |
commit | a3280e2a494fe369a041ebdefdf2afb515952c18 (patch) | |
tree | a4bcdb33f129df0805027dc1a0598fe3f712edc4 /src/backend/commands | |
parent | f4b2a62ae3147ab262eaa21f8be76e569ffd8dfa (diff) |
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 <[email protected]>
Discussion: https://www.postgresql.org/message-id/flat/CAAJ_b962c5AcYW9KUt_R_ER5qs3fUGbe4az-SP-vuwPS-w-AGA@mail.gmail.com
Diffstat (limited to 'src/backend/commands')
-rw-r--r-- | src/backend/commands/tablecmds.c | 66 |
1 files changed, 37 insertions, 29 deletions
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 4974536e084..778e956b1ff 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -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, ©Tuple->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, ©Tuple->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); @@ -12321,6 +12296,39 @@ ATExecAlterChildConstr(List **wqueue, ATAlterConstraint *cmdcon, } /* + * 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, ©Tuple->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 * * XXX The reason we handle recursion here rather than at Phase 1 is because |