From 800996d580e7eb3395efeb9c95d6c077866e84b8 Mon Sep 17 00:00:00 2001
From: Amul Sul <sulamul@gmail.com>
Date: Wed, 6 Dec 2017 14:28:27 +0530
Subject: [PATCH] Parallel Append crash fix v2

v2: Update w.r.t Amit Khandekar's review comments[3]:
 - Setting pstate->pa_next_plan to invalid index, not to bail out

v1:
Fixes the crash on builfarm[1]

The reason behind the crash is the same that we had discussed before[2]
but this time crash is due to assert that I've added in the assumption
that we always coming  from the previous check in WHILE loop

--------------
References:
--------------
1] https://postgr.es/m/17868.1512519318@sss.pgh.pa.us
2] https://postgr.es/m/CAAJ_b975k58H+Ed4=p0vbJunwO2reOMX5CVB8_R=JmXxY3uW=Q@mail.gmail.com
3] https://postgr.es/m/CAJ3gD9e3_D3fFqzWBFYoaF-6yCXgbOFn3Mb-pgd_mxvjpsw7Rw@mail.gmail.com
---
 src/backend/executor/nodeAppend.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/backend/executor/nodeAppend.c b/src/backend/executor/nodeAppend.c
index 246a0b2d85..96d9a7aed5 100644
--- a/src/backend/executor/nodeAppend.c
+++ b/src/backend/executor/nodeAppend.c
@@ -506,8 +506,10 @@ choose_next_subplan_for_worker(AppendState *node)
 	node->as_whichplan = pstate->pa_next_plan++;
 	if (pstate->pa_next_plan >= node->as_nplans)
 	{
-		Assert(append->first_partial_plan < node->as_nplans);
-		pstate->pa_next_plan = append->first_partial_plan;
+		if (append->first_partial_plan < node->as_nplans)
+			pstate->pa_next_plan = append->first_partial_plan;
+		else
+			pstate->pa_next_plan = INVALID_SUBPLAN_INDEX;
 	}
 
 	/* If non-partial, immediately mark as finished. */
-- 
2.14.1

