Skip to content

Commit 64224a8

Browse files
committed
refactor: re-add ATExecAlterChildConstr()
ATExecAlterChildConstr() was removed in commit 80d7f99, but it is needed in some subsequent patches for the NOT ENFORCED feature, to recurse over child constraints. This adds it back in slightly altered form. Author: Amul Sul <amul.sul@enterprisedb.com> Reviewed-by: Alexandra Wang <alexandra.wang.oss@gmail.com> Discussion: https://www.postgresql.org/message-id/flat/CAAJ_b962c5AcYW9KUt_R_ER5qs3fUGbe4az-SP-vuwPS-w-AGA%40mail.gmail.com
1 parent 76def4c commit 64224a8

File tree

1 file changed

+54
-30
lines changed

1 file changed

+54
-30
lines changed

src/backend/commands/tablecmds.c

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,10 @@ static bool ATExecAlterConstraintInternal(List **wqueue, ATAlterConstraint *cmdc
398398
static void AlterConstrTriggerDeferrability(Oid conoid, Relation tgrel, Relation rel,
399399
bool deferrable, bool initdeferred,
400400
List **otherrelids);
401+
static void ATExecAlterChildConstr(List **wqueue, ATAlterConstraint *cmdcon,
402+
Relation conrel, Relation tgrel, Relation rel,
403+
HeapTuple contuple, bool recurse, List **otherrelids,
404+
LOCKMODE lockmode);
401405
static ObjectAddress ATExecValidateConstraint(List **wqueue,
402406
Relation rel, char *constrName,
403407
bool recurse, bool recursing, LOCKMODE lockmode);
@@ -12031,41 +12035,13 @@ ATExecAlterConstraintInternal(List **wqueue, ATAlterConstraint *cmdcon,
1203112035
/*
1203212036
* If the table at either end of the constraint is partitioned, we need to
1203312037
* handle every constraint that is a child of this one.
12034-
*
12035-
* Note that this doesn't handle recursion the normal way, viz. by
12036-
* scanning the list of child relations and recursing; instead it uses the
12037-
* conparentid relationships. This may need to be reconsidered.
1203812038
*/
1203912039
if (recurse && changed &&
1204012040
(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE ||
1204112041
(OidIsValid(refrelid) &&
1204212042
get_rel_relkind(refrelid) == RELKIND_PARTITIONED_TABLE)))
12043-
{
12044-
ScanKeyData pkey;
12045-
SysScanDesc pscan;
12046-
HeapTuple childtup;
12047-
12048-
ScanKeyInit(&pkey,
12049-
Anum_pg_constraint_conparentid,
12050-
BTEqualStrategyNumber, F_OIDEQ,
12051-
ObjectIdGetDatum(currcon->oid));
12052-
12053-
pscan = systable_beginscan(conrel, ConstraintParentIndexId,
12054-
true, NULL, 1, &pkey);
12055-
12056-
while (HeapTupleIsValid(childtup = systable_getnext(pscan)))
12057-
{
12058-
Form_pg_constraint childcon = (Form_pg_constraint) GETSTRUCT(childtup);
12059-
Relation childrel;
12060-
12061-
childrel = table_open(childcon->conrelid, lockmode);
12062-
ATExecAlterConstraintInternal(wqueue, cmdcon, conrel, tgrel, childrel,
12063-
childtup, recurse, otherrelids, lockmode);
12064-
table_close(childrel, NoLock);
12065-
}
12066-
12067-
systable_endscan(pscan);
12068-
}
12043+
ATExecAlterChildConstr(wqueue, cmdcon, conrel, tgrel, rel, contuple,
12044+
recurse, otherrelids, lockmode);
1206912045

1207012046
/*
1207112047
* Update the catalog for inheritability. No work if the constraint is
@@ -12203,6 +12179,54 @@ AlterConstrTriggerDeferrability(Oid conoid, Relation tgrel, Relation rel,
1220312179
systable_endscan(tgscan);
1220412180
}
1220512181

12182+
/*
12183+
* Invokes ATExecAlterConstraintInternal for each constraint that is a child of
12184+
* the specified constraint.
12185+
*
12186+
* Note that this doesn't handle recursion the normal way, viz. by scanning the
12187+
* list of child relations and recursing; instead it uses the conparentid
12188+
* relationships. This may need to be reconsidered.
12189+
*
12190+
* The arguments to this function have the same meaning as the arguments to
12191+
* ATExecAlterConstraintInternal.
12192+
*/
12193+
static void
12194+
ATExecAlterChildConstr(List **wqueue, ATAlterConstraint *cmdcon,
12195+
Relation conrel, Relation tgrel, Relation rel,
12196+
HeapTuple contuple, bool recurse, List **otherrelids,
12197+
LOCKMODE lockmode)
12198+
{
12199+
Form_pg_constraint currcon;
12200+
Oid conoid;
12201+
ScanKeyData pkey;
12202+
SysScanDesc pscan;
12203+
HeapTuple childtup;
12204+
12205+
currcon = (Form_pg_constraint) GETSTRUCT(contuple);
12206+
conoid = currcon->oid;
12207+
12208+
ScanKeyInit(&pkey,
12209+
Anum_pg_constraint_conparentid,
12210+
BTEqualStrategyNumber, F_OIDEQ,
12211+
ObjectIdGetDatum(conoid));
12212+
12213+
pscan = systable_beginscan(conrel, ConstraintParentIndexId,
12214+
true, NULL, 1, &pkey);
12215+
12216+
while (HeapTupleIsValid(childtup = systable_getnext(pscan)))
12217+
{
12218+
Form_pg_constraint childcon = (Form_pg_constraint) GETSTRUCT(childtup);
12219+
Relation childrel;
12220+
12221+
childrel = table_open(childcon->conrelid, lockmode);
12222+
ATExecAlterConstraintInternal(wqueue, cmdcon, conrel, tgrel, childrel,
12223+
childtup, recurse, otherrelids, lockmode);
12224+
table_close(childrel, NoLock);
12225+
}
12226+
12227+
systable_endscan(pscan);
12228+
}
12229+
1220612230
/*
1220712231
* ALTER TABLE VALIDATE CONSTRAINT
1220812232
*

0 commit comments

Comments
 (0)