@@ -104,6 +104,7 @@ ExecInitAppend(Append *node, EState *estate, int eflags)
104104PlanState * * appendplanstates ;
105105Bitmapset * validsubplans ;
106106int nplans ;
107+ int firstvalid ;
107108int i ,
108109j ;
109110ListCell * lc ;
@@ -207,19 +208,30 @@ ExecInitAppend(Append *node, EState *estate, int eflags)
207208/*
208209 * call ExecInitNode on each of the valid plans to be executed and save
209210 * the results into the appendplanstates array.
211+ *
212+ * While at it, find out the first valid partial plan.
210213 */
211214j = i = 0 ;
215+ firstvalid = nplans ;
212216foreach (lc ,node -> appendplans )
213217{
214218if (bms_is_member (i ,validsubplans ))
215219{
216220Plan * initNode = (Plan * )lfirst (lc );
217221
222+ /*
223+ * Record the lowest appendplans index which is a valid partial
224+ * plan.
225+ */
226+ if (i >=node -> first_partial_plan && j < firstvalid )
227+ firstvalid = j ;
228+
218229appendplanstates [j ++ ]= ExecInitNode (initNode ,estate ,eflags );
219230}
220231i ++ ;
221232}
222233
234+ appendstate -> as_first_partial_plan = firstvalid ;
223235appendstate -> appendplans = appendplanstates ;
224236appendstate -> as_nplans = nplans ;
225237
@@ -499,7 +511,6 @@ static bool
499511choose_next_subplan_for_leader (AppendState * node )
500512{
501513ParallelAppendState * pstate = node -> as_pstate ;
502- Append * append = (Append * )node -> ps .plan ;
503514
504515/* Backward scan is not supported by parallel-aware plans */
505516Assert (ScanDirectionIsForward (node -> ps .state -> es_direction ));
@@ -556,7 +567,7 @@ choose_next_subplan_for_leader(AppendState *node)
556567}
557568
558569/* If non-partial, immediately mark as finished. */
559- if (node -> as_whichplan < append -> first_partial_plan )
570+ if (node -> as_whichplan < node -> as_first_partial_plan )
560571node -> as_pstate -> pa_finished [node -> as_whichplan ]= true;
561572
562573LWLockRelease (& pstate -> pa_lock );
@@ -581,7 +592,6 @@ static bool
581592choose_next_subplan_for_worker (AppendState * node )
582593{
583594ParallelAppendState * pstate = node -> as_pstate ;
584- Append * append = (Append * )node -> ps .plan ;
585595
586596/* Backward scan is not supported by parallel-aware plans */
587597Assert (ScanDirectionIsForward (node -> ps .state -> es_direction ));
@@ -629,14 +639,14 @@ choose_next_subplan_for_worker(AppendState *node)
629639/* Advance to the next valid plan. */
630640pstate -> pa_next_plan = nextplan ;
631641}
632- else if (node -> as_whichplan > append -> first_partial_plan )
642+ else if (node -> as_whichplan > node -> as_first_partial_plan )
633643{
634644/*
635645 * Try looping back to the first valid partial plan, if there is
636646 * one. If there isn't, arrange to bail out below.
637647 */
638648nextplan = bms_next_member (node -> as_valid_subplans ,
639- append -> first_partial_plan - 1 );
649+ node -> as_first_partial_plan - 1 );
640650pstate -> pa_next_plan =
641651nextplan < 0 ?node -> as_whichplan :nextplan ;
642652}
@@ -670,7 +680,7 @@ choose_next_subplan_for_worker(AppendState *node)
670680if (pstate -> pa_next_plan < 0 )
671681{
672682int nextplan = bms_next_member (node -> as_valid_subplans ,
673- append -> first_partial_plan - 1 );
683+ node -> as_first_partial_plan - 1 );
674684
675685if (nextplan >=0 )
676686pstate -> pa_next_plan = nextplan ;
@@ -686,7 +696,7 @@ choose_next_subplan_for_worker(AppendState *node)
686696}
687697
688698/* If non-partial, immediately mark as finished. */
689- if (node -> as_whichplan < append -> first_partial_plan )
699+ if (node -> as_whichplan < node -> as_first_partial_plan )
690700node -> as_pstate -> pa_finished [node -> as_whichplan ]= true;
691701
692702LWLockRelease (& pstate -> pa_lock );