From: Jeff Davis <pgsql@j-davis.com>
To: pgsql-bugs@postgresql.org
Cc: Dean Rasheed <dean.a.rasheed@gmail.com>
Subject: wrong results: merge when not matched by source
Date: Thu, 17 Sep 2026 12:17:56 -0700
Message-ID: <ccdab5ba02c65af195b5a6d2d744a01d9de47cd3.camel@j-davis.com> (raw)
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).
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-bugs@postgresql.org
Cc: pgsql@j-davis.com, dean.a.rasheed@gmail.com
Subject: Re: wrong results: merge when not matched by source
In-Reply-To: <ccdab5ba02c65af195b5a6d2d744a01d9de47cd3.camel@j-davis.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox