Skip to content

Commit a3280e2

Browse files
committed
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
1 parent f4b2a62 commit a3280e2

File tree

1 file changed

+37
-29
lines changed

1 file changed

+37
-29
lines changed

src/backend/commands/tablecmds.c

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,8 @@ static void ATExecAlterChildConstr(List **wqueue, ATAlterConstraint *cmdcon,
402402
Relation conrel, Relation tgrel, Relation rel,
403403
HeapTuple contuple, bool recurse, List **otherrelids,
404404
LOCKMODE lockmode);
405+
static void AlterConstrUpdateConstraintEntry(ATAlterConstraint *cmdcon, Relation conrel,
406+
HeapTuple contuple);
405407
static ObjectAddress ATExecValidateConstraint(List **wqueue,
406408
Relation rel, char *constrName,
407409
bool recurse, bool recursing, LOCKMODE lockmode);
@@ -12093,23 +12095,9 @@ ATExecAlterConstraintInternal(List **wqueue, ATAlterConstraint *cmdcon,
1209312095
(currcon->condeferrable != cmdcon->deferrable ||
1209412096
currcon->condeferred != cmdcon->initdeferred))
1209512097
{
12096-
HeapTuple copyTuple;
12097-
Form_pg_constraint copy_con;
12098-
12099-
copyTuple = heap_copytuple(contuple);
12100-
copy_con = (Form_pg_constraint) GETSTRUCT(copyTuple);
12101-
copy_con->condeferrable = cmdcon->deferrable;
12102-
copy_con->condeferred = cmdcon->initdeferred;
12103-
CatalogTupleUpdate(conrel, &copyTuple->t_self, copyTuple);
12104-
12105-
InvokeObjectPostAlterHook(ConstraintRelationId, currcon->oid, 0);
12106-
12107-
heap_freetuple(copyTuple);
12098+
AlterConstrUpdateConstraintEntry(cmdcon, conrel, contuple);
1210812099
changed = true;
1210912100

12110-
/* Make new constraint flags visible to others */
12111-
CacheInvalidateRelcache(rel);
12112-
1211312101
/*
1211412102
* Now we need to update the multiple entries in pg_trigger that
1211512103
* implement the constraint.
@@ -12140,27 +12128,14 @@ ATExecAlterConstraintInternal(List **wqueue, ATAlterConstraint *cmdcon,
1214012128
AttrNumber colNum;
1214112129
char *colName;
1214212130
List *children;
12143-
HeapTuple copyTuple;
12144-
Form_pg_constraint copy_con;
1214512131

1214612132
/* The current implementation only works for NOT NULL constraints */
1214712133
Assert(currcon->contype == CONSTRAINT_NOTNULL);
1214812134

12149-
copyTuple = heap_copytuple(contuple);
12150-
copy_con = (Form_pg_constraint) GETSTRUCT(copyTuple);
12151-
copy_con->connoinherit = cmdcon->noinherit;
12152-
12153-
CatalogTupleUpdate(conrel, &copyTuple->t_self, copyTuple);
12154-
12155-
InvokeObjectPostAlterHook(ConstraintRelationId, currcon->oid, 0);
12156-
12135+
AlterConstrUpdateConstraintEntry(cmdcon, conrel, contuple);
1215712136
CommandCounterIncrement();
12158-
heap_freetuple(copyTuple);
1215912137
changed = true;
1216012138

12161-
/* Make new constraint flags visible to others */
12162-
CacheInvalidateRelcache(rel);
12163-
1216412139
/* Fetch the column number and name */
1216512140
colNum = extractNotNullColumn(contuple);
1216612141
colName = get_attname(currcon->conrelid, colNum, false);
@@ -12320,6 +12295,39 @@ ATExecAlterChildConstr(List **wqueue, ATAlterConstraint *cmdcon,
1232012295
systable_endscan(pscan);
1232112296
}
1232212297

12298+
/*
12299+
* Update the constraint entry for the given ATAlterConstraint command, and
12300+
* invoke the appropriate hooks.
12301+
*/
12302+
static void
12303+
AlterConstrUpdateConstraintEntry(ATAlterConstraint *cmdcon, Relation conrel,
12304+
HeapTuple contuple)
12305+
{
12306+
HeapTuple copyTuple;
12307+
Form_pg_constraint copy_con;
12308+
12309+
Assert(cmdcon->alterDeferrability || cmdcon->alterInheritability);
12310+
12311+
copyTuple = heap_copytuple(contuple);
12312+
copy_con = (Form_pg_constraint) GETSTRUCT(copyTuple);
12313+
12314+
if (cmdcon->alterDeferrability)
12315+
{
12316+
copy_con->condeferrable = cmdcon->deferrable;
12317+
copy_con->condeferred = cmdcon->initdeferred;
12318+
}
12319+
if (cmdcon->alterInheritability)
12320+
copy_con->connoinherit = cmdcon->noinherit;
12321+
12322+
CatalogTupleUpdate(conrel, &copyTuple->t_self, copyTuple);
12323+
InvokeObjectPostAlterHook(ConstraintRelationId, copy_con->oid, 0);
12324+
12325+
/* Make new constraint flags visible to others */
12326+
CacheInvalidateRelcacheByRelid(copy_con->conrelid);
12327+
12328+
heap_freetuple(copyTuple);
12329+
}
12330+
1232312331
/*
1232412332
* ALTER TABLE VALIDATE CONSTRAINT
1232512333
*

0 commit comments

Comments
 (0)