From 7859802d453d3161291dacef5468380f9d7780b0 Mon Sep 17 00:00:00 2001
From: jian he <jian.universality@gmail.com>
Date: Sat, 27 Dec 2025 13:58:11 +0800
Subject: [PATCH v3 2/3] add constrrelOid field to CreateTrigStmt

In the spirit of the change made to add the relOid field to CreateTrigStmt.

discussion: https://postgr.es/m/CACJufxHJAr2FjbeB6ghg_-N5dxX5JVnjKSLOUxOyt4TeaAWQkg@mail.gmail.com
discussion: https://postgr.es/m/CACJufxGkqYrmwMdvUOUPet0443oUTgF_dKCpw3TfJiutfuywAQ@mail.gmail.com
commitfest: https://commitfest.postgresql.org/patch/6087
commitfest: https://commitfest.postgresql.org/patch/6089
---
 src/backend/catalog/index.c      |  3 ++-
 src/backend/commands/tablecmds.c | 13 ++++++++-----
 src/backend/commands/trigger.c   | 18 +++++++-----------
 src/backend/parser/gram.y        |  2 ++
 src/backend/tcop/utility.c       |  2 +-
 src/include/commands/trigger.h   |  4 ++--
 src/include/nodes/parsenodes.h   |  1 +
 7 files changed, 23 insertions(+), 20 deletions(-)

diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c
index 8e74ff1dd33..b3b034cf3b8 100644
--- a/src/backend/catalog/index.c
+++ b/src/backend/catalog/index.c
@@ -2040,9 +2040,10 @@ index_constraint_create(Relation heapRelation,
 		trigger->deferrable = true;
 		trigger->initdeferred = initdeferred;
 		trigger->constrrel = NULL;
+		trigger->constrrelOid = InvalidOid;
 
 		(void) CreateTrigger(trigger, NULL,
-							 InvalidOid, conOid, indexRelationId, InvalidOid,
+							 conOid, indexRelationId, InvalidOid,
 							 InvalidOid, NULL, true, false);
 	}
 
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index ee400c1cd58..cae3d5218aa 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -13863,8 +13863,9 @@ CreateFKCheckTrigger(Oid myRelOid, Oid refRelOid, Constraint *fkconstraint,
 	fk_trigger->deferrable = fkconstraint->deferrable;
 	fk_trigger->initdeferred = fkconstraint->initdeferred;
 	fk_trigger->constrrel = NULL;
+	fk_trigger->constrrelOid = refRelOid;
 
-	trigAddress = CreateTrigger(fk_trigger, NULL, refRelOid,
+	trigAddress = CreateTrigger(fk_trigger, NULL,
 								constraintOid, indexOid, InvalidOid,
 								parentTrigOid, NULL, true, false);
 
@@ -13909,6 +13910,7 @@ createForeignKeyActionTriggers(Oid myRelOid, Oid refRelOid, Constraint *fkconstr
 	fk_trigger->whenClause = NULL;
 	fk_trigger->transitionRels = NIL;
 	fk_trigger->constrrel = NULL;
+	fk_trigger->constrrelOid = myRelOid;
 
 	switch (fkconstraint->fk_del_action)
 	{
@@ -13943,7 +13945,7 @@ createForeignKeyActionTriggers(Oid myRelOid, Oid refRelOid, Constraint *fkconstr
 			break;
 	}
 
-	trigAddress = CreateTrigger(fk_trigger, NULL, myRelOid,
+	trigAddress = CreateTrigger(fk_trigger, NULL,
 								constraintOid, indexOid, InvalidOid,
 								parentDelTrigger, NULL, true, false);
 	if (deleteTrigOid)
@@ -13970,6 +13972,7 @@ createForeignKeyActionTriggers(Oid myRelOid, Oid refRelOid, Constraint *fkconstr
 	fk_trigger->whenClause = NULL;
 	fk_trigger->transitionRels = NIL;
 	fk_trigger->constrrel = NULL;
+	fk_trigger->constrrelOid = myRelOid;
 
 	switch (fkconstraint->fk_upd_action)
 	{
@@ -14004,7 +14007,7 @@ createForeignKeyActionTriggers(Oid myRelOid, Oid refRelOid, Constraint *fkconstr
 			break;
 	}
 
-	trigAddress = CreateTrigger(fk_trigger, NULL, myRelOid,
+	trigAddress = CreateTrigger(fk_trigger, NULL,
 								constraintOid, indexOid, InvalidOid,
 								parentUpdTrigger, NULL, true, false);
 	if (updateTrigOid)
@@ -20920,9 +20923,9 @@ CloneRowTriggersToPartition(Relation parent, Relation partition)
 		trigStmt->deferrable = trigForm->tgdeferrable;
 		trigStmt->initdeferred = trigForm->tginitdeferred;
 		trigStmt->constrrel = NULL; /* passed separately */
+		trigStmt->constrrelOid = trigForm->tgconstrrelid;
 
-		CreateTriggerFiringOn(trigStmt, NULL,
-							  trigForm->tgconstrrelid, InvalidOid, InvalidOid,
+		CreateTriggerFiringOn(trigStmt, NULL, InvalidOid, InvalidOid,
 							  trigForm->tgfoid, trigForm->oid, qual,
 							  false, true, trigForm->tgenabled);
 
diff --git a/src/backend/commands/trigger.c b/src/backend/commands/trigger.c
index 6e079213863..18cd3cc41b3 100644
--- a/src/backend/commands/trigger.c
+++ b/src/backend/commands/trigger.c
@@ -111,10 +111,6 @@ static HeapTuple check_modified_virtual_generated(TupleDesc tupdesc, HeapTuple t
  * queryString is the source text of the CREATE TRIGGER command.
  * This must be supplied if a whenClause is specified, else it can be NULL.
  *
- * refRelOid, if nonzero, is the relation to which the constraint trigger
- * refers.  If zero, the constraint relation name provided in the statement
- * will be looked up as needed.
- *
  * constraintOid, if nonzero, says that this trigger is being created
  * internally to implement that constraint.  A suitable pg_depend entry will
  * be made to link the trigger to that constraint.  constraintOid is zero when
@@ -155,12 +151,12 @@ static HeapTuple check_modified_virtual_generated(TupleDesc tupdesc, HeapTuple t
  */
 ObjectAddress
 CreateTrigger(CreateTrigStmt *stmt, const char *queryString,
-			  Oid refRelOid, Oid constraintOid, Oid indexOid,
+			  Oid constraintOid, Oid indexOid,
 			  Oid funcoid, Oid parentTriggerOid, Node *whenClause,
 			  bool isInternal, bool in_partition)
 {
 	return
-		CreateTriggerFiringOn(stmt, queryString, refRelOid,
+		CreateTriggerFiringOn(stmt, queryString,
 							  constraintOid, indexOid, funcoid,
 							  parentTriggerOid, whenClause, isInternal,
 							  in_partition, TRIGGER_FIRES_ON_ORIGIN);
@@ -172,7 +168,7 @@ CreateTrigger(CreateTrigStmt *stmt, const char *queryString,
  */
 ObjectAddress
 CreateTriggerFiringOn(CreateTrigStmt *stmt, const char *queryString,
-					  Oid refRelOid, Oid constraintOid,
+					  Oid constraintOid,
 					  Oid indexOid, Oid funcoid, Oid parentTriggerOid,
 					  Node *whenClause, bool isInternal, bool in_partition,
 					  char trigger_fires_when)
@@ -324,10 +320,10 @@ CreateTriggerFiringOn(CreateTrigStmt *stmt, const char *queryString,
 		 * might end up creating a pg_constraint entry referencing a
 		 * nonexistent table.
 		 */
-		if (OidIsValid(refRelOid))
+		if (OidIsValid(stmt->constrrelOid))
 		{
-			LockRelationOid(refRelOid, AccessShareLock);
-			constrrelid = refRelOid;
+			LockRelationOid(stmt->constrrelOid, AccessShareLock);
+			constrrelid = stmt->constrrelOid;
 		}
 		else if (stmt->constrrel != NULL)
 			constrrelid = RangeVarGetRelid(stmt->constrrel, AccessShareLock,
@@ -1184,8 +1180,8 @@ CreateTriggerFiringOn(CreateTrigStmt *stmt, const char *queryString,
 										childTbl, rel);
 
 			childStmt->relOid = partdesc->oids[i];
+			childStmt->constrrelOid = stmt->constrrelOid;
 			CreateTriggerFiringOn(childStmt, queryString,
-								  refRelOid,
 								  InvalidOid, InvalidOid,
 								  funcoid, trigoid, qual,
 								  isInternal, true, trigger_fires_when);
diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y
index 01e66ba615e..4a0bab33e70 100644
--- a/src/backend/parser/gram.y
+++ b/src/backend/parser/gram.y
@@ -6151,6 +6151,7 @@ CreateTrigStmt:
 					n->deferrable = false;
 					n->initdeferred = false;
 					n->constrrel = NULL;
+					n->constrrelOid = InvalidOid;
 					$$ = (Node *) n;
 				}
 		  | CREATE opt_or_replace CONSTRAINT TRIGGER name AFTER TriggerEvents ON
@@ -6202,6 +6203,7 @@ CreateTrigStmt:
 								   &n->deferrable, &n->initdeferred, &dummy,
 								   NULL, NULL, yyscanner);
 					n->constrrel = $10;
+					n->constrrelOid = InvalidOid;
 					$$ = (Node *) n;
 				}
 		;
diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c
index 7d6387ad9b5..6c3642e00bc 100644
--- a/src/backend/tcop/utility.c
+++ b/src/backend/tcop/utility.c
@@ -1701,7 +1701,7 @@ ProcessUtilitySlow(ParseState *pstate,
 
 			case T_CreateTrigStmt:
 				address = CreateTrigger((CreateTrigStmt *) parsetree,
-										queryString, InvalidOid,
+										queryString,
 										InvalidOid, InvalidOid, InvalidOid,
 										InvalidOid, NULL, false, false);
 				break;
diff --git a/src/include/commands/trigger.h b/src/include/commands/trigger.h
index ae7abd456bf..a2b69576093 100644
--- a/src/include/commands/trigger.h
+++ b/src/include/commands/trigger.h
@@ -152,11 +152,11 @@ extern PGDLLIMPORT int SessionReplicationRole;
 #define TRIGGER_DISABLED					'D'
 
 extern ObjectAddress CreateTrigger(CreateTrigStmt *stmt, const char *queryString,
-								   Oid refRelOid, Oid constraintOid, Oid indexOid,
+								   Oid constraintOid, Oid indexOid,
 								   Oid funcoid, Oid parentTriggerOid, Node *whenClause,
 								   bool isInternal, bool in_partition);
 extern ObjectAddress CreateTriggerFiringOn(CreateTrigStmt *stmt, const char *queryString,
-										   Oid refRelOid, Oid constraintOid,
+										   Oid constraintOid,
 										   Oid indexOid, Oid funcoid, Oid parentTriggerOid,
 										   Node *whenClause, bool isInternal, bool in_partition,
 										   char trigger_fires_when);
diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h
index f876f00ab9f..a3bf11e4fb7 100644
--- a/src/include/nodes/parsenodes.h
+++ b/src/include/nodes/parsenodes.h
@@ -3164,6 +3164,7 @@ typedef struct CreateTrigStmt
 	bool		deferrable;		/* [NOT] DEFERRABLE */
 	bool		initdeferred;	/* INITIALLY {DEFERRED|IMMEDIATE} */
 	RangeVar   *constrrel;		/* opposite relation, if RI trigger */
+	Oid			constrrelOid;	/* opposite relation Oid, if RI trigger */
 } CreateTrigStmt;
 
 /* ----------------------
-- 
2.34.1

