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

application/x-patch

Filename: v14-0003-Move-the-RemoveInheritedConstraint-function-call.patch
Type: application/x-patch
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 v14-0003
Subject: Move the RemoveInheritedConstraint() function call slightly earlier.
File+
src/backend/commands/tablecmds.c 15 15
From dfb13da6362047344f104e84ca7e5a5e97ab7797 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 v14 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 37e52a853cc..44189c8c7f7 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -11550,6 +11550,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
@@ -11586,21 +11601,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