From 2fd83ff14e0a3880c1ff5f6458cfd52638724a7b Mon Sep 17 00:00:00 2001
From: Jehan-Guillaume de Rorthais <jgdr@dalibo.com>
Date: Wed, 25 Sep 2024 13:31:07 +0200
Subject: [PATCH v3-17 4/4] Use addFkConstraint() in CloneFkReferencing()

---
 src/backend/commands/tablecmds.c | 123 +++++++++++--------------------
 1 file changed, 43 insertions(+), 80 deletions(-)

diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index ca9fb3818b..23be2eefd0 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -512,13 +512,14 @@ static ObjectAddress ATAddForeignKeyConstraint(List **wqueue, AlteredTableInfo *
 											   Relation rel, Constraint *fkconstraint,
 											   bool recurse, bool recursing,
 											   LOCKMODE lockmode);
-static ObjectAddress addFkConstraint(Constraint *fkconstraint, Relation rel,
+static ObjectAddress addFkConstraint(char *constraintname,
+									 Constraint *fkconstraint, Relation rel,
 									 Relation pkrel, Oid indexOid,
 									 Oid parentConstr, FKConstraintSide fkside,
 									 int numfks, int16 *pkattnum, int16 *fkattnum,
 									 Oid *pfeqoperators, Oid *ppeqoperators,
 									 Oid *ffeqoperators, int numfkdelsetcols,
-									 int16 *fkdelsetcols);
+									 int16 *fkdelsetcols, bool is_internal);
 static void addFkRecurseReferenced(List **wqueue, Constraint *fkconstraint,
 											Relation rel, Relation pkrel, Oid indexOid, Oid parentConstr,
 											int numfks, int16 *pkattnum, int16 *fkattnum,
@@ -9952,7 +9953,7 @@ ATAddForeignKeyConstraint(List **wqueue, AlteredTableInfo *tab, Relation rel,
 	 * Create all the constraint and trigger objects, recursing to partitions
 	 * as necessary.  First handle the referenced side.
 	 */
-	address = addFkConstraint(fkconstraint, rel, pkrel,
+	address = addFkConstraint(fkconstraint->conname, fkconstraint, rel, pkrel,
 							  indexOid,
 							  InvalidOid,	/* no parent constraint */
 							  FK_BOTH_SIDE,
@@ -9963,7 +9964,8 @@ ATAddForeignKeyConstraint(List **wqueue, AlteredTableInfo *tab, Relation rel,
 							  ppeqoperators,
 							  ffeqoperators,
 							  numfkdelsetcols,
-							  fkdelsetcols);
+							  fkdelsetcols,
+							  false);
 
 	addFkRecurseReferenced(wqueue, fkconstraint, rel, pkrel,
 									 indexOid,
@@ -10044,6 +10046,8 @@ validateFkOnDeleteSetColumns(int numfks, const int16 *fkattnums,
  *
  * Creates a pg_constraint rows referencing a parent if given.
  *
+ * * constraintname the base name for the constraint being added. This is
+ *   copied to fkconstraint->conname if the later is not set.
  * * fkconstraint is the constraint being added.
  * * rel is the root referencing relation.
  * * pkrel is the referenced relation; might be a partition, if recursing.
@@ -10061,11 +10065,12 @@ validateFkOnDeleteSetColumns(int numfks, const int16 *fkattnums,
  *      NULL/DEFAULT clause.
  */
 static ObjectAddress
-addFkConstraint(Constraint *fkconstraint, Relation rel, Relation pkrel,
-				Oid indexOid, Oid parentConstr, FKConstraintSide fkside,
-				int numfks, int16 *pkattnum, int16 *fkattnum,
-				Oid *pfeqoperators, Oid *ppeqoperators,
-				Oid *ffeqoperators, int numfkdelsetcols, int16 *fkdelsetcols)
+addFkConstraint(char *constraintname, Constraint *fkconstraint, Relation rel,
+				Relation pkrel, Oid indexOid, Oid parentConstr,
+				FKConstraintSide fkside, int numfks, int16 *pkattnum,
+				int16 *fkattnum, Oid *pfeqoperators, Oid *ppeqoperators,
+				Oid *ffeqoperators, int numfkdelsetcols, int16 *fkdelsetcols,
+				bool is_internal)
 {
 	ObjectAddress address;
 	Oid			constrOid;
@@ -10091,13 +10096,16 @@ addFkConstraint(Constraint *fkconstraint, Relation rel, Relation pkrel,
 	 */
 	if (ConstraintNameIsUsed(CONSTRAINT_RELATION,
 							 RelationGetRelid(rel),
-							 fkconstraint->conname))
+							 constraintname))
 		conname = ChooseConstraintName(RelationGetRelationName(rel),
 									   ChooseForeignKeyConstraintNameAddition(fkconstraint->fk_attrs),
 									   "fkey",
 									   RelationGetNamespace(rel), NIL);
 	else
-		conname = fkconstraint->conname;
+		conname = constraintname;
+
+	if (fkconstraint->conname == NULL)
+		fkconstraint->conname = pstrdup(conname);
 
 	if (OidIsValid(parentConstr))
 	{
@@ -10149,7 +10157,7 @@ addFkConstraint(Constraint *fkconstraint, Relation rel, Relation pkrel,
 									  conislocal,	/* islocal */
 									  coninhcount,	/* inhcount */
 									  connoinherit, /* conNoInherit */
-									  false);	/* is_internal */
+									  is_internal);	/* is_internal */
 
 	ObjectAddressSet(address, ConstraintRelationId, constrOid);
 
@@ -10282,11 +10290,12 @@ addFkRecurseReferenced(List **wqueue, Constraint *fkconstraint, Relation rel,
 				elog(ERROR, "index for %u not found in partition %s",
 					 indexOid, RelationGetRelationName(partRel));
 
-			address = addFkConstraint(fkconstraint, rel, partRel,
-								   partIndexId, parentConstr, FK_REFERENCED_SIDE,
-								   numfks, mapped_pkattnum, fkattnum,
-								   pfeqoperators, ppeqoperators, ffeqoperators,
-								   numfkdelsetcols, fkdelsetcols);
+			address = addFkConstraint(fkconstraint->conname, fkconstraint, rel,
+								   partRel, partIndexId, parentConstr,
+								   FK_REFERENCED_SIDE, numfks, mapped_pkattnum,
+								   fkattnum, pfeqoperators, ppeqoperators,
+								   ffeqoperators, numfkdelsetcols,
+								   fkdelsetcols, false);
 			addFkRecurseReferenced(wqueue, fkconstraint, rel, partRel,
 								   partIndexId, address.objectId, numfks,
 								   mapped_pkattnum, fkattnum,
@@ -10472,12 +10481,12 @@ addFkRecurseReferencing(List **wqueue, Constraint *fkconstraint, Relation rel,
 			 * No luck finding a good constraint to reuse; create our own.
 			 */
 
-			address = addFkConstraint(fkconstraint,
+			address = addFkConstraint(fkconstraint->conname, fkconstraint,
 									  partition, pkrel, indexOid, parentConstr,
 									  FK_REFERENCING_SIDE, numfks, pkattnum,
 									  mapped_fkattnum, pfeqoperators,
 									  ppeqoperators, ffeqoperators,
-									  numfkdelsetcols, fkdelsetcols);
+									  numfkdelsetcols, fkdelsetcols, false);
 
 			/* call ourselves to finalize the creation and we're done */
 			addFkRecurseReferencing(wqueue, fkconstraint, partition, pkrel,
@@ -10715,10 +10724,11 @@ CloneFkReferenced(Relation parentRel, Relation partitionRel)
 									constrForm->confrelid, constrForm->conrelid,
 									&deleteTriggerOid, &updateTriggerOid);
 
-		address = addFkConstraint(fkconstraint, fkRel, partitionRel, partIndexId,
-								  constrOid, FK_REFERENCED_SIDE, numfks,
-								  mapped_confkey, conkey, conpfeqop, conppeqop,
-								  conffeqop, numfkdelsetcols, confdelsetcols);
+		address = addFkConstraint(fkconstraint->conname, fkconstraint, fkRel,
+								  partitionRel, partIndexId, constrOid,
+								  FK_REFERENCED_SIDE, numfks, mapped_confkey,
+								  conkey, conpfeqop, conppeqop, conffeqop,
+								  numfkdelsetcols, confdelsetcols, false);
 		addFkRecurseReferenced(NULL,
 							   fkconstraint,
 							   fkRel,
@@ -10839,9 +10849,7 @@ CloneFkReferencing(List **wqueue, Relation parentRel, Relation partRel)
 		Constraint *fkconstraint;
 		bool		attached;
 		Oid			indexOid;
-		Oid			constrOid;
-		ObjectAddress address,
-					referenced;
+		ObjectAddress address;
 		ListCell   *lc;
 		Oid			insertTriggerOid,
 					updateTriggerOid;
@@ -10938,7 +10946,7 @@ CloneFkReferencing(List **wqueue, Relation parentRel, Relation partRel)
 		fkconstraint->old_conpfeqop = NIL;
 		fkconstraint->old_pktable_oid = InvalidOid;
 		fkconstraint->skip_validation = false;
-		fkconstraint->initially_valid = true;
+		fkconstraint->initially_valid = constrForm->convalidated;
 		for (int i = 0; i < numfks; i++)
 		{
 			Form_pg_attribute att;
@@ -10948,71 +10956,26 @@ CloneFkReferencing(List **wqueue, Relation parentRel, Relation partRel)
 			fkconstraint->fk_attrs = lappend(fkconstraint->fk_attrs,
 											 makeString(NameStr(att->attname)));
 		}
-		if (ConstraintNameIsUsed(CONSTRAINT_RELATION,
-								 RelationGetRelid(partRel),
-								 NameStr(constrForm->conname)))
-			fkconstraint->conname =
-				ChooseConstraintName(RelationGetRelationName(partRel),
-									 ChooseForeignKeyConstraintNameAddition(fkconstraint->fk_attrs),
-									 "fkey",
-									 RelationGetNamespace(partRel), NIL);
-		else
-			fkconstraint->conname = pstrdup(NameStr(constrForm->conname));
 
 		indexOid = constrForm->conindid;
-		constrOid =
-			CreateConstraintEntry(fkconstraint->conname,
-								  constrForm->connamespace,
-								  CONSTRAINT_FOREIGN,
-								  fkconstraint->deferrable,
-								  fkconstraint->initdeferred,
-								  constrForm->convalidated,
-								  parentConstrOid,
-								  RelationGetRelid(partRel),
-								  mapped_conkey,
-								  numfks,
-								  numfks,
-								  InvalidOid,	/* not a domain constraint */
-								  indexOid,
-								  constrForm->confrelid,	/* same foreign rel */
-								  confkey,
-								  conpfeqop,
-								  conppeqop,
-								  conffeqop,
-								  numfks,
-								  fkconstraint->fk_upd_action,
-								  fkconstraint->fk_del_action,
-								  confdelsetcols,
-								  numfkdelsetcols,
-								  fkconstraint->fk_matchtype,
-								  NULL,
-								  NULL,
-								  NULL,
-								  false,	/* islocal */
-								  1,	/* inhcount */
-								  false,	/* conNoInherit */
-								  true);
 
-		/* Set up partition dependencies for the new constraint */
-		ObjectAddressSet(address, ConstraintRelationId, constrOid);
-		ObjectAddressSet(referenced, ConstraintRelationId, parentConstrOid);
-		recordDependencyOn(&address, &referenced, DEPENDENCY_PARTITION_PRI);
-		ObjectAddressSet(referenced, RelationRelationId,
-						 RelationGetRelid(partRel));
-		recordDependencyOn(&address, &referenced, DEPENDENCY_PARTITION_SEC);
+		address = addFkConstraint(NameStr(constrForm->conname), fkconstraint,
+								  partRel, pkrel, indexOid, parentConstrOid,
+								  FK_REFERENCING_SIDE, numfks, confkey,
+								  mapped_conkey, conpfeqop,
+								  conppeqop, conffeqop,
+								  numfkdelsetcols, confdelsetcols,
+								  true);
 
 		/* Done with the cloned constraint's tuple */
 		ReleaseSysCache(tuple);
 
-		/* Make all this visible before recursing */
-		CommandCounterIncrement();
-
 		addFkRecurseReferencing(wqueue,
 								fkconstraint,
 								partRel,
 								pkrel,
 								indexOid,
-								constrOid,
+								address.objectId,
 								numfks,
 								confkey,
 								mapped_conkey,
-- 
2.46.0

