@@ -104,6 +104,7 @@ ExecInitAppend(Append *node, EState *estate, int eflags)
104104 PlanState * * appendplanstates ;
105105 Bitmapset * validsubplans ;
106106 int nplans ;
107+ int firstvalid ;
107108 int i ,
108109 j ;
109110 ListCell * 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 */
211214 j = i = 0 ;
215+ firstvalid = nplans ;
212216 foreach (lc , node -> appendplans )
213217 {
214218 if (bms_is_member (i , validsubplans ))
215219 {
216220 Plan * 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+
218229 appendplanstates [j ++ ] = ExecInitNode (initNode , estate , eflags );
219230 }
220231 i ++ ;
221232 }
222233
234+ appendstate -> as_first_partial_plan = firstvalid ;
223235 appendstate -> appendplans = appendplanstates ;
224236 appendstate -> as_nplans = nplans ;
225237
@@ -499,7 +511,6 @@ static bool
499511choose_next_subplan_for_leader (AppendState * node )
500512{
501513 ParallelAppendState * pstate = node -> as_pstate ;
502- Append * append = (Append * ) node -> ps .plan ;
503514
504515 /* Backward scan is not supported by parallel-aware plans */
505516 Assert (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 )
560571 node -> as_pstate -> pa_finished [node -> as_whichplan ] = true;
561572
562573 LWLockRelease (& pstate -> pa_lock );
@@ -581,7 +592,6 @@ static bool
581592choose_next_subplan_for_worker (AppendState * node )
582593{
583594 ParallelAppendState * pstate = node -> as_pstate ;
584- Append * append = (Append * ) node -> ps .plan ;
585595
586596 /* Backward scan is not supported by parallel-aware plans */
587597 Assert (ScanDirectionIsForward (node -> ps .state -> es_direction ));
@@ -629,14 +639,14 @@ choose_next_subplan_for_worker(AppendState *node)
629639 /* Advance to the next valid plan. */
630640 pstate -> 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 */
638648 nextplan = bms_next_member (node -> as_valid_subplans ,
639- append -> first_partial_plan - 1 );
649+ node -> as_first_partial_plan - 1 );
640650 pstate -> pa_next_plan =
641651 nextplan < 0 ? node -> as_whichplan : nextplan ;
642652 }
@@ -670,7 +680,7 @@ choose_next_subplan_for_worker(AppendState *node)
670680 if (pstate -> pa_next_plan < 0 )
671681 {
672682 int nextplan = bms_next_member (node -> as_valid_subplans ,
673- append -> first_partial_plan - 1 );
683+ node -> as_first_partial_plan - 1 );
674684
675685 if (nextplan >= 0 )
676686 pstate -> 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 )
690700 node -> as_pstate -> pa_finished [node -> as_whichplan ] = true;
691701
692702 LWLockRelease (& pstate -> pa_lock );
0 commit comments