wrong results: merge when not matched by source
AI-discovered bug report appended to this email.
Regards,
Jeff Davis
-- MERGE WHEN NOT MATCHED BY SOURCE + concurrent DELETE inserts a
-- null-source row. Two sessions, READ COMMITTED. Affects 17+.
--
-- Setup (either session):
DROP TABLE IF EXISTS target;
CREATE TABLE target (key int, val text);
INSERT INTO target VALUES (1, 'matched'), (2, 'nms');
-- Session 1:
BEGIN ISOLATION LEVEL READ COMMITTED;
DELETE FROM target WHERE key = 2; -- holds the NMS row
-- Session 2 (blocks on the NMS row):
BEGIN ISOLATION LEVEL READ COMMITTED;
MERGE INTO target t
USING (SELECT 1 AS key, 'src' AS val) s
ON t.key = s.key
WHEN MATCHED THEN UPDATE SET val = s.val
WHEN NOT MATCHED BY SOURCE THEN UPDATE SET val = 'nms-action'
WHEN NOT MATCHED THEN INSERT VALUES (s.key, s.val)
RETURNING merge_action(), t.*;
-- Session 1:
COMMIT; -- unblocks session 2
-- Session 2 then returns:
--
-- merge_action | key | val
-- --------------+-----+-----
-- INSERT | |
-- UPDATE | 1 | src
--
-- Expected: only UPDATE of key=1; table is {(1, src)}.
-- Actual: also INSERT of (NULL, NULL).
Jeff Davis <pgsql@j-davis.com> 于2026年9月18日周五 03:18写道:
-- Expected: only UPDATE of key=1; table is {(1, src)}.
-- Actual: also INSERT of (NULL, NULL).
I think the problem is that the TM_Deleted case in
ExecMergeMatched() unconditionally sets *matched to false.
This is correct when the candidate row was originally a MATCHED row:
the source row still exists after the target row is concurrently
deleted, so it should be processed by the NOT MATCHED BY TARGET
actions.
However, it is not correct when processing an original NOT MATCHED BY
SOURCE row. In that case, the source row was already absent, and after
the concurrent deletion of the target row, there is no candidate row
left to process. Setting *matched to false causes ExecMerge() to
call ExecMergeNotMatched(), which executes the NOT MATCHED BY TARGET
INSERT using the null source side of the original join row.
A tentative fix is to set *matched to false only if the current
action is a MATCHED action, as in the attached patch.
There is another TM_Deleted case below, reached while handling
TM_Updated and following the update chain:
...
case TM_Deleted:
*matched = false;
goto out;
...
I think that case may need similar treatment. The existing
was_matched variable could perhaps be used there:
...
if (was_matched)
*matched = false;
...
I have left that case unchanged for now because I would like to
confirm whether the same state distinction applies to all paths
through that code.
The attached patch is intended to show the proposed fix and does not
yet include a regression test.
--
Thanks,
Tender Wang
On Fri, 18 Sept 2026 at 03:02, Tender Wang <tndrwang@gmail.com> wrote:
Jeff Davis <pgsql@j-davis.com> 于2026年9月18日周五 03:18写道:
-- Expected: only UPDATE of key=1; table is {(1, src)}.
-- Actual: also INSERT of (NULL, NULL).I think the problem is that the TM_Deleted case in
ExecMergeMatched() unconditionally sets *matched to false.This is correct when the candidate row was originally a MATCHED row:
the source row still exists after the target row is concurrently
deleted, so it should be processed by the NOT MATCHED BY TARGET
actions.However, it is not correct when processing an original NOT MATCHED BY
SOURCE row. In that case, the source row was already absent, and after
the concurrent deletion of the target row, there is no candidate row
left to process. Setting *matched to false causes ExecMerge() to
call ExecMergeNotMatched(), which executes the NOT MATCHED BY TARGET
INSERT using the null source side of the original join row.
Yes, that analysis is correct. If there was no source row, and the
target row is concurrently deleted, then there's nothing left to do.
It shouldn't execute a NOT MATCHED BY TARGET action if there was never
a source row to begin with.
A tentative fix is to set *matched to false only if the current
action is a MATCHED action, as in the attached patch.
That fix looks good to me. Nice and simple.
There is another TM_Deleted case below, reached while handling
TM_Updated and following the update chain:
...
case TM_Deleted:
*matched = false;
goto out;
...I think that case may need similar treatment. The existing
was_matched variable could perhaps be used there:
...
if (was_matched)
*matched = false;
Yes, that's basically the same. It can be triggered by a slight
modification to the original test case, making Session 1 do an UPDATE
before the DELETE:
-- Session 1:
BEGIN ISOLATION LEVEL READ COMMITTED;
UPDATE target SET val = 'nms-updated' WHERE key = 2; -- holds the NMS row
DELETE FROM target WHERE key = 2;
The attached patch is intended to show the proposed fix and does not
yet include a regression test.
Attached is an update with the fix for the second case, and tests for both.
Regards,
Dean
Attachments:
v2-fix-concurrent-delete-issue-with-merge-when-not-matched-by-source.patchtext/x-patch; charset=US-ASCII; name=v2-fix-concurrent-delete-issue-with-merge-when-not-matched-by-source.patchDownload+226-6
On Fri, 18 Sept 2026, 12:03 Dean Rasheed, <dean.a.rasheed@gmail.com> wrote:
That fix looks good to me. Nice and simple.
Actually, the last part of the new comment is wrong/unnecessary. If it
changes matched from true to false, retrying, it will have locked the
target tuple, and it can't be concurrently deleted when it retries. It
never retries more than once. So the comment can be simplified a bit.
Regards,
Dean
Show quoted text
Dean Rasheed <dean.a.rasheed@gmail.com> 于2026年9月18日周五 20:22写道:
On Fri, 18 Sept 2026, 12:03 Dean Rasheed, <dean.a.rasheed@gmail.com> wrote:
That fix looks good to me. Nice and simple.
Actually, the last part of the new comment is wrong/unnecessary. If it changes matched from true to false, retrying, it will have locked the target tuple, and it can't be concurrently deleted when it retries. It never retries more than once. So the comment can be simplified a bit.
Thanks for confirming.
I also noticed the following part of the comment at the beginning of
`ExecMerge()`:
...
If the target tuple gets deleted or a concurrent update causes the join
quals to fail, it returns a matched status of false and we call
ExecMergeNotMatched().
...
With this change, perhaps this part of the comment should also be
adjusted to distinguish an original `WHEN MATCHED` case from an
original `WHEN NOT MATCHED BY SOURCE` case.
For the former, a concurrent delete leaves the source tuple to be
processed by `WHEN NOT MATCHED [BY TARGET]` actions, whereas for the
latter, neither a source tuple nor a target tuple remains, so there is
no further action to execute.
Do you think it's worth updating this comment as part of the patch?
--
Thanks,
Tender Wang
On Fri, 18 Sept 2026, 13:52 Tender Wang, <tndrwang@gmail.com> wrote:
Dean Rasheed <dean.a.rasheed@gmail.com> 于2026年9月18日周五 20:22写道:
On Fri, 18 Sept 2026, 12:03 Dean Rasheed, <dean.a.rasheed@gmail.com>
wrote:
That fix looks good to me. Nice and simple.
Actually, the last part of the new comment is wrong/unnecessary. If it
changes matched from true to false, retrying, it will have locked the
target tuple, and it can't be concurrently deleted when it retries. It
never retries more than once. So the comment can be simplified a bit.Thanks for confirming.
I also noticed the following part of the comment at the beginning of
`ExecMerge()`:...
If the target tuple gets deleted or a concurrent update causes the join
quals to fail, it returns a matched status of false and we call
ExecMergeNotMatched().
...With this change, perhaps this part of the comment should also be
adjusted to distinguish an original `WHEN MATCHED` case from an
original `WHEN NOT MATCHED BY SOURCE` case.For the former, a concurrent delete leaves the source tuple to be
processed by `WHEN NOT MATCHED [BY TARGET]` actions, whereas for the
latter, neither a source tuple nor a target tuple remains, so there is
no further action to execute.Do you think it's worth updating this comment as part of the patch?
Sure. I'm AFK at the moment, so if you want to have a crack at that, I'll
take a look when I get back.
Regards,
Dean
Show quoted text
On Fri, 18 Sept 2026 at 13:52, Tender Wang <tndrwang@gmail.com> wrote:
I also noticed the following part of the comment at the beginning of
`ExecMerge()`:If the target tuple gets deleted or a concurrent update causes the join
quals to fail, it returns a matched status of false and we call
ExecMergeNotMatched().With this change, perhaps this part of the comment should also be
adjusted to distinguish an original `WHEN MATCHED` case from an
original `WHEN NOT MATCHED BY SOURCE` case.Do you think it's worth updating this comment as part of the patch?
OK, here is an update doing that.
Since a new v19 beta is about to be released, I'll hold off on pushing
this until later next week.
Regards,
Dean
Dean Rasheed <dean.a.rasheed@gmail.com> 于2026年9月19日周六 16:43写道:
OK, here is an update doing that.
Since a new v19 beta is about to be released, I'll hold off on pushing
this until later next week.
Thanks for working on this.
By the way, since you are looking at MERGE, I wonder if you might also
have a chance to look at the other issue reported earlier involving a
prepared MERGE in [1]/messages/by-id/19689-0a1b106035cd8a5b@postgresql.org.
I am not even sure whether the reported behavior should be considered a bug.
I proposed a tentative fix in that thread, but I would appreciate your
thoughts on both the expected behavior and the proposed approach.
[1]: /messages/by-id/19689-0a1b106035cd8a5b@postgresql.org
--
Thanks,
Tender Wang
On Sun, 20 Sept 2026 at 02:41, Tender Wang <tndrwang@gmail.com> wrote:
Dean Rasheed <dean.a.rasheed@gmail.com> 于2026年9月19日周六 16:43写道:
OK, here is an update doing that.
Since a new v19 beta is about to be released, I'll hold off on pushing
this until later next week.
Pushed and back-patched to v17.
By the way, since you are looking at MERGE, I wonder if you might also
have a chance to look at the other issue reported earlier involving a
prepared MERGE in [1].
I am not even sure whether the reported behavior should be considered a bug.
I proposed a tentative fix in that thread, but I would appreciate your
thoughts on both the expected behavior and the proposed approach.
I'll try and have a look.
Regards,
Dean