diff --git a/src/backend/executor/execPartition.c b/src/backend/executor/execPartition.c index e30db12113b4..4cc338e63743 100644 --- a/src/backend/executor/execPartition.c +++ b/src/backend/executor/execPartition.c @@ -606,6 +606,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, @@ -648,10 +660,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, @@ -705,14 +713,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, @@ -923,11 +923,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, @@ -1023,12 +1019,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);