pgsql: Fix stale source values in MERGE after a concurrent update.

From: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix stale source values in MERGE after a concurrent update.
Date: 2026-11-21 12:17:58
Message-ID: E1xUk2X-00000000dKR-3e2g@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix stale source values in MERGE after a concurrent update.

When a MERGE has WHEN NOT MATCHED BY SOURCE actions and an initially
matched target row is concurrently updated so that it no longer
satisfies the join condition, causing a WHEN NOT MATCHED BY SOURCE
action to be executed, the RETURNING list was incorrectly evaluated
using the original non-NULL source values. All source values returned
by a WHEN NOT MATCHED BY SOURCE action should be NULL.

Fix in ExecMergeMatched(), when we transition from MATCHED to NOT
MATCHED BY SOURCE after a concurrent update, by building a new
all-NULL slot for use in the RETURNING projection.

Backpatch to v17, where WHEN NOT MATCHED BY SOURCE support was added
to MERGE.

Bug: #19912
Reported-by: Chunling Qin <303677365(at)qq(dot)com>
Author: Tender Wang <tndrwang(at)gmail(dot)com>
Author: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
Discussion: https://postgr.es/m/19912-ceea4a1c0a7dd41c%40postgresql.org
Backpatch-through: 17

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/b291954ea5a4855ded56c334859d7de9fea372eb

Modified Files
--------------
src/backend/executor/nodeModifyTable.c | 37 ++++++-
src/test/isolation/expected/merge-not-matched.out | 113 ++++++++++++++++++++++
src/test/isolation/isolation_schedule | 1 +
src/test/isolation/specs/merge-not-matched.spec | 49 ++++++++++
4 files changed, 197 insertions(+), 3 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Fujii Masao 2026-11-21 14:19:09 pgsql: Fix libpq crashes on malformed target_session_attrs query result
Previous Message Álvaro Herrera 2026-11-21 11:31:25 Re: pgsql: Don't trust NOT NULL on virtual generated columns of partitioned