Fix stale source values in MERGE RETURNING after a cross-partition update
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
On Sun, 22 Nov 2026 at 07:46, Tender Wang <tndrwang@gmail.com> wrote:
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.
Ugh yes, that's annoying. Well spotted.
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.
Hmm, I don't like that solution because it's somewhat delocalised,
which I think makes it fragile if new code is added using
context->planSlot, instead of your new returningSlot.
I think a better (more localised) solution is to have
ExecMergeMatched() swap out context->planSlot while it executes the
NOT MATCHED BY SOURCE action. That feels more robust, because it
removes any chance of any code called while executing the action
seeing the old stale source values. Something like v2, attached.
Also, I don't think merge-match-recheck is the right place to test
this, since it's not about rechecking AND conditions. I've added new
tests to merge-not-matched, based on the ones added before for bug
#19912, testing both left and full joins.
(I didn't remove your tests from v2 yet, because I wanted to confirm
that they also passed with the new code.)
Regards,
Dean
Attachments:
v2-Fix-stale-MERGE-RETURNING-source-values-after-a-cros.patchtext/x-patch; charset=US-ASCII; name=v2-Fix-stale-MERGE-RETURNING-source-values-after-a-cros.patchDownload+206-16
Dean Rasheed <dean.a.rasheed@gmail.com> 于2026年11月22日周日 19:32写道:
Hmm, I don't like that solution because it's somewhat delocalised,
which I think makes it fragile if new code is added using
context->planSlot, instead of your new returningSlot.I think a better (more localised) solution is to have
ExecMergeMatched() swap out context->planSlot while it executes the
NOT MATCHED BY SOURCE action. That feels more robust, because it
removes any chance of any code called while executing the action
seeing the old stale source values. Something like v2, attached.
Yeah, I agree that temporarily replacing context->planSlot in
ExecMergeMatched() is cleaner.
It gives the entire NOT MATCHED BY SOURCE action a consistent view of
the source row, including the cross-partition RETURNING path.
Restoring the original slot before returning also makes it available
to any subsequent NOT MATCHED BY TARGET action.
Also, I don't think merge-match-recheck is the right place to test
this, since it's not about rechecking AND conditions. I've added new
tests to merge-not-matched, based on the ones added before for bug
#19912, testing both left and full joins.
+1
--
Tender Wang
On Mon, 23 Nov 2026 at 09:11, Tender Wang <tndrwang@gmail.com> wrote:
Dean Rasheed <dean.a.rasheed@gmail.com> 于2026年11月22日周日 19:32写道:
I think a better (more localised) solution is to have
ExecMergeMatched() swap out context->planSlot while it executes the
NOT MATCHED BY SOURCE action. That feels more robust, because it
removes any chance of any code called while executing the action
seeing the old stale source values. Something like v2, attached.Yeah, I agree that temporarily replacing context->planSlot in
ExecMergeMatched() is cleaner.
It gives the entire NOT MATCHED BY SOURCE action a consistent view of
the source row, including the cross-partition RETURNING path.
Restoring the original slot before returning also makes it available
to any subsequent NOT MATCHED BY TARGET action.Also, I don't think merge-match-recheck is the right place to test
this, since it's not about rechecking AND conditions. I've added new
tests to merge-not-matched, based on the ones added before for bug
#19912, testing both left and full joins.+1
Pushed and back-patched to v17.
Regards,
Dean