Skip to content

Commit 468abb8

Browse files
committed
Fix incorrect logic for choosing the next Parallel Append subplan
In 499be01 support for pruning unneeded Append subnodes was added. The logic in that commit was not correctly checking if the next subplan was in fact a valid subplan. This could cause parallel workers processes to be given a subplan to work on which didn't require any work. Per code review following an otherwise unexplained regression failure in buildfarm member Pademelon. (We haven't been able to reproduce the failure, so this is a bit of a blind fix in terms of whether it'll actually fix it; but it is a clear bug nonetheless). In passing, also add a comment to explain what first_partial_plan means. Author: David Rowley Discussion: https://postgr.es/m/CAKJS1f_E5r05hHUVG3UmCQJ49DGKKHtN=SHybD44LdzBn+CJng@mail.gmail.com
1 parent d775482 commit 468abb8

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

src/backend/executor/nodeAppend.c

+38-12
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,11 @@ choose_next_subplan_for_leader(AppendState *node)
547547
LWLockRelease(&pstate->pa_lock);
548548
return false;
549549
}
550+
551+
/*
552+
* We needn't pay attention to as_valid_subplans here as all invalid
553+
* plans have been marked as finished.
554+
*/
550555
node->as_whichplan--;
551556
}
552557

@@ -612,18 +617,28 @@ choose_next_subplan_for_worker(AppendState *node)
612617
/* Save the plan from which we are starting the search. */
613618
node->as_whichplan = pstate->pa_next_plan;
614619

615-
/* Loop until we find a subplan to execute. */
620+
/* Loop until we find a valid subplan to execute. */
616621
while (pstate->pa_finished[pstate->pa_next_plan])
617622
{
618-
if (pstate->pa_next_plan < node->as_nplans - 1)
623+
int nextplan;
624+
625+
nextplan = bms_next_member(node->as_valid_subplans,
626+
pstate->pa_next_plan);
627+
if (nextplan >= 0)
619628
{
620-
/* Advance to next plan. */
621-
pstate->pa_next_plan++;
629+
/* Advance to the next valid plan. */
630+
pstate->pa_next_plan = nextplan;
622631
}
623632
else if (node->as_whichplan > append->first_partial_plan)
624633
{
625-
/* Loop back to first partial plan. */
626-
pstate->pa_next_plan = append->first_partial_plan;
634+
/*
635+
* Try looping back to the first valid partial plan, if there is
636+
* one. If there isn't, arrange to bail out below.
637+
*/
638+
nextplan = bms_next_member(node->as_valid_subplans,
639+
append->first_partial_plan - 1);
640+
pstate->pa_next_plan =
641+
nextplan < 0 ? node->as_whichplan : nextplan;
627642
}
628643
else
629644
{
@@ -644,16 +659,27 @@ choose_next_subplan_for_worker(AppendState *node)
644659
}
645660

646661
/* Pick the plan we found, and advance pa_next_plan one more time. */
647-
node->as_whichplan = pstate->pa_next_plan++;
648-
if (pstate->pa_next_plan >= node->as_nplans)
662+
node->as_whichplan = pstate->pa_next_plan;
663+
pstate->pa_next_plan = bms_next_member(node->as_valid_subplans,
664+
pstate->pa_next_plan);
665+
666+
/*
667+
* If there are no more valid plans then try setting the next plan to the
668+
* first valid partial plan.
669+
*/
670+
if (pstate->pa_next_plan < 0)
649671
{
650-
if (append->first_partial_plan < node->as_nplans)
651-
pstate->pa_next_plan = append->first_partial_plan;
672+
int nextplan = bms_next_member(node->as_valid_subplans,
673+
append->first_partial_plan - 1);
674+
675+
if (nextplan >= 0)
676+
pstate->pa_next_plan = nextplan;
652677
else
653678
{
654679
/*
655-
* We have only non-partial plans, and we already chose the last
656-
* one; so arrange for the other workers to immediately bail out.
680+
* There are no valid partial plans, and we already chose the last
681+
* non-partial plan; so flag that there's nothing more for our
682+
* fellow workers to do.
657683
*/
658684
pstate->pa_next_plan = INVALID_SUBPLAN_INDEX;
659685
}

src/include/nodes/plannodes.h

+5
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,11 @@ typedef struct Append
255255
/* RT indexes of non-leaf tables in a partition tree */
256256
List *partitioned_rels;
257257
List *appendplans;
258+
259+
/*
260+
* All 'appendplans' preceding this index are non-partial plans. All
261+
* 'appendplans' from this index onwards are partial plans.
262+
*/
258263
int first_partial_plan;
259264

260265
/*

0 commit comments

Comments
 (0)