pgsql: Fix concurrent delete handling in MERGE ... WHEN NOT MATCHED BY

From: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix concurrent delete handling in MERGE ... WHEN NOT MATCHED BY
Date: 2026-09-23 08:41:40
Message-ID: E1x9IXs-00000000tF2-3xqQ@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix concurrent delete handling in MERGE ... WHEN NOT MATCHED BY SOURCE.

When executing a WHEN NOT MATCHED BY SOURCE action in MERGE, if the
tuple from the target table was concurrently deleted, the retry code
would incorrectly execute a WHEN NOT MATCHED BY TARGET action for the
row instead, giving incorrect results.

Fix in ExecMergeMatched() -- when handling a TM_Deleted failure
status, it should only set *matched to false (telling the caller to
execute any WHEN NOT MATCHED BY TARGET actions) if the failed action
was a WHEN MATCHED action. For a failed WHEN NOT MATCHED BY SOURCE
action, the source tuple did not exist, and the target tuple has just
been deleted, so it should do nothing.

Back-patch to v17, where support for WHEN NOT MATCHED BY SOURCE
actions was introduced.

Reported-by: Jeff Davis <pgsql(at)j-davis(dot)com>
Author: Tender Wang <tndrwang(at)gmail(dot)com>
Reviewed-by: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
Discussion: https://postgr.es/m/ccdab5ba02c65af195b5a6d2d744a01d9de47cd3.camel@j-davis.com
Backpatch-through: 17

Branch
------
REL_17_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/745152e451c10ff8635b0f0f186de98fe0528fec

Modified Files
--------------
src/backend/executor/nodeModifyTable.c | 35 ++++--
src/test/isolation/expected/merge-delete.out | 158 +++++++++++++++++++++++++++
src/test/isolation/specs/merge-delete.spec | 37 +++++++
3 files changed, 220 insertions(+), 10 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message John Naylor 2026-09-23 10:36:02 pgsql: Clean up usage of hash_any() and hash_any_extended()
Previous Message Peter Eisentraut 2026-09-23 07:32:22 pgsql: pgindent: Fix indentation of alignas() in struct members