From 1248fde46d39264f51c4dc07ad78c6c47b86605c Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@enterprisedb.com>
Date: Mon, 3 Apr 2017 14:27:52 +1200
Subject: [PATCH] Expose current QueryEnvironment to SPI_cursor_open.

For consistency with SPI_execute_plan, teach SPI_cursor_open to see the
current QueryEnvironment as established by SPI_register_relation.
---
 src/backend/executor/spi.c   | 3 +++
 src/pl/plpgsql/src/pl_exec.c | 9 ---------
 src/pl/plpgsql/src/plpgsql.h | 4 ----
 3 files changed, 3 insertions(+), 13 deletions(-)

diff --git a/src/backend/executor/spi.c b/src/backend/executor/spi.c
index 2f07a444b4d..a38fbd13e1c 100644
--- a/src/backend/executor/spi.c
+++ b/src/backend/executor/spi.c
@@ -1257,6 +1257,9 @@ SPI_cursor_open_internal(const char *name, SPIPlanPtr plan,
 					 errdetail("Scrollable cursors must be READ ONLY.")));
 	}
 
+	/* Make current query environment available to portal at execution time. */
+	portal->queryEnv = _SPI_current->queryEnv;
+
 	/*
 	 * If told to be read-only, or in parallel mode, verify that this query is
 	 * in fact read-only.  This can't be done earlier because we need to look
diff --git a/src/pl/plpgsql/src/pl_exec.c b/src/pl/plpgsql/src/pl_exec.c
index 43da986fc0a..b14b815a4da 100644
--- a/src/pl/plpgsql/src/pl_exec.c
+++ b/src/pl/plpgsql/src/pl_exec.c
@@ -695,7 +695,6 @@ plpgsql_exec_trigger(PLpgSQL_function *func,
 	 */
 	if (trigdata->tg_newtable || trigdata->tg_oldtable)
 	{
-		estate.queryEnv = create_queryEnv();
 		if (trigdata->tg_newtable)
 		{
 			EphemeralNamedRelation enr =
@@ -708,7 +707,6 @@ plpgsql_exec_trigger(PLpgSQL_function *func,
 			enr->md.enrtype = ENR_NAMED_TUPLESTORE;
 			enr->md.enrtuples = tuplestore_tuple_count(trigdata->tg_newtable);
 			enr->reldata = trigdata->tg_newtable;
-			register_ENR(estate.queryEnv, enr);
 			rc = SPI_register_relation(enr);
 			Assert(rc >= 0);
 		}
@@ -724,7 +722,6 @@ plpgsql_exec_trigger(PLpgSQL_function *func,
 			enr->md.enrtype = ENR_NAMED_TUPLESTORE;
 			enr->md.enrtuples = tuplestore_tuple_count(trigdata->tg_oldtable);
 			enr->reldata = trigdata->tg_oldtable;
-			register_ENR(estate.queryEnv, enr);
 			rc = SPI_register_relation(enr);
 			Assert(rc >= 0);
 		}
@@ -3483,9 +3480,6 @@ plpgsql_estate_setup(PLpgSQL_execstate *estate,
 	estate->paramLI->paramMask = NULL;
 	estate->params_dirty = false;
 
-	/* default tuplestore cache to "none" */
-	estate->queryEnv = NULL;
-
 	/* set up for use of appropriate simple-expression EState and cast hash */
 	if (simple_eval_estate)
 	{
@@ -7373,9 +7367,6 @@ exec_dynquery_with_params(PLpgSQL_execstate *estate,
 	/* Release transient data */
 	MemoryContextReset(stmt_mcontext);
 
-	/* Make sure the portal knows about any named tuplestores. */
-	portal->queryEnv = estate->queryEnv;
-
 	return portal;
 }
 
diff --git a/src/pl/plpgsql/src/plpgsql.h b/src/pl/plpgsql/src/plpgsql.h
index 43a62ef34e4..abb19035b66 100644
--- a/src/pl/plpgsql/src/plpgsql.h
+++ b/src/pl/plpgsql/src/plpgsql.h
@@ -20,7 +20,6 @@
 #include "commands/event_trigger.h"
 #include "commands/trigger.h"
 #include "executor/spi.h"
-#include "utils/queryenvironment.h"
 
 /**********************************************************************
  * Definitions
@@ -911,9 +910,6 @@ typedef struct PLpgSQL_execstate
 	ParamListInfo paramLI;
 	bool		params_dirty;	/* T if any resettable datum has been passed */
 
-	/* custom environment for parsing/execution of query for this context */
-	QueryEnvironment *queryEnv;
-
 	/* EState to use for "simple" expression evaluation */
 	EState	   *simple_eval_estate;
 
-- 
2.12.2

