v12-0001-Add-AlterConstraintStmt-struct-for-ALTER-.-CONST.patch

application/octet-stream

Filename: v12-0001-Add-AlterConstraintStmt-struct-for-ALTER-.-CONST.patch
Type: application/octet-stream
Part: 0
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 v12-0001
Subject: Add AlterConstraintStmt struct for ALTER .. CONSTRAINT.
File+
src/backend/commands/tablecmds.c 18 18
src/backend/parser/gram.y 1 2
src/include/nodes/parsenodes.h 14 0
src/tools/pgindent/typedefs.list 1 0
From d9f8118d6d89a50d5618d9f346b6a6c20c11d59a Mon Sep 17 00:00:00 2001
From: Amul Sul <amul.sul@enterprisedb.com>
Date: Tue, 4 Feb 2025 09:55:18 +0530
Subject: [PATCH v12 1/7] Add AlterConstraintStmt struct for ALTER ..
 CONSTRAINT.

Replace the use of Constraint with the new AlterConstraintStmt struct,
which allows us to pass additional information, such as whether the
constraint attribute is set. This is necessary for the feature patches
that involve altering the enforceability of foreign key and check
constraints as part of the implementation.
---
 src/backend/commands/tablecmds.c | 36 ++++++++++++++++----------------
 src/backend/parser/gram.y        |  3 +--
 src/include/nodes/parsenodes.h   | 14 +++++++++++++
 src/tools/pgindent/typedefs.list |  1 +
 4 files changed, 34 insertions(+), 20 deletions(-)

diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 18f64db6e39..2158b3d6c66 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -389,17 +389,17 @@ static void AlterIndexNamespaces(Relation classRel, Relation rel,
 static void AlterSeqNamespaces(Relation classRel, Relation rel,
 							   Oid oldNspOid, Oid newNspOid, ObjectAddresses *objsMoved,
 							   LOCKMODE lockmode);
-static ObjectAddress ATExecAlterConstraint(Relation rel, AlterTableCmd *cmd,
+static ObjectAddress ATExecAlterConstraint(Relation rel, AlterConstraintStmt *cmdcon,
 										   bool recurse, bool recursing, LOCKMODE lockmode);
-static bool ATExecAlterConstrRecurse(Constraint *cmdcon, Relation conrel, Relation tgrel,
-									 Relation rel, HeapTuple contuple, List **otherrelids,
-									 LOCKMODE lockmode);
+static bool ATExecAlterConstrRecurse(AlterConstraintStmt *cmdcon, Relation conrel,
+									 Relation tgrel, Relation rel, HeapTuple contuple,
+									 List **otherrelids, LOCKMODE lockmode);
 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);
+static void ATExecAlterChildConstr(AlterConstraintStmt *cmdcon, Relation conrel,
+								   Relation tgrel, Relation rel, HeapTuple contuple,
+								   List **otherrelids, LOCKMODE lockmode);
 static ObjectAddress ATExecValidateConstraint(List **wqueue,
 											  Relation rel, char *constrName,
 											  bool recurse, bool recursing, LOCKMODE lockmode);
@@ -5432,7 +5432,10 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab,
 											   lockmode);
 			break;
 		case AT_AlterConstraint:	/* ALTER CONSTRAINT */
