hi.

I noticed the following code pattern repeated several times in
ExecInitPartitionInfo.
```
if (part_attmap == NULL)
            build_attrmap_by_name(RelationGetDescr(partrel),
                                  RelationGetDescr(firstResultRel),
                                  false);
```

we can consolidated into one, like:

+    if (node != NULL &&
+        (list_length(node->withCheckOptionLists) > 0 ||
+         list_length(node->returningLists) > 0 ||
+         node->onConflictAction != ONCONFLICT_NONE ||
+         node->operation == CMD_MERGE))
+    {
+        part_attmap =
+            build_attrmap_by_name(RelationGetDescr(partrel),
+                                  RelationGetDescr(firstResultRel),
+                                  false);
+    }
+

it matters, because nearby patch ON CONFLICT DO SELECT is= going to add another
one.

what do you think?

--
jian
https://www.enterprisedb.com/
From 32c9323b68fd6abb45dc0edec0a65a7dd076cef7 Mon Sep 17 00:00:00 2001
From: jian he <[email protected]>
Date: Fri, 28 Nov 2025 18:50:35 +0800
Subject: [PATCH v1 1/1] refactor ExecInitPartitionInfo

Discussion: https://postgr.es/m/
---
 src/backend/executor/execPartition.c | 36 ++++++++++------------------
 1 file changed, 13 insertions(+), 23 deletions(-)

diff --git a/src/backend/executor/execPartition.c b/src/backend/executor/execPartition.c
index 0dcce181f09..f855d2c235c 100644
--- a/src/backend/executor/execPartition.c
+++ b/src/backend/executor/execPartition.c
@@ -546,6 +546,18 @@ ExecInitPartitionInfo(ModifyTableState *mtstate, EState *estate,
 						(node != NULL &&
 						 node->onConflictAction != ONCONFLICT_NONE));
 
+	if (node != NULL &&
+		(list_length(node->withCheckOptionLists) > 0 ||
+		 list_length(node->returningLists) > 0 ||
+		 node->onConflictAction != ONCONFLICT_NONE ||
+		 node->operation == CMD_MERGE))
+	{
+		part_attmap =
+			build_attrmap_by_name(RelationGetDescr(partrel),
+								  RelationGetDescr(firstResultRel),
+								  false);
+	}
+
 	/*
 	 * Build WITH CHECK OPTION constraints for the partition.  Note that we
 	 * didn't build the withCheckOptionList for partitions within the planner,
@@ -588,10 +600,6 @@ ExecInitPartitionInfo(ModifyTableState *mtstate, EState *estate,
 		/*
 		 * Convert Vars in it to contain this partition's attribute numbers.
 		 */
-		part_attmap =
-			build_attrmap_by_name(RelationGetDescr(partrel),
-								  RelationGetDescr(firstResultRel),
-								  false);
 		wcoList = (List *)
 			map_variable_attnos((Node *) wcoList,
 								firstVarno, 0,
@@ -645,14 +653,6 @@ ExecInitPartitionInfo(ModifyTableState *mtstate, EState *estate,
 		 */
 		returningList = linitial(node->returningLists);
 
-		/*
-		 * Convert Vars in it to contain this partition's attribute numbers.
-		 */
-		if (part_attmap == NULL)
-			part_attmap =
-				build_attrmap_by_name(RelationGetDescr(partrel),
-									  RelationGetDescr(firstResultRel),
-									  false);
 		returningList = (List *)
 			map_variable_attnos((Node *) returningList,
 								firstVarno, 0,
@@ -791,11 +791,7 @@ ExecInitPartitionInfo(ModifyTableState *mtstate, EState *estate,
 				 * target relation (firstVarno).
 				 */
 				onconflset = copyObject(node->onConflictSet);
-				if (part_attmap == NULL)
-					part_attmap =
-						build_attrmap_by_name(RelationGetDescr(partrel),
-											  RelationGetDescr(firstResultRel),
-											  false);
+
 				onconflset = (List *)
 					map_variable_attnos((Node *) onconflset,
 										INNER_VAR, 0,
@@ -891,12 +887,6 @@ ExecInitPartitionInfo(ModifyTableState *mtstate, EState *estate,
 		ExprContext *econtext = mtstate->ps.ps_ExprContext;
 		Node	   *joinCondition;
 
-		if (part_attmap == NULL)
-			part_attmap =
-				build_attrmap_by_name(RelationGetDescr(partrel),
-									  RelationGetDescr(firstResultRel),
-									  false);
-
 		if (unlikely(!leaf_part_rri->ri_projectNewInfoValid))
 			ExecInitMergeTupleSlots(mtstate, leaf_part_rri);
 
-- 
2.34.1

Reply via email to