git.postgresql.org Git - postgresql.git/commitdiff

git projects / postgresql.git / commitdiff
? search:
summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: f4b2a62)
refactor: Move some code that updates pg_constraint to a separate function
2025年3月25日 13:37:22 +0000 (14:37 +0100)
2025年3月25日 13:37:22 +0000 (14:37 +0100)
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


diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 4974536e084e49a3c4065b0cf00c3fa819b39a38..778e956b1ffa3730fd7ba09bf40852c2bead886c 100644 (file)
--- 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, &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
*
This is the main PostgreSQL git repository.
RSS Atom

AltStyle によって変換されたページ (->オリジナル) /