Skip to content

Commit 953ff99

Browse files
committed
Fix parallel-safety marking when moving initplans to another node.
Our policy since commit ab77a5a has been that a plan node having any initplans is automatically not parallel-safe. (This could be relaxed, but not today.) clean_up_removed_plan_level neglected this, and could attach initplans to a parallel-safe child plan node without clearing the plan's parallel-safe flag. That could lead to "subplan was not initialized" errors at runtime, in case an initplan referenced another one and only the referencing one got transmitted to parallel workers. The fix in clean_up_removed_plan_level is trivial enough. materialize_finished_plan also moves initplans from one node to another, but it's okay because it already copies the source node's parallel_safe flag. The other place that does this kind of thing is standard_planner's hack to inject a top-level Gather when debug_parallel_query is active. But that's actually dead code given that we're correctly enforcing the "initplans aren't parallel safe" rule, so just replace it with an Assert that there are no initplans. Also improve some related comments. Normally we'd add a regression test case for this sort of bug. The mistake itself is already reached by existing tests, but there is accidentally no visible problem. The only known test case that creates an actual failure seems too indirect and fragile to justify keeping it as a regression test (not least because it fails to fail in v11, though the bug is clearly present there too). Per report from Justin Pryzby. Back-patch to all supported branches. Discussion: https://postgr.es/m/ZDVt6MaNWkRDO1LQ@telsasoft.com
1 parent bbf7336 commit 953ff99

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

src/backend/optimizer/plan/planner.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -431,12 +431,10 @@ standard_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
431431
Gather *gather = makeNode(Gather);
432432

433433
/*
434-
* If there are any initPlans attached to the formerly-top plan node,
435-
* move them up to the Gather node; same as we do for Material node in
436-
* materialize_finished_plan.
434+
* Top plan must not have any initPlans, else it shouldn't have been
435+
* marked parallel-safe.
437436
*/
438-
gather->plan.initPlan = top_plan->initPlan;
439-
top_plan->initPlan = NIL;
437+
Assert(top_plan->initPlan == NIL);
440438

441439
gather->plan.targetlist = top_plan->targetlist;
442440
gather->plan.qual = NIL;

src/backend/optimizer/plan/setrefs.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,19 @@ trivial_subqueryscan(SubqueryScan *plan)
11591159
static Plan *
11601160
clean_up_removed_plan_level(Plan *parent, Plan *child)
11611161
{
1162-
/* We have to be sure we don't lose any initplans */
1162+
/*
1163+
* We have to be sure we don't lose any initplans, so move any that were
1164+
* attached to the parent plan to the child. If we do move any, the child
1165+
* is no longer parallel-safe.
1166+
*/
1167+
if (parent->initPlan)
1168+
child->parallel_safe = false;
1169+
1170+
/*
1171+
* Attach plans this way so that parent's initplans are processed before
1172+
* any pre-existing initplans of the child. Probably doesn't matter, but
1173+
* let's preserve the ordering just in case.
1174+
*/
11631175
child->initPlan = list_concat(parent->initPlan,
11641176
child->initPlan);
11651177

src/backend/optimizer/plan/subselect.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,7 +2093,7 @@ SS_identify_outer_params(PlannerInfo *root)
20932093
* This is separate from SS_attach_initplans because we might conditionally
20942094
* create more initPlans during create_plan(), depending on which Path we
20952095
* select. However, Paths that would generate such initPlans are expected
2096-
* to have included their cost already.
2096+
* to have included their cost and parallel-safety effects already.
20972097
*/
20982098
void
20992099
SS_charge_for_initplans(PlannerInfo *root, RelOptInfo *final_rel)
@@ -2149,8 +2149,10 @@ SS_charge_for_initplans(PlannerInfo *root, RelOptInfo *final_rel)
21492149
* (In principle the initPlans could go in any node at or above where they're
21502150
* referenced; but there seems no reason to put them any lower than the
21512151
* topmost node, so we don't bother to track exactly where they came from.)
2152-
* We do not touch the plan node's cost; the initplans should have been
2153-
* accounted for in path costing.
2152+
*
2153+
* We do not touch the plan node's cost or parallel_safe flag. The initplans
2154+
* must have been accounted for in SS_charge_for_initplans, or by any later
2155+
* code that adds initplans via SS_make_initplan_from_plan.
21542156
*/
21552157
void
21562158
SS_attach_initplans(PlannerInfo *root, Plan *plan)

src/backend/optimizer/util/pathnode.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3232,7 +3232,7 @@ create_minmaxagg_path(PlannerInfo *root,
32323232
/* For now, assume we are above any joins, so no parameterization */
32333233
pathnode->path.param_info = NULL;
32343234
pathnode->path.parallel_aware = false;
3235-
/* A MinMaxAggPath implies use of subplans, so cannot be parallel-safe */
3235+
/* A MinMaxAggPath implies use of initplans, so cannot be parallel-safe */
32363236
pathnode->path.parallel_safe = false;
32373237
pathnode->path.parallel_workers = 0;
32383238
/* Result is one unordered row */

0 commit comments

Comments
 (0)