-			address = ATExecAlterConstraint(rel, cmd, false, false, lockmode);
+			address =
+				ATExecAlterConstraint(rel,
+									  castNode(AlterConstraintStmt, cmd->def),
+									  false, false, lockmode);
 			break;
 		case AT_ValidateConstraint: /* VALIDATE CONSTRAINT */
 			address = ATExecValidateConstraint(wqueue, rel, cmd->name, cmd->recurse,
@@ -11711,10 +11714,9 @@ GetForeignKeyCheckTriggers(Relation trigrel,
  * InvalidObjectAddress.
  */
 static ObjectAddress
-ATExecAlterConstraint(Relation rel, AlterTableCmd *cmd, bool recurse,
+ATExecAlterConstraint(Relation rel, AlterConstraintStmt *cmdcon, bool recurse,
 					  bool recursing, LOCKMODE lockmode)
 {
-	Constraint *cmdcon;
 	Relation	conrel;
 	Relation	tgrel;
 	SysScanDesc scan;
@@ -11725,8 +11727,6 @@ ATExecAlterConstraint(Relation rel, AlterTableCmd *cmd, bool recurse,
 	List	   *otherrelids = NIL;
 	ListCell   *lc;
 
-	cmdcon = castNode(Constraint, cmd->def);
-
 	conrel = table_open(ConstraintRelationId, RowExclusiveLock);
 	tgrel = table_open(TriggerRelationId, RowExclusiveLock);
 
@@ -11849,9 +11849,9 @@ ATExecAlterConstraint(Relation rel, AlterTableCmd *cmd, bool recurse,
  * but existing releases don't do that.)
  */
 static bool
-ATExecAlterConstrRecurse(Constraint *cmdcon, Relation conrel, Relation tgrel,
-						 Relation rel, HeapTuple contuple, List **otherrelids,
-						 LOCKMODE lockmode)
+ATExecAlterConstrRecurse(AlterConstraintStmt *cmdcon, Relation conrel,
+						 Relation tgrel, Relation rel, HeapTuple contuple,
+						 List **otherrelids, LOCKMODE lockmode)
 {
 	Form_pg_constraint currcon;
 	Oid			conoid;
@@ -11989,9 +11989,9 @@ AlterConstrTriggerDeferrability(Oid conoid, Relation tgrel, Relation rel,
  * ATExecAlterConstrRecurse.
  */
 static void
-ATExecAlterChildConstr(Constraint *cmdcon, Relation conrel, Relation tgrel,
-					   Relation rel, HeapTuple contuple, List **otherrelids,
-					   LOCKMODE lockmode)
+ATExecAlterChildConstr(AlterConstraintStmt *cmdcon, Relation conrel,
+					   Relation tgrel, Relation rel, HeapTuple contuple,
+					   List **otherrelids, LOCKMODE lockmode)
 {
 	Form_pg_constraint currcon;
 	Oid			conoid;
diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y
index d7f9c00c409..e845e888b06 100644
--- a/src/backend/parser/gram.y
+++ b/src/backend/parser/gram.y
@@ -2657,11 +2657,10 @@ alter_table_cmd:
 			| ALTER CONSTRAINT name ConstraintAttributeSpec
 				{
 					AlterTableCmd *n = makeNode(AlterTableCmd);
-					Constraint *c = makeNode(Constraint);
+					AlterConstraintStmt *c = makeNode(AlterConstraintStmt);
 
 					n->subtype = AT_AlterConstraint;
 					n->def = (Node *) c;
-					c->contype = CONSTR_FOREIGN; /* others not supported, yet */
 					c->conname = $3;
 					processCASbits($4, @4, "ALTER CONSTRAINT statement",
 									&c->deferrable,
diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h
index ffe155ee20e..4ae9906a674 100644
--- a/src/include/nodes/parsenodes.h
+++ b/src/include/nodes/parsenodes.h
@@ -2503,6 +2503,20 @@ typedef struct AlterCollationStmt
 	List	   *collname;
 } AlterCollationStmt;
 
+/* ----------------------
+ * Alter Constraint
+ * ----------------------
+ */
+typedef struct AlterConstraintStmt
+{
+	NodeTag		type;
+
+	char	   *conname;		/* Constraint name */
+
+	bool		deferrable;		/* DEFERRABLE? */
+	bool		initdeferred;	/* INITIALLY DEFERRED? */
+} AlterConstraintStmt;
+
 
 /* ----------------------
  *	Alter Domain
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 9a3bee93dec..b7780ad57a3 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -66,6 +66,7 @@ AllocSetFreeList
 AllocateDesc
 AllocateDescKind
 AlterCollationStmt
+AlterConstraintStmt
 AlterDatabaseRefreshCollStmt
 AlterDatabaseSetStmt
 AlterDatabaseStmt
-- 
2.43.5