From c538e14a0d785c76ec5555673418ae1c17ff79ba Mon Sep 17 00:00:00 2001
From: Amit Kapila <akapila@postgresql.org>
Date: Mon, 2 Sep 2019 12:36:21 +0530
Subject: [PATCH 1/2] Forbid Limit node to shutdown resources.

As part of commit 19df1702f5, we allowed a Limit node to shut down the
resources to get the correct stats from parallel nodes beneath it.  The
idea was correct, but we could not correctly identify the end of execution
and it ends up destroying the parallel context which is required for
rescans.  The correct fix is to identify whether rescans are possible and
then accordingly shutdown resources.  This would require us to invent some
new infrastructure which we can't back-patch. So, for now, we will just
disallow to shutdown resources via Limit node.  This will open up the
issue of incorrect stats when there are parallel nodes beneath Limit node,
but it will still be better to at least allow such queries to run to
completion and give correct results.

Reported-by: Jerry Sievers
Diagnosed-by: Thomas Munro
Author: Amit Kapila
Backpatch-through: 9.6
Discussion: https://postgr.es/m/87ims2amh6.fsf@jsievers.enova.com
---
 src/backend/executor/nodeLimit.c | 8 --------
 1 file changed, 8 deletions(-)

diff --git a/src/backend/executor/nodeLimit.c b/src/backend/executor/nodeLimit.c
index baa669a..d21b5ce 100644
--- a/src/backend/executor/nodeLimit.c
+++ b/src/backend/executor/nodeLimit.c
@@ -134,14 +134,6 @@ ExecLimit(PlanState *pstate)
 					node->position - node->offset >= node->count)
 				{
 					node->lstate = LIMIT_WINDOWEND;
-
-					/*
-					 * If we know we won't need to back up, we can release
-					 * resources at this point.
-					 */
-					if (!(node->ps.state->es_top_eflags & EXEC_FLAG_BACKWARD))
-						(void) ExecShutdownNode(outerPlan);
-
 					return NULL;
 				}
 
-- 
1.8.3.1

