From a5a79e9046e5b527144a0b725e1edb5a58546334 Mon Sep 17 00:00:00 2001
From: Jehan-Guillaume de Rorthais <jgdr@dalibo.com>
Date: Tue, 24 Sep 2024 18:10:33 +0200
Subject: [PATCH v3 3/4] Use addFkConstraint() in addFkRecurseReferencing()

---
 src/backend/commands/tablecmds.c | 124 ++++++++++++-------------------
 1 file changed, 49 insertions(+), 75 deletions(-)

diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index f7e0b8fa87..9dd4f2e94f 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -348,6 +348,14 @@ typedef struct ForeignTruncateInfo
 	List	   *rels;
 } ForeignTruncateInfo;
 
+typedef enum FKConstraintSide
+{
+	FK_BOTH_SIDE,
+	FK_REFERENCED_SIDE,
+	FK_REFERENCING_SIDE
+}
+FKConstraintSide;
+
 /*
  * Partition tables are expected to be dropped when the parent partitioned
  * table gets dropped. Hence for partitioning we use AUTO dependency.
@@ -509,7 +517,8 @@ static ObjectAddress ATAddForeignKeyConstraint(List **wqueue, AlteredTableInfo *
 											   bool recurse, bool recursing,
 											   LOCKMODE lockmode);
 static ObjectAddress addFkConstraint(Constraint *fkconstraint, Relation rel,
-									 Relation pkrel, Oid indexOid, Oid parentConstr,
+									 Relation pkrel, Oid indexOid,
+									 Oid parentConstr, FKConstraintSide fkside,
 									 int numfks, int16 *pkattnum, int16 *fkattnum,
 									 Oid *pfeqoperators, Oid *ppeqoperators,
 									 Oid *ffeqoperators, int numfkdelsetcols,
@@ -10072,6 +10081,7 @@ ATAddForeignKeyConstraint(List **wqueue, AlteredTableInfo *tab, Relation rel,
 	address = addFkConstraint(fkconstraint, rel, pkrel,
 							  indexOid,
 							  InvalidOid,	/* no parent constraint */
+							  FK_BOTH_SIDE,
 							  numfks,
 							  pkattnum,
 							  fkattnum,
