v15-0003-Move-the-RemoveInheritedConstraint-function-call.patch

application/octet-stream

Filename: v15-0003-Move-the-RemoveInheritedConstraint-function-call.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 v15-0003
Subject: Move the RemoveInheritedConstraint() function call slightly earlier.
File+
src/backend/commands/tablecmds.c 15 15
From 443b0ec319cbbd884fd1cfe16fa872a070a1474c Mon Sep 17 00:00:00 2001
From: Amul Sul <amul.sul@enterprisedb.com>
Date: Mon, 10 Feb 2025 09:12:06 +0530
Subject: [PATCH v15 3/9] Move the RemoveInheritedConstraint() function call
 slightly earlier.

This change is harmless and does not affect the existing intended
operation. It is necessary for the feature patch operation, where we
may need to change the child constraint to enforced. In this case, we
would create the necessary triggers and queue the constraint for
validation, so it is important to remove any unnecessary constraints
before proceeding.

-- NOTE --
This is a small change that could have been included in the previous
"split tryAttachPartitionForeignKey" refactoring patch, but was kept
separate to highlight the changes.
---------
---
 src/backend/commands/tablecmds.c | 30 +++++++++++++++---------------
 1 file changed, 15 insertions(+), 15 deletions(-)

diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index cb62e138a94..c501201cad7 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -11551,6 +11551,21 @@ AttachPartitionForeignKey(List **wqueue,
 	partConstrFrelid = partConstr->confrelid;
 	partConstrRelid = partConstr->conrelid;
 
+	/*
+	 * If the referenced table is partitioned, then the partition we're
+	 * attaching now has extra pg_constraint rows and action triggers that are
+	 * no longer needed.  Remove those.
+	 */
+	if (get_rel_relkind(partConstrFrelid) == RELKIND_PARTITIONED_TABLE)
+	{
+		Relation	pg_constraint = table_open(ConstraintRelationId, RowShareLock);
+
+		RemoveInheritedConstraint(pg_constraint, trigrel, partConstrOid,
+								  partConstrRelid);
+
+		table_close(pg_constraint, RowShareLock);
+	}
+
 	/*
 	 * Will we need to validate this constraint?   A valid parent constraint
 	 * implies that all child constraints have been validated, so if this one
@@ -11587,21 +11602,6 @@ AttachPartitionForeignKey(List **wqueue,
 	TriggerSetParentTrigger(trigrel, updateTriggerOid, parentUpdTrigger,
 							RelationGetRelid(partition));
 
-	/*
-	 * If the referenced table is partitioned, then the partition we're
-	 * attaching now has extra pg_constraint rows and action triggers that are
-	 * no longer needed.  Remove those.
-	 */
-	if (get_rel_relkind(partConstrFrelid) == RELKIND_PARTITIONED_TABLE)
-	{
-		Relation	pg_constraint = table_open(ConstraintRelationId, RowShareLock);
-
-		RemoveInheritedConstraint(pg_constraint, trigrel, partConstrOid,
-								  partConstrRelid);
-
-		table_close(pg_constraint, RowShareLock);
-	}
-
 	/*
 	 * We updated this pg_constraint row above to set its parent; validating
 	 * it will cause its convalidated flag to change, so we need CCI here.  In
-- 
2.43.5