From 8eb630bfa9b8147c742a27a4974d49bacec9c5f2 Mon Sep 17 00:00:00 2001 From: Amul Sul Date: Tue, 14 Jan 2025 22:35:32 +0530 Subject: [PATCH v15 5/9] refactor: Change ATExecAlterConstrRecurse() argument. Instead of passing the Relation of the referencing relation, we will pass a relid. Opening the relation using the relid again should not cause significant issues. However, this approach makes recursion more efficient, especially in the later patch where we will be recreating the referencing and referred triggers. In addition to that passing relid of the referred relation too. ---- NOTE: This patch is not meant to be committed separately. It should be squashed with the main patch that adds ENFORCED/NOT ENFORCED. ---- --- src/backend/commands/tablecmds.c | 45 ++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 288d033434d..1e93591e3e0 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -392,14 +392,18 @@ static void AlterSeqNamespaces(Relation classRel, Relation rel, static ObjectAddress ATExecAlterConstraint(Relation rel, ATAlterConstraint *cmdcon, bool recurse, LOCKMODE lockmode); static bool ATExecAlterConstraintInternal(ATAlterConstraint *cmdcon, Relation conrel, - Relation tgrel, Relation rel, HeapTuple contuple, - bool recurse, List **otherrelids, LOCKMODE lockmode); + Relation tgrel, const Oid fkrelid, + const Oid pkrelid, HeapTuple contuple, + bool recurse, List **otherrelids, + LOCKMODE lockmode); static void AlterConstrTriggerDeferrability(Oid conoid, Relation tgrel, Relation rel, bool deferrable, bool initdeferred, List **otherrelids); static void ATExecAlterChildConstr(ATAlterConstraint *cmdcon, Relation conrel, - Relation tgrel, Relation rel, HeapTuple contuple, - bool recurse, List **otherrelids, LOCKMODE lockmode); + Relation tgrel, const Oid fkrelid, + const Oid pkrelid, HeapTuple contuple, + bool recurse, List **otherrelids, + LOCKMODE lockmode); static ObjectAddress ATExecValidateConstraint(List **wqueue, Relation rel, char *constrName, bool recurse, bool recursing, LOCKMODE lockmode); @@ -12002,8 +12006,9 @@ ATExecAlterConstraint(Relation rel, ATAlterConstraint *cmdcon, bool recurse, /* * Do the actual catalog work, and recurse if necessary. */ - if (ATExecAlterConstraintInternal(cmdcon, conrel, tgrel, rel, contuple, - recurse, &otherrelids, lockmode)) + if (ATExecAlterConstraintInternal(cmdcon, conrel, tgrel, currcon->conrelid, + currcon->confrelid, contuple, recurse, + &otherrelids, lockmode)) ObjectAddressSet(address, ConstraintRelationId, currcon->oid); /* @@ -12035,12 +12040,14 @@ ATExecAlterConstraint(Relation rel, ATAlterConstraint *cmdcon, bool recurse, */ static bool ATExecAlterConstraintInternal(ATAlterConstraint *cmdcon, Relation conrel, - Relation tgrel, Relation rel, HeapTuple contuple, + Relation tgrel, const Oid fkrelid, + const Oid pkrelid, HeapTuple contuple, bool recurse, List **otherrelids, LOCKMODE lockmode) { Form_pg_constraint currcon; Oid refrelid = InvalidOid; bool changed = false; + Relation rel; /* since this function recurses, it could be driven to stack overflow */ check_stack_depth(); @@ -12049,6 +12056,8 @@ ATExecAlterConstraintInternal(ATAlterConstraint *cmdcon, Relation conrel, if (currcon->contype == CONSTRAINT_FOREIGN) refrelid = currcon->confrelid; + rel = table_open(currcon->conrelid, lockmode); + /* * Update pg_constraint with the flags from cmdcon. * @@ -12093,8 +12102,10 @@ ATExecAlterConstraintInternal(ATAlterConstraint *cmdcon, Relation conrel, (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE || (OidIsValid(refrelid) && get_rel_relkind(refrelid) == RELKIND_PARTITIONED_TABLE))) - ATExecAlterChildConstr(cmdcon, conrel, tgrel, rel, contuple, - recurse, otherrelids, lockmode); + ATExecAlterChildConstr(cmdcon, conrel, tgrel, fkrelid, pkrelid, + contuple, recurse, otherrelids, lockmode); + + table_close(rel, NoLock); return changed; } @@ -12177,8 +12188,9 @@ AlterConstrTriggerDeferrability(Oid conoid, Relation tgrel, Relation rel, */ static void ATExecAlterChildConstr(ATAlterConstraint *cmdcon, Relation conrel, - Relation tgrel, Relation rel, HeapTuple contuple, - bool recurse, List **otherrelids, LOCKMODE lockmode) + Relation tgrel, const Oid fkrelid, const Oid pkrelid, + HeapTuple contuple, bool recurse, List **otherrelids, + LOCKMODE lockmode) { Form_pg_constraint currcon; Oid conoid; @@ -12198,15 +12210,8 @@ ATExecAlterChildConstr(ATAlterConstraint *cmdcon, Relation conrel, true, NULL, 1, &pkey); while (HeapTupleIsValid(childtup = systable_getnext(pscan))) - { - Form_pg_constraint childcon = (Form_pg_constraint) GETSTRUCT(childtup); - Relation childrel; - - childrel = table_open(childcon->conrelid, lockmode); - ATExecAlterConstraintInternal(cmdcon, conrel, tgrel, childrel, childtup, - recurse, otherrelids, lockmode); - table_close(childrel, NoLock); - } + ATExecAlterConstraintInternal(cmdcon, conrel, tgrel, fkrelid, pkrelid, + childtup, recurse, otherrelids, lockmode); systable_endscan(pscan); } -- 2.43.5