v8-0003-refactor-Change-ATExecAlterConstrRecurse-argument.patch

application/octet-stream

Filename: v8-0003-refactor-Change-ATExecAlterConstrRecurse-argument.patch
Type: application/octet-stream
Part: 2
Message: Re: NOT ENFORCED constraint feature

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: format-patch
Series: patch v8-0003
Subject: refactor: Change ATExecAlterConstrRecurse() argument.
File+
src/backend/commands/tablecmds.c 45 21
From 1ba785ac684266d7bc6c3b9969288b9dbc87d15b Mon Sep 17 00:00:00 2001
From: Amul Sul <amul.sul@enterprisedb.com>
Date: Mon, 23 Sep 2024 12:37:05 +0530
Subject: [PATCH v8 3/5] 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 and Oids of triggers.

----
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 | 66 ++++++++++++++++++++++----------
 1 file changed, 45 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 7ee90d9a4a8..441d3968f39 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -392,14 +392,22 @@ static void AlterSeqNamespaces(Relation classRel, Relation rel,
 static ObjectAddress ATExecAlterConstraint(Relation rel, AlterTableCmd *cmd,
 										   bool recurse, bool recursing, LOCKMODE lockmode);
 static bool ATExecAlterConstrRecurse(Constraint *cmdcon, Relation conrel, Relation tgrel,
-									 Relation rel, HeapTuple contuple, List **otherrelids,
-									 LOCKMODE lockmode);
+									 const Oid fkrelid, const Oid pkrelid,
+									 HeapTuple contuple, List **otherrelids,
+									 LOCKMODE lockmode, Oid ReferencedParentDelTrigger,
+									 Oid ReferencedParentUpdTrigger,
+									 Oid ReferencingParentInsTrigger,
+									 Oid ReferencingParentUpdTrigger);
 static void AlterConstrTriggerDeferrability(Oid conoid, Relation tgrel, Relation rel,
 											bool deferrable, bool initdeferred,
 											List **otherrelids);
 static void ATExecAlterChildConstr(Constraint *cmdcon, Relation conrel, Relation tgrel,
-								   Relation rel, HeapTuple contuple, List **otherrelids,
-								   LOCKMODE lockmode);
+								   const Oid fkrelid, const Oid pkrelid,
+								   HeapTuple contuple, List **otherrelids,
+								   LOCKMODE lockmode, Oid ReferencedParentDelTrigger,
+								   Oid ReferencedParentUpdTrigger,
+								   Oid ReferencingParentInsTrigger,
+								   Oid ReferencingParentUpdTrigger);
 static ObjectAddress ATExecValidateConstraint(List **wqueue,
 											  Relation rel, char *constrName,
 											  bool recurse, bool recursing, LOCKMODE lockmode);
@@ -11797,8 +11805,10 @@ ATExecAlterConstraint(Relation rel, AlterTableCmd *cmd, bool recurse,
 		currcon->condeferred != cmdcon->initdeferred ||
 		rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
 	{
-		if (ATExecAlterConstrRecurse(cmdcon, conrel, tgrel, rel, contuple,
-									 &otherrelids, lockmode))
+		if (ATExecAlterConstrRecurse(cmdcon, conrel, tgrel, currcon->conrelid,
+									 currcon->confrelid, contuple, &otherrelids,
+									 lockmode, InvalidOid, InvalidOid,
+									 InvalidOid, InvalidOid))
 			ObjectAddressSet(address, ConstraintRelationId, currcon->oid);
 	}
 
@@ -11831,13 +11841,18 @@ ATExecAlterConstraint(Relation rel, AlterTableCmd *cmd, bool recurse,
  */
 static bool
 ATExecAlterConstrRecurse(Constraint *cmdcon, Relation conrel, Relation tgrel,
-						 Relation rel, HeapTuple contuple, List **otherrelids,
-						 LOCKMODE lockmode)
+						 const Oid fkrelid, const Oid pkrelid,
+						 HeapTuple contuple, List **otherrelids,
+						 LOCKMODE lockmode, Oid ReferencedParentDelTrigger,
+						 Oid ReferencedParentUpdTrigger,
+						 Oid ReferencingParentInsTrigger,
+						 Oid ReferencingParentUpdTrigger)
 {
 	Form_pg_constraint currcon;
 	Oid			conoid;
 	Oid			refrelid;
 	bool		changed = false;
+	Relation	rel;
 
 	/* since this function recurses, it could be driven to stack overflow */
 	check_stack_depth();
@@ -11846,6 +11861,8 @@ ATExecAlterConstrRecurse(Constraint *cmdcon, Relation conrel, Relation tgrel,
 	conoid = currcon->oid;
 	refrelid = currcon->confrelid;
 
+	rel = table_open(currcon->conrelid, lockmode);
+
 	/*
 	 * Update pg_constraint with the flags from cmdcon.
 	 *
@@ -11891,8 +11908,14 @@ ATExecAlterConstrRecurse(Constraint *cmdcon, Relation conrel, Relation tgrel,
 	 */
 	if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE ||
 		get_rel_relkind(refrelid) == RELKIND_PARTITIONED_TABLE)
-		ATExecAlterChildConstr(cmdcon, conrel, tgrel, rel, contuple,
-							   otherrelids, lockmode);
+		ATExecAlterChildConstr(cmdcon, conrel, tgrel, fkrelid, pkrelid,
+							   contuple, otherrelids, lockmode,
+							   ReferencedParentDelTrigger,
+							   ReferencedParentUpdTrigger,
+							   ReferencingParentInsTrigger,
+							   ReferencingParentUpdTrigger);
+
+	table_close(rel, NoLock);
 
 	return changed;
 }
@@ -11971,8 +11994,12 @@ AlterConstrTriggerDeferrability(Oid conoid, Relation tgrel, Relation rel,
  */
 static void
 ATExecAlterChildConstr(Constraint *cmdcon, Relation conrel, Relation tgrel,
-					   Relation rel, HeapTuple contuple, List **otherrelids,
-					   LOCKMODE lockmode)
+					   const Oid fkrelid, const Oid pkrelid,
+					   HeapTuple contuple, List **otherrelids,
+					   LOCKMODE lockmode, Oid ReferencedParentDelTrigger,
+					   Oid ReferencedParentUpdTrigger,
+					   Oid ReferencingParentInsTrigger,
+					   Oid ReferencingParentUpdTrigger)
 {
 	Form_pg_constraint currcon;
 	Oid			conoid;
@@ -11992,15 +12019,12 @@ ATExecAlterChildConstr(Constraint *cmdcon, Relation conrel, Relation tgrel,
 							   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);
-		ATExecAlterConstrRecurse(cmdcon, conrel, tgrel, childrel, childtup,
-								 otherrelids, lockmode);
-		table_close(childrel, NoLock);
-	}
+		ATExecAlterConstrRecurse(cmdcon, conrel, tgrel, fkrelid, pkrelid,
+								 childtup, otherrelids, lockmode,
+								 ReferencedParentDelTrigger,
+								 ReferencedParentUpdTrigger,
+								 ReferencingParentInsTrigger,
+								 ReferencingParentUpdTrigger);
 
 	systable_endscan(pscan);
 }
-- 
2.43.5