From e16ff2f63cecbd4cb823d9d838717d8bb52b8a65 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@enterprisedb.com>
Date: Tue, 27 Feb 2018 14:25:43 +1300
Subject: [PATCH] Fix tuplestore read pointer confusion in
 nodeNamedtuplestorescan.c.

Tom Kazimiers discovered that transition tables don't work correctly when they
are scanned by more than one executor node.  That's because commit 18ce3a4a
allocated separate read pointers for each executor node as it should but
failed to make them active at the appropriate times.  Repair.

Author: Thomas Munro
Reported-By: Tom Kazimiers
Discussion: https://postgr.es/m/20180224034748.bixarv6632vbxgeb%40dewberry.localdomain
---
 src/backend/executor/nodeNamedtuplestorescan.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/src/backend/executor/nodeNamedtuplestorescan.c b/src/backend/executor/nodeNamedtuplestorescan.c
index 4d898b1f83..b260ad2594 100644
--- a/src/backend/executor/nodeNamedtuplestorescan.c
+++ b/src/backend/executor/nodeNamedtuplestorescan.c
@@ -40,6 +40,7 @@ NamedTuplestoreScanNext(NamedTuplestoreScanState *node)
 	 * Get the next tuple from tuplestore. Return NULL if no more tuples.
 	 */
 	slot = node->ss.ss_ScanTupleSlot;
+	tuplestore_select_read_pointer(node->relation, node->readptr);
 	(void) tuplestore_gettupleslot(node->relation, true, false, slot);
 	return slot;
 }
@@ -116,6 +117,7 @@ ExecInitNamedTuplestoreScan(NamedTuplestoreScan *node, EState *estate, int eflag
 	 * The new read pointer copies its position from read pointer 0, which
 	 * could be anywhere, so explicitly rewind it.
 	 */
+	tuplestore_select_read_pointer(scanstate->relation, scanstate->readptr);
 	tuplestore_rescan(scanstate->relation);
 
 	/*
-- 
2.15.1

