From 9a785314183af0339a0be88dcc9245d24e8d9818 Mon Sep 17 00:00:00 2001
From: Amul Sul <sulamul@gmail.com>
Date: Wed, 6 Dec 2017 10:22:20 +0530
Subject: [PATCH] Parallel Append crash fix

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

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
---
 src/backend/executor/nodeAppend.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/src/backend/executor/nodeAppend.c b/src/backend/executor/nodeAppend.c
index 246a0b2d85..528a88b240 100644
--- a/src/backend/executor/nodeAppend.c
+++ b/src/backend/executor/nodeAppend.c
@@ -506,7 +506,14 @@ 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);
+		/* No partial plans then bail out. */
+		if (append->first_partial_plan >= node->as_nplans)
+		{
+			pstate->pa_next_plan = INVALID_SUBPLAN_INDEX;
+			node->as_whichplan = INVALID_SUBPLAN_INDEX;
+			LWLockRelease(&pstate->pa_lock);
+			return false;
+		}
 		pstate->pa_next_plan = append->first_partial_plan;
 	}
 
-- 
2.14.1

