From fab48a75f2bdb89ba96068938da759b6e67682c2 Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horiguchi.kyotaro@lab.ntt.co.jp>
Date: Wed, 22 Feb 2017 17:28:49 +0900
Subject: [PATCH 12/13] Fix a possible bug.

I haven't found that but calling postgresIterateForeignScan on eof'ed
node can cause a crash. This may fix it.
---
 contrib/postgres_fdw/postgres_fdw.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/postgres_fdw.c
index 04f520b..6b694d0 100644
--- a/contrib/postgres_fdw/postgres_fdw.c
+++ b/contrib/postgres_fdw/postgres_fdw.c
@@ -1449,6 +1449,16 @@ postgresIterateForeignScan(ForeignScanState *node)
 	{
 		ForeignScanState *next_conn_owner = node;
 
+		/*
+		 * This can be called for eof'ed node, do nothing other than returning
+		 * null tuple for the case
+		 */
+		if (GetPgFdwScanState(node)->eof_reached)
+		{
+			fsstate->result_ready = true;
+			return ExecClearTuple(slot);
+		}
+
 		/* This node has sent a query on this connection */
 		if (fsstate->s.connpriv->current_owner == node)
 		{
-- 
2.9.2

