pgsql: Fix Assert failure in EXPLAIN ANALYZE MERGE with 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 Assert failure in EXPLAIN ANALYZE MERGE with a concurrent up
Date: 2025-11-16 22:17:53
Message-ID: E1vKl4D-006wtA-29@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix Assert failure in EXPLAIN ANALYZE MERGE with a concurrent update.

When instrumenting a MERGE command containing both WHEN NOT MATCHED BY
SOURCE and WHEN NOT MATCHED BY TARGET actions using EXPLAIN ANALYZE, a
concurrent update of the target relation could lead to an Assert
failure in show_modifytable_info(). In a non-assert build, this would
lead to an incorrect value for "skipped" tuples in the EXPLAIN output,
rather than a crash.

This could happen if the concurrent update caused a matched row to no
longer match, in which case ExecMerge() treats the single originally
matched row as a pair of not matched rows, and potentially executes 2
not-matched actions for the single source row. This could then lead to
a state where the number of rows processed by the ModifyTable node
exceeds the number of rows produced by its source node, causing
"skipped_path" in show_modifytable_info() to be negative, triggering
the Assert.

Fix this in ExecMergeMatched() by incrementing the instrumentation
tuple count on the source node whenever a concurrent update of this
kind is detected, if both kinds of merge actions exist, so that the
number of source rows matches the number of actions potentially
executed, and the "skipped" tuple count is correct.

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

Bug: #19111
Reported-by: Dilip Kumar <dilipbalaut(at)gmail(dot)com>
Author: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
Reviewed-by: Dilip Kumar <dilipbalaut(at)gmail(dot)com>
Discussion: https://postgr.es/m/19111-5b06624513d301b3@postgresql.org
Backpatch-through: 17

Branch
------
REL_17_STABLE

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

Modified Files
--------------
src/backend/executor/nodeModifyTable.c | 21 ++++
src/test/isolation/expected/merge-update.out | 146 +++++++++++++++++++++++++++
src/test/isolation/specs/merge-update.spec | 50 +++++++++
3 files changed, 217 insertions(+)

Browse pgsql-committers by date

  From Date Subject
Next Message Michael Paquier 2025-11-16 23:11:24 pgsql: Add test for temporary file removal and WITH HOLD cursor
Previous Message David Rowley 2025-11-16 21:53:33 pgsql: Doc: include MERGE in variable substitution command list