From 5ab3a089fc40a288f48210a728540634ad060e98 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 v14 1/9] Add ATAlterConstraint struct for ALTER .. CONSTRAINT.

Replace the use of Constraint with the new ATAlterConstraint 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 72a1b64c2a2..ec50fbf0809 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, ATAlterConstraint *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(ATAlterConstraint *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(ATAlterConstraint *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);
@@ -5450,7 +5450,10 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab,
 											   lockmode);
 			break;
 		case AT_AlterConstraint:	/* ALTER CONSTRAINT */
-			address = ATExecAlterConstraint(rel, cmd, false, false, lockmode);
+			address =
+				ATExecAlterConstraint(rel,
+									  castNode(ATAlterConstraint, cmd->def),
+									  false, false, lockmode);
 			break;
 		case AT_ValidateConstraint: /* VALIDATE CONSTRAINT */
 			address = ATExecValidateConstraint(wqueue, rel, cmd->name, cmd->recurse,
@@ -11801,10 +11804,9 @@ GetForeignKeyCheckTriggers(Relation trigrel,
  * InvalidObjectAddress.
  */
 static ObjectAddress
-ATExecAlterConstraint(Relation rel, AlterTableCmd *cmd, bool recurse,
+ATExecAlterConstraint(Relation rel, ATAlterConstraint *cmdcon, bool recurse,
 					  bool recursing, LOCKMODE lockmode)
 {
-	Constraint *cmdcon;
 	Relation	conrel;
 	Relation	tgrel;
 	SysScanDesc scan;
@@ -11815,8 +11817,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);
 
@@ -11939,9 +11939,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(ATAlterConstraint *cmdcon, Relation conrel,
+						 Relation tgrel, Relation rel, HeapTuple contuple,
+						 List **otherrelids, LOCKMODE lockmode)
 {
 	Form_pg_constraint currcon;
 	Oid			conoid;
@@ -12079,9 +12079,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(ATAlterConstraint *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 d3887628d46..ec4c07fab5d 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);
+					ATAlterConstraint *c = makeNode(ATAlterConstraint);
 
 					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 8dd421fa0ef..c0018abf16f 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 ATAlterConstraint
+{
+	NodeTag		type;
+
+	char	   *conname;			/* Constraint name */
+
+	bool		deferrable;			/* DEFERRABLE? */
+	bool		initdeferred;		/* INITIALLY DEFERRED? */
+} ATAlterConstraint;
+
 
 /* ----------------------
  *	Alter Domain
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index b6c170ac249..a501d2b4999 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -66,6 +66,7 @@ AllocSetFreeList
 AllocateDesc
 AllocateDescKind
 AlterCollationStmt
+ATAlterConstraint
 AlterDatabaseRefreshCollStmt
 AlterDatabaseSetStmt
 AlterDatabaseStmt
-- 
2.43.5

