From 28317de723b60b61c40e7de4341a3029f698ddaf Mon Sep 17 00:00:00 2001 From: Amit Langote Date: Wed, 19 Mar 2025 12:14:24 +0900 Subject: 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 Diagnozed-by: Tender Wang Diagnozed-by: Dean Rasheed Co-authored-by: Dean Rasheed Reviewed-by: Tender Wang Reviewed-by: Dean Rasheed Discussion: https://postgr.es/m/18830-1f31ea1dc930d444%40postgresql.org --- src/backend/commands/explain.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'src/backend/commands/explain.c') 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); -- cgit v1.2.3