pgsql: Fix cross-partition source values in MERGE after a concurrent up

From: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix cross-partition source values in MERGE after a concurrent up
Date: 2026-11-23 12:21:01
Message-ID: E1xVT2b-000000001Fb-1mPq@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix cross-partition source values in MERGE after a concurrent update.

Follow-up to commit b291954ea5a. When a concurrent update causes MERGE
to switch from a WHEN MATCHED action to a WHEN NOT MATCHED BY SOURCE
action, any source values returned should be NULL, not the source
values from the originally matched tuple.

Commit b291954ea5a attempted to ensure this by building a new all-NULL
slot for use in the RETURNING projection, when ExecMergeMatched()
transitions from WHEN MATCHED to WHEN NOT MATCHED BY SOURCE after a
concurrent update. However, this overlooked the code path taken for a
cross-partition update, which executes ExecProcessReturning() from
ExecInsert() rather than directly from ExecMergeMatched().

Fix by temporarily replacing the ModifyTableContext planSlot with the
new all-NULL slot while the WHEN NOT MATCHED BY SOURCE action is
executed, so that the correct NULL source values are seen by all code
executed as part of the action.

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

Reported-by: Tender Wang <tndrwang(at)gmail(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/CAHewXNkZb5rBnen7ALsXX3d5JgdpzUjsNY9pqbxub3N-%2BNb%3DyQ%40mail.gmail.com
Backpatch-through: 17

Branch
------
REL_19_STABLE

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

Modified Files
--------------
src/backend/executor/nodeModifyTable.c | 29 +++---
src/test/isolation/expected/merge-not-matched.out | 112 ++++++++++++++++++++++
src/test/isolation/specs/merge-not-matched.spec | 38 +++++++-
3 files changed, 163 insertions(+), 16 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Nathan Bossart 2026-11-23 18:32:36 pgsql: pg_restore: Fix incompatibilities with extracted tar dumps.
Previous Message Álvaro Herrera 2026-11-23 11:49:47 pgsql: Adjust message style