@@ -10169,6 +10179,7 @@ validateFkOnDeleteSetColumns(int numfks, const int16 *fkattnums,
  * * indexOid is the OID of the index (on pkrel) implementing this constraint.
  * * parentConstr is the OID of a parent constraint; InvalidOid if this is a
  *   top-level constraint.
+ * * fkside is the side of FK related to this inherited constraint.
  * * numfks is the number of columns in the foreign key.
  * * pkattnum is the attnum array of referenced attributes.
  * * fkattnum is the attnum array of referencing attributes.
@@ -10181,8 +10192,9 @@ validateFkOnDeleteSetColumns(int numfks, const int16 *fkattnums,
  */
 static ObjectAddress
 addFkConstraint(Constraint *fkconstraint, Relation rel, Relation pkrel,
-				Oid indexOid, Oid parentConstr, int numfks, int16 *pkattnum,
-				int16 *fkattnum, Oid *pfeqoperators, Oid *ppeqoperators,
+				Oid indexOid, Oid parentConstr, FKConstraintSide fkside,
+				int numfks, int16 *pkattnum, int16 *fkattnum,
+				Oid *pfeqoperators, Oid *ppeqoperators,
 				Oid *ffeqoperators, int numfkdelsetcols, int16 *fkdelsetcols,
 				bool with_period)
 {
@@ -10273,18 +10285,31 @@ addFkConstraint(Constraint *fkconstraint, Relation rel, Relation pkrel,
 
 	ObjectAddressSet(address, ConstraintRelationId, constrOid);
 
-	/*
-	 * Mark the child constraint as part of the parent constraint; it must not
-	 * be dropped on its own.  (This constraint is deleted when the partition
-	 * is detached, but a special check needs to occur that the partition
-	 * contains no referenced values.)
-	 */
 	if (OidIsValid(parentConstr))
 	{
 		ObjectAddress referenced;
 
 		ObjectAddressSet(referenced, ConstraintRelationId, parentConstr);
-		recordDependencyOn(&address, &referenced, DEPENDENCY_INTERNAL);
+		if (fkside == FK_REFERENCED_SIDE)
+		{
+			/*
+			 * Mark the child constraint as part of the parent constraint; it must not
+			 * be dropped on its own.  (This constraint is deleted when the partition
+			 * is detached, but a special check needs to occur that the partition
+			 * contains no referenced values.)
+			 */
+			recordDependencyOn(&address, &referenced, DEPENDENCY_INTERNAL);
+		}
+		else
+		{
+			/*
+			 * Give this constraint partition-type dependencies on the parent
+			 * constraint as well as the table.
+			 */
+			recordDependencyOn(&address, &referenced, DEPENDENCY_PARTITION_PRI);
+			ObjectAddressSet(referenced, RelationRelationId, RelationGetRelid(rel));
+			recordDependencyOn(&address, &referenced, DEPENDENCY_PARTITION_SEC);
+		}
 	}
 
 	/* make new constraint visible, in case we add more */
@@ -10392,8 +10417,8 @@ addFkRecurseReferenced(List **wqueue, Constraint *fkconstraint, Relation rel,
 					 indexOid, RelationGetRelationName(partRel));
 
 			address = addFkConstraint(fkconstraint, rel, partRel,
-								   partIndexId, parentConstr, numfks,
-								   mapped_pkattnum, fkattnum,
+								   partIndexId, parentConstr, FK_REFERENCED_SIDE,
+								   numfks, mapped_pkattnum, fkattnum,
 								   pfeqoperators, ppeqoperators, ffeqoperators,
 								   numfkdelsetcols, fkdelsetcols, with_period);
 			addFkRecurseReferenced(wqueue, fkconstraint, rel, partRel,
@@ -10540,10 +10565,7 @@ addFkRecurseReferencing(List **wqueue, Constraint *fkconstraint, Relation rel,
 			AttrMap    *attmap;
 			AttrNumber	mapped_fkattnum[INDEX_MAX_KEYS];
 			bool		attached;
-			char	   *conname;
-			Oid			constrOid;
-			ObjectAddress address,
-						referenced;
+			ObjectAddress address;
 			ListCell   *cell;
 
 			CheckAlterTableIsSafe(partition);
@@ -10586,66 +10608,18 @@ addFkRecurseReferencing(List **wqueue, Constraint *fkconstraint, Relation rel,
 			/*
 			 * No luck finding a good constraint to reuse; create our own.
 			 */
-			if (ConstraintNameIsUsed(CONSTRAINT_RELATION,
-									 RelationGetRelid(partition),
-									 fkconstraint->conname))
-				conname = ChooseConstraintName(RelationGetRelationName(partition),
-											   ChooseForeignKeyConstraintNameAddition(fkconstraint->fk_attrs),
-											   "fkey",
-											   RelationGetNamespace(partition), NIL);
-			else
-				conname = fkconstraint->conname;
-			constrOid =
-				CreateConstraintEntry(conname,
-									  RelationGetNamespace(partition),
-									  CONSTRAINT_FOREIGN,
-									  fkconstraint->deferrable,
-									  fkconstraint->initdeferred,
-									  fkconstraint->initially_valid,
-									  parentConstr,
-									  partitionId,
-									  mapped_fkattnum,
-									  numfks,
-									  numfks,
-									  InvalidOid,
-									  indexOid,
-									  RelationGetRelid(pkrel),
-									  pkattnum,
-									  pfeqoperators,
-									  ppeqoperators,
-									  ffeqoperators,
-									  numfks,
-									  fkconstraint->fk_upd_action,
-									  fkconstraint->fk_del_action,
-									  fkdelsetcols,
-									  numfkdelsetcols,
-									  fkconstraint->fk_matchtype,
-									  NULL,
-									  NULL,
-									  NULL,
-									  false,
-									  1,
-									  false,
-									  with_period,	/* conPeriod */
-									  false);
-
-			/*
-			 * Give this constraint partition-type dependencies on the parent
-			 * constraint as well as the table.
-			 */
-			ObjectAddressSet(address, ConstraintRelationId, constrOid);
-			ObjectAddressSet(referenced, ConstraintRelationId, parentConstr);
-			recordDependencyOn(&address, &referenced, DEPENDENCY_PARTITION_PRI);
-			ObjectAddressSet(referenced, RelationRelationId, partitionId);
-			recordDependencyOn(&address, &referenced, DEPENDENCY_PARTITION_SEC);
-
-			/* Make all this visible before recursing */
-			CommandCounterIncrement();
+			address = addFkConstraint(fkconstraint,
+									  partition, pkrel, indexOid, parentConstr,
+									  FK_REFERENCING_SIDE, numfks, pkattnum,
+									  mapped_fkattnum, pfeqoperators,
+									  ppeqoperators, ffeqoperators,
+									  numfkdelsetcols, fkdelsetcols,
+									  with_period);
 
 			/* call ourselves to finalize the creation and we're done */
 			addFkRecurseReferencing(wqueue, fkconstraint, partition, pkrel,
 									indexOid,
-									constrOid,
+									address.objectId,
 									numfks,
 									pkattnum,
 									mapped_fkattnum,
@@ -10880,9 +10854,9 @@ CloneFkReferenced(Relation parentRel, Relation partitionRel)
 									&deleteTriggerOid, &updateTriggerOid);
 
 		address = addFkConstraint(fkconstraint, fkRel, partitionRel, partIndexId,
-								  constrOid, numfks, mapped_confkey, conkey,
-								  conpfeqop, conppeqop, conffeqop,
-								  numfkdelsetcols, confdelsetcols,
+								  constrOid, FK_REFERENCED_SIDE, numfks,
+								  mapped_confkey, conkey, conpfeqop, conppeqop,
+								  conffeqop, numfkdelsetcols, confdelsetcols,
 								  constrForm->conperiod);
 		addFkRecurseReferenced(NULL,
 							   fkconstraint,
-- 
2.46.0

