summaryrefslogtreecommitdiff
path: root/src/backend/commands
diff options
context:
space:
mode:
authorAmit Langote2025-03-19 03:14:24 +0000
committerAmit Langote2025-03-19 03:14:24 +0000
commit28317de723b60b61c40e7de4341a3029f698ddaf (patch)
tree8825f1161b48eaedf1efb6daaf32adfc72ab02c6 /src/backend/commands
parent06fb5612c970b3af95aca3db5a955669b07537ca (diff)
Ensure first ModifyTable rel initialized if all are pruned
Commit cbc127917e introduced tracking of unpruned relids to avoid processing pruned relations, and changed ExecInitModifyTable() to initialize only unpruned result relations. As a result, MERGE statements that prune all target partitions can now lead to crashes or incorrect behavior during execution. The crash occurs because some executor code paths rely on ModifyTableState.resultRelInfo[0] being present and initialized, even when no result relations remain after pruning. For example, ExecMerge() and ExecMergeNotMatched() use the first resultRelInfo to determine the appropriate action. Similarly, ExecInitPartitionInfo() assumes that at least one result relation exists. To preserve these assumptions, ExecInitModifyTable() now includes the first result relation in the initialized result relation list if all result relations for that ModifyTable were pruned. To enable that, ExecDoInitialPruning() ensures the first relation is locked if it was pruned and locking is necessary. To support this exception to the pruning logic, PlannedStmt now includes a list of RT indexes identifying the first result relation of each ModifyTable node in the plan. This allows ExecDoInitialPruning() to check whether each such relation was pruned and, if so, lock it if necessary. Bug: #18830 Reported-by: Robins Tharakan <[email protected]> Diagnozed-by: Tender Wang <[email protected]> Diagnozed-by: Dean Rasheed <[email protected]> Co-authored-by: Dean Rasheed <[email protected]> Reviewed-by: Tender Wang <[email protected]> Reviewed-by: Dean Rasheed <[email protected]> Discussion: https://postgr.es/m/18830-1f31ea1dc930d444%40postgresql.org
Diffstat (limited to 'src/backend/commands')
-rw-r--r--src/backend/commands/explain.c14
1 files changed, 12 insertions, 2 deletions
diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c
index 22616cf7add..33a16d2d8e2 100644
--- a/src/backend/commands/explain.c
+++ b/src/backend/commands/explain.c
@@ -4575,10 +4575,20 @@ show_modifytable_info(ModifyTableState *mtstate, List *ancestors,
break;
}
- /* Should we explicitly label target relations? */
+ /*
+ * Should we explicitly label target relations?
+ *
+ * If there's only one target relation, do not list it if it's the
+ * relation named in the query, or if it has been pruned. (Normally
+ * mtstate->resultRelInfo doesn't include pruned relations, but a single
+ * pruned target relation may be present, if all other target relations
+ * have been pruned. See ExecInitModifyTable().)
+ */
labeltargets = (mtstate->mt_nrels > 1 ||
(mtstate->mt_nrels == 1 &&
- mtstate->resultRelInfo[0].ri_RangeTableIndex != node->nominalRelation));
+ mtstate->resultRelInfo[0].ri_RangeTableIndex != node->nominalRelation &&
+ bms_is_member(mtstate->resultRelInfo[0].ri_RangeTableIndex,
+ mtstate->ps.state->es_unpruned_relids)));
if (labeltargets)
ExplainOpenGroup("Target Tables", "Target Tables", false, es);