From 15d99d372f3277bce12811abdaeb1bcffd5bdf41 Mon Sep 17 00:00:00 2001 From: Tender Wang Date: Wed, 18 Nov 2026 09:37:52 +0800 Subject: [PATCH v2] Use EPQ tuple for MERGE NOT MATCHED BY SOURCE actions When a concurrently updated target tuple no longer satisfies the MERGE join condition, EvalPlanQual returns a tuple with the source side correctly set to null, and ExecMergeMatched switches from MATCHED to NOT MATCHED BY SOURCE actions. However, the action quals and projections continued to use the original plan slot, which still contained values from the previously matched source tuple. RETURNING expressions also used that slot. As a result, NOT MATCHED BY SOURCE actions could select the wrong action, store stale source values, or return them to the user. Keep a separate plan slot for the action currently being executed. When EPQ changes a MATCHED row to NOT MATCHED BY SOURCE, use the EPQ slot for the action quals, projection, and RETURNING. Leave context->planSlot unchanged, since a subsequent NOT MATCHED BY TARGET action still needs the original source tuple. --- src/backend/executor/nodeModifyTable.c | 35 +++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c index 966094b6a32..8ac566dfdff 100644 --- a/src/backend/executor/nodeModifyTable.c +++ b/src/backend/executor/nodeModifyTable.c @@ -3312,6 +3312,8 @@ ExecMergeMatched(ModifyTableContext *context, ResultRelInfo *resultRelInfo, List *actionStates; TupleTableSlot *newslot = NULL; TupleTableSlot *rslot = NULL; + TupleTableSlot *actionPlanSlot = context->planSlot; + TupleTableSlot *nullSourceSlot = NULL; EState *estate = context->estate; ExprContext *econtext = mtstate->ps.ps_ExprContext; bool isNull; @@ -3336,7 +3338,7 @@ ExecMergeMatched(ModifyTableContext *context, ResultRelInfo *resultRelInfo, * or NOT MATCHED BY SOURCE tuple and UPDATE/DELETE actions. */ econtext->ecxt_scantuple = resultRelInfo->ri_oldTupleSlot; - econtext->ecxt_innertuple = context->planSlot; + econtext->ecxt_innertuple = actionPlanSlot; econtext->ecxt_outertuple = NULL; /* @@ -3718,6 +3720,25 @@ lmerge_matched: /* Switch lists, if necessary */ if (!*matched) { + /* + * The EPQ subplan may be a full join, in which case + * its first output tuple might be the NOT MATCHED + * BY TARGET tuple, with non-NULL source values. + * Therefore, don't use epqslot for the NOT MATCHED + * BY SOURCE action. Instead, use an explicitly + * null-extended subplan tuple, while leaving + * context->planSlot unchanged for any + * subsequent NOT MATCHED BY TARGET action. + */ + if (nullSourceSlot == NULL) + { + nullSourceSlot = + MakeSingleTupleTableSlot(context->planSlot->tts_tupleDescriptor, + &TTSOpsVirtual); + ExecStoreAllNullTuple(nullSourceSlot); + } + actionPlanSlot = nullSourceSlot; + econtext->ecxt_innertuple = actionPlanSlot; actionStates = mergeActions[MERGE_WHEN_NOT_MATCHED_BY_SOURCE]; /* @@ -3819,7 +3840,7 @@ lmerge_matched: false, resultRelInfo->ri_oldTupleSlot, newslot, - context->planSlot); + actionPlanSlot); break; case CMD_DELETE: @@ -3828,7 +3849,7 @@ lmerge_matched: true, resultRelInfo->ri_oldTupleSlot, NULL, - context->planSlot); + actionPlanSlot); break; case CMD_NOTHING: @@ -3851,6 +3872,14 @@ lmerge_matched: * Successfully executed an action or no qualifying action was found. */ out: + if (nullSourceSlot != NULL) + { + econtext->ecxt_innertuple = NULL; + if (resultRelInfo->ri_projectReturning != NULL) + resultRelInfo->ri_projectReturning->pi_exprContext->ecxt_outertuple = NULL; + ExecDropSingleTupleTableSlot(nullSourceSlot); + } + if (ItemPointerIsValid(&lockedtid)) UnlockTuple(resultRelInfo->ri_RelationDesc, &lockedtid, InplaceUpdateTupleLock); -- 2.43.0