diff --git a/src/backend/executor/nodeAppend.c b/src/backend/executor/nodeAppend.c index 64a17fb..2dbb658 100644 --- a/src/backend/executor/nodeAppend.c +++ b/src/backend/executor/nodeAppend.c @@ -456,6 +456,8 @@ choose_next_subplan_for_worker(AppendState *node) { ParallelAppendState *pstate = node->as_pstate; Append *append = (Append *) node->ps.plan; + int initial_plan; + bool should_wrap_around; /* Backward scan is not supported by parallel-aware plans */ Assert(ScanDirectionIsForward(node->ps.state->es_direction)); @@ -473,6 +475,14 @@ choose_next_subplan_for_worker(AppendState *node) return false; } + /* + * If we are starting the search from a non-partial plan, no point in + * looping back to the first partial plan: we would have covered all the + * plans by the time we reach the last plan. + */ + initial_plan = pstate->pa_next_plan; + should_wrap_around = (initial_plan >= append->first_partial_plan); + /* Loop until we find a subplan to execute. */ while (pstate->pa_finished[pstate->pa_next_plan]) { @@ -481,18 +491,18 @@ choose_next_subplan_for_worker(AppendState *node) /* Advance to next plan. */ pstate->pa_next_plan++; } - else if (append->first_partial_plan < node->as_nplans) + else if (!should_wrap_around) { - /* Loop back to first partial plan. */ - pstate->pa_next_plan = append->first_partial_plan; + /* Covered all plans, arrange to bail out. */ + pstate->pa_next_plan = initial_plan; } else { - /* At last plan, no partial plans, arrange to bail out. */ - pstate->pa_next_plan = node->as_whichplan; + /* Loop back to first partial plan. */ + pstate->pa_next_plan = append->first_partial_plan; } - if (pstate->pa_next_plan == node->as_whichplan) + if (pstate->pa_next_plan == initial_plan) { /* We've tried everything! */ pstate->pa_next_plan = INVALID_SUBPLAN_INDEX;