Hi, I found a follow-up case for #19912. When a concurrent update causes MERGE to switch from MATCHED to NOT MATCHED BY SOURCE, ExecMergeMatched() builds an all-NULL plan slot for the action. If that action moves the target row to another partition, ExecInsert() evaluates RETURNING using context->planSlot, which still contains the source values from the original match.
How to reproduce: CREATE TABLE tp (k int, id int) PARTITION BY RANGE (id); CREATE TABLE tp1 PARTITION OF tp FOR VALUES FROM (1) TO (10); CREATE TABLE tp2 PARTITION OF tp FOR VALUES FROM (10) TO (20); CREATE TABLE src (sid int); INSERT INTO tp VALUES (5, 5); INSERT INTO src VALUES (5); session 1 BEGIN; UPDATE tp SET k = 99 WHERE id = 5; session 2 MERGE INTO tp t USING src s ON t.k = s.sid WHEN MATCHED THEN UPDATE SET k = t.k WHEN NOT MATCHED BY SOURCE THEN UPDATE SET id = 15 RETURNING merge_action(), s.sid, t.k, t.id; session1 commit; The attached patch adds an optional returningSlot to ModifyTableContext. ExecMergeMatched() sets it to the current action’s plan slot before calling ExecUpdateAct() and clears it when the call returns. This lets both update paths use the same slot for RETURNING and preserves the original plan slot for any subsequent WHEN NOT MATCHED BY TARGET action. ExecModifyTable() initializes the override to NULL, so ExecInsert() uses context->planSlot whenever no override is set. CC’ing Dean since this follows up on the fix for #19912. -- Tender Wang
From b4bc17928ce7ef460a012ef882aa54d7e8c8df87 Mon Sep 17 00:00:00 2001 From: Tender Wang <[email protected]> Date: Sun, 22 Nov 2026 15:16:52 +0800 Subject: [PATCH] Fix stale MERGE RETURNING source values after a cross-partition update When a concurrent update causes a MERGE action to switch from MATCHED to NOT MATCHED BY SOURCE, ExecMergeMatched() uses an all-NULL plan slot for the action. If the action moves the row to another partition, ExecInsert() projects RETURNING using the original plan slot, exposing source values from the former match. Pass the action's plan slot through ModifyTableContext for the RETURNING projection. Preserve the original plan slot for any subsequent NOT MATCHED BY TARGET action. --- src/backend/executor/nodeModifyTable.c | 14 +++++++++- .../expected/merge-match-recheck.out | 27 +++++++++++++++++++ .../isolation/specs/merge-match-recheck.spec | 16 +++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c index b000650abe5..8889a835d19 100644 --- a/src/backend/executor/nodeModifyTable.c +++ b/src/backend/executor/nodeModifyTable.c @@ -119,6 +119,12 @@ typedef struct ModifyTableContext * cross-partition UPDATE */ TupleTableSlot *cpUpdateReturningSlot; + + /* + * Alternate subplan slot for RETURNING during a MERGE update. + * NULL means to use planSlot. + */ + TupleTableSlot *returningSlot; } ModifyTableContext; /* @@ -1328,6 +1334,7 @@ ExecInsert(ModifyTableContext *context, if (resultRelInfo->ri_projectReturning) { TupleTableSlot *oldSlot = NULL; + TupleTableSlot *returningSlot = context->returningSlot; /* * If this is part of a cross-partition UPDATE, and the RETURNING list @@ -1360,7 +1367,8 @@ ExecInsert(ModifyTableContext *context, } result = ExecProcessReturning(context, resultRelInfo, false, - oldSlot, slot, planSlot); + oldSlot, slot, + returningSlot ? returningSlot : planSlot); /* * For a cross-partition UPDATE, release the old tuple, first making @@ -3458,9 +3466,12 @@ lmerge_matched: /* checked ri_needLockTagTuple above */ Assert(oldtuple == NULL); + /* Use the action's source values for RETURNING. */ + context->returningSlot = planSlot; result = ExecUpdateAct(context, resultRelInfo, tupleid, NULL, newslot, canSetTag, &updateCxt); + context->returningSlot = NULL; /* * As in ExecUpdate(), if ExecUpdateAct() reports that a @@ -4506,6 +4517,7 @@ ExecModifyTable(PlanState *pstate) context.mtstate = node; context.epqstate = &node->mt_epqstate; context.estate = estate; + context.returningSlot = NULL; /* * Fetch rows from subplan, and execute the required table modification diff --git a/src/test/isolation/expected/merge-match-recheck.out b/src/test/isolation/expected/merge-match-recheck.out index 10ef2ad2fcd..4d25cb887a4 100644 --- a/src/test/isolation/expected/merge-match-recheck.out +++ b/src/test/isolation/expected/merge-match-recheck.out @@ -650,3 +650,30 @@ key|balance|status|val (1 row) step c1: COMMIT; + +starting permutation: break_match_pa merge_nmbs_move_pa c2 select_pa2 c1 +step break_match_pa: UPDATE target_pa SET key = 2 WHERE key = 1; +step merge_nmbs_move_pa: + MERGE INTO target_pa t + USING target s + ON s.key = t.key + WHEN MATCHED THEN + UPDATE SET val = s.val + WHEN NOT MATCHED BY SOURCE THEN + UPDATE SET balance = 210 + RETURNING s.key AS source_key, t.balance; + <waiting ...> +step c2: COMMIT; +step merge_nmbs_move_pa: <... completed> +source_key|balance +----------+------- + | 210 +(1 row) + +step select_pa2: SELECT balance FROM ONLY target_pa2; +balance +------- + 210 +(1 row) + +step c1: COMMIT; diff --git a/src/test/isolation/specs/merge-match-recheck.spec b/src/test/isolation/specs/merge-match-recheck.spec index 6054fcade26..62a8ec0ccae 100644 --- a/src/test/isolation/specs/merge-match-recheck.spec +++ b/src/test/isolation/specs/merge-match-recheck.spec @@ -103,6 +103,17 @@ step "merge_bal_pa" UPDATE SET balance = balance * 8, val = t.val || ' when3' RETURNING t.key, old.balance AS old_balance, new.balance AS new_balance, t.status, t.val; } +step "merge_nmbs_move_pa" +{ + MERGE INTO target_pa t + USING target s + ON s.key = t.key + WHEN MATCHED THEN + UPDATE SET val = s.val + WHEN NOT MATCHED BY SOURCE THEN + UPDATE SET balance = 210 + RETURNING s.key AS source_key, t.balance; +} step "merge_bal_tg" { WITH t AS ( @@ -145,6 +156,7 @@ step "merge_delete_tg" step "select1" { SELECT * FROM target; } step "select1_pa" { SELECT * FROM target_pa; } +step "select_pa2" { SELECT balance FROM ONLY target_pa2; } step "select1_tg" { SELECT * FROM target_tg; } step "c1" { COMMIT; } @@ -170,6 +182,7 @@ step "update7" { UPDATE target t SET balance = 350, val = t.val || ' updated by step "update_bal1" { UPDATE target t SET balance = 50, val = t.val || ' updated by update_bal1' WHERE t.key = 1; } step "update_bal1_pa" { UPDATE target_pa t SET balance = 50, val = t.val || ' updated by update_bal1_pa' WHERE t.key = 1; } step "update_bal1_tg" { UPDATE target_tg t SET balance = 50, val = t.val || ' updated by update_bal1_tg' WHERE t.key = 1; } +step "break_match_pa" { UPDATE target_pa SET key = 2 WHERE key = 1; } step "c2" { COMMIT; } # merge_status sees concurrently updated row and rechecks WHEN conditions, but recheck passes and final status = 's2' @@ -217,3 +230,6 @@ permutation "update1_tg" "merge_delete_tg" "c2" "select1_tg" "c1" # merge_delete sees concurrently updated row and rechecks WHEN conditions, recheck fails, so final balance is 100 permutation "update_bal1" "merge_delete" "c2" "select1" "c1" permutation "update_bal1_tg" "merge_delete_tg" "c2" "select1_tg" "c1" + +# A concurrent update breaks the match, then the NMBS action moves the row. +permutation "break_match_pa" "merge_nmbs_move_pa" "c2" "select_pa2" "c1" -- 2.43.0
