Fix possible infinite loop with Parallel Append.
authorRobert Haas <rhaas@postgresql.org>
Thu, 8 Feb 2018 17:31:48 +0000 (12:31 -0500)
committerRobert Haas <rhaas@postgresql.org>
Thu, 8 Feb 2018 17:31:48 +0000 (12:31 -0500)
When the previously-chosen plan was non-partial, all pa_finished
flags for partial plans are now set, and pa_next_plan has not yet
been set to INVALID_SUBPLAN_INDEX, the previous code could go into
an infinite loop.

Report by Rajkumar Raghuwanshi.  Patch by Amit Khandekar and me.
Review by Kyotaro Horiguchi.

Discussion: http://postgr.es/m/CAJ3gD9cf43z78qY=U=H0HvOEN341qfRO-vLpnKPSviHeWgJQ5w@mail.gmail.com

src/backend/executor/nodeAppend.c

index 64a17fb03271ac72f8bcbaedb54ccbf12225230b..264d8fea8d931de4d5c7b07e4111ead15a7c8de4 100644 (file)
@@ -473,6 +473,9 @@ choose_next_subplan_for_worker(AppendState *node)
                return false;
        }
 
+       /* Save the plan from which we are starting the search. */
+       node->as_whichplan = pstate->pa_next_plan;
+
        /* Loop until we find a subplan to execute. */
        while (pstate->pa_finished[pstate->pa_next_plan])
        {
@@ -481,14 +484,17 @@ 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 (node->as_whichplan > append->first_partial_plan)
                {
                        /* Loop back to first partial plan. */
                        pstate->pa_next_plan = append->first_partial_plan;
                }
                else
                {
-                       /* At last plan, no partial plans, arrange to bail out. */
+                       /*
+                        * At last plan, and either there are no partial plans or we've
+                        * tried them all.  Arrange to bail out.
+                        */
                        pstate->pa_next_plan = node->as_whichplan;
                }