diff options
| author | Dean Rasheed <dean.a.rasheed@gmail.com> | 2025-11-16 22:14:06 +0000 |
|---|---|---|
| committer | Dean Rasheed <dean.a.rasheed@gmail.com> | 2025-11-16 22:14:06 +0000 |
| commit | 1b92fe7bb9d8b8977bf7e48912b12f97f128ec1d (patch) | |
| tree | 85181375a3f6aac6cf94129fceac8cd6a7ede840 /src/backend/executor/nodeModifyTable.c | |
| parent | 2b54a1abdbb09a4b170bca82726dabe7b2d6555b (diff) | |
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@gmail.com>
Author: Dean Rasheed <dean.a.rasheed@gmail.com>
Reviewed-by: Dilip Kumar <dilipbalaut@gmail.com>
Discussion: https://postgr.es/m/19111-5b06624513d301b3@postgresql.org
Backpatch-through: 17
Diffstat (limited to 'src/backend/executor/nodeModifyTable.c')
| -rw-r--r-- | src/backend/executor/nodeModifyTable.c | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c index 4c5647ac38a..00429326c34 100644 --- a/src/backend/executor/nodeModifyTable.c +++ b/src/backend/executor/nodeModifyTable.c @@ -3467,7 +3467,28 @@ lmerge_matched: /* Switch lists, if necessary */ if (!*matched) + { actionStates = mergeActions[MERGE_WHEN_NOT_MATCHED_BY_SOURCE]; + + /* + * If we have both NOT MATCHED BY SOURCE + * and NOT MATCHED BY TARGET actions (a + * full join between the source and target + * relations), the single previously + * matched tuple from the outer plan node + * is treated as two not matched tuples, + * in the same way as if they had not + * matched to start with. Therefore, we + * must adjust the outer plan node's tuple + * count, if we're instrumenting the + * query, to get the correct "skipped" row + * count --- see show_modifytable_info(). + */ + if (outerPlanState(mtstate)->instrument && + mergeActions[MERGE_WHEN_NOT_MATCHED_BY_SOURCE] && + mergeActions[MERGE_WHEN_NOT_MATCHED_BY_TARGET]) + InstrUpdateTupleCount(outerPlanState(mtstate)->instrument, 1.0); + } } /* |
