From 39ab995282e74422a69b0091e129ae051619b96e Mon Sep 17 00:00:00 2001
From: Jelte Fennema <jelte.fennema@microsoft.com>
Date: Fri, 25 Aug 2023 19:48:24 +0200
Subject: [PATCH v3 2/3] Completely remove fixed_result from CachedPlanSource

Because of the previous patch, no fixed_result plans exist anymore. This
removes all references. It's done in a separate commit to ease
reviewing, if this gets merged we might want to squash them together.
---
 src/backend/commands/prepare.c      | 12 +-----------
 src/backend/executor/spi.c          |  6 ++----
 src/backend/tcop/postgres.c         |  6 +-----
 src/backend/utils/cache/plancache.c |  7 +------
 src/include/utils/plancache.h       |  4 +---
 5 files changed, 6 insertions(+), 29 deletions(-)

diff --git a/src/backend/commands/prepare.c b/src/backend/commands/prepare.c
index 18f70319fc4..da3115beb99 100644
--- a/src/backend/commands/prepare.c
+++ b/src/backend/commands/prepare.c
@@ -127,8 +127,7 @@ PrepareQuery(ParseState *pstate, PrepareStmt *stmt,
 					   nargs,
 					   NULL,
 					   NULL,
-					   CURSOR_OPT_PARALLEL_OK,	/* allow parallel mode */
-					   true);	/* fixed result */
+					   CURSOR_OPT_PARALLEL_OK); /* allow parallel mode */
 
 	/*
 	 * Save the results.
@@ -165,10 +164,6 @@ ExecuteQuery(ParseState *pstate,
 	/* Look it up in the hash table */
 	entry = FetchPreparedStatement(stmt->name, true);
 
-	/* Shouldn't find a non-fixed-result cached plan */
-	if (!entry->plansource->fixed_result)
-		elog(ERROR, "EXECUTE does not support variable-result cached plans");
-
 	/* Evaluate parameters, if any */
 	if (entry->plansource->num_params > 0)
 	{
@@ -469,7 +464,6 @@ FetchPreparedStatementResultDesc(PreparedStatement *stmt)
 	 * Since we don't allow prepared statements' result tupdescs to change,
 	 * there's no need to worry about revalidating the cached plan here.
 	 */
-	Assert(stmt->plansource->fixed_result);
 	if (stmt->plansource->resultDesc)
 		return CreateTupleDescCopy(stmt->plansource->resultDesc);
 	else
@@ -591,10 +585,6 @@ ExplainExecuteQuery(ExecuteStmt *execstmt, IntoClause *into, ExplainState *es,
 	/* Look it up in the hash table */
 	entry = FetchPreparedStatement(execstmt->name, true);
 
-	/* Shouldn't find a non-fixed-result cached plan */
-	if (!entry->plansource->fixed_result)
-		elog(ERROR, "EXPLAIN EXECUTE does not support variable-result cached plans");
-
 	query_string = entry->plansource->query_string;
 
 	/* Evaluate parameters, if any */
diff --git a/src/backend/executor/spi.c b/src/backend/executor/spi.c
index 33975687b38..52128290741 100644
--- a/src/backend/executor/spi.c
+++ b/src/backend/executor/spi.c
@@ -2285,8 +2285,7 @@ _SPI_prepare_plan(const char *src, SPIPlanPtr plan)
 						   plan->nargs,
 						   plan->parserSetup,
 						   plan->parserSetupArg,
-						   plan->cursor_options,
-						   false);	/* not fixed result */
+						   plan->cursor_options);	/* not fixed result */
 
 		plancache_list = lappend(plancache_list, plansource);
 	}
@@ -2522,8 +2521,7 @@ _SPI_execute_plan(SPIPlanPtr plan, const SPIExecuteOptions *options,
 							   plan->nargs,
 							   plan->parserSetup,
 							   plan->parserSetupArg,
-							   plan->cursor_options,
-							   false);	/* not fixed result */
+							   plan->cursor_options);
 		}
 
 		/*
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index 8ea654a4c26..9371a8f6e51 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -1576,8 +1576,7 @@ exec_parse_message(const char *query_string,	/* string to execute */
 					   numParams,
 					   NULL,
 					   NULL,
-					   CURSOR_OPT_PARALLEL_OK,	/* allow parallel mode */
-					   true);	/* fixed result */
+					   CURSOR_OPT_PARALLEL_OK); /* allow parallel mode */
 
 	/* If we got a cancel signal during analysis, quit */
 	CHECK_FOR_INTERRUPTS();
@@ -2651,9 +2650,6 @@ exec_describe_statement_message(const char *stmt_name)
 					 errmsg("unnamed prepared statement does not exist")));
 	}
 
-	/* Prepared statements shouldn't have changeable result descs */
-	Assert(psrc->fixed_result);
-
 	/*
 	 * If we are in aborted transaction state, we can't run
 	 * SendRowDescriptionMessage(), because that needs catalog accesses.
diff --git a/src/backend/utils/cache/plancache.c b/src/backend/utils/cache/plancache.c
index a6fbe235381..1106215b781 100644
--- a/src/backend/utils/cache/plancache.c
+++ b/src/backend/utils/cache/plancache.c
@@ -203,7 +203,6 @@ CreateCachedPlan(RawStmt *raw_parse_tree,
 	plansource->parserSetup = NULL;
 	plansource->parserSetupArg = NULL;
 	plansource->cursor_options = 0;
-	plansource->fixed_result = false;
 	plansource->resultDesc = NULL;
 	plansource->context = source_context;
 	plansource->query_list = NIL;
@@ -271,7 +270,6 @@ CreateOneShotCachedPlan(RawStmt *raw_parse_tree,
 	plansource->parserSetup = NULL;
 	plansource->parserSetupArg = NULL;
 	plansource->cursor_options = 0;
-	plansource->fixed_result = false;
 	plansource->resultDesc = NULL;
 	plansource->context = CurrentMemoryContext;
 	plansource->query_list = NIL;
@@ -346,8 +344,7 @@ CompleteCachedPlan(CachedPlanSource *plansource,
 				   int num_params,
 				   ParserSetupHook parserSetup,
 				   void *parserSetupArg,
-				   int cursor_options,
-				   bool fixed_result)
+				   int cursor_options)
 {
 	MemoryContext source_context = plansource->context;
 	MemoryContext oldcxt = CurrentMemoryContext;
@@ -430,7 +427,6 @@ CompleteCachedPlan(CachedPlanSource *plansource,
 	plansource->parserSetup = parserSetup;
 	plansource->parserSetupArg = parserSetupArg;
 	plansource->cursor_options = cursor_options;
-	plansource->fixed_result = fixed_result;
 	plansource->resultDesc = PlanCacheComputeResultDesc(querytree_list);
 
 	MemoryContextSwitchTo(oldcxt);
@@ -1547,7 +1543,6 @@ CopyCachedPlan(CachedPlanSource *plansource)
 	newsource->parserSetup = plansource->parserSetup;
 	newsource->parserSetupArg = plansource->parserSetupArg;
 	newsource->cursor_options = plansource->cursor_options;
-	newsource->fixed_result = plansource->fixed_result;
 	if (plansource->resultDesc)
 		newsource->resultDesc = CreateTupleDescCopy(plansource->resultDesc);
 	else
diff --git a/src/include/utils/plancache.h b/src/include/utils/plancache.h
index 916e59d9fef..36fd0104aec 100644
--- a/src/include/utils/plancache.h
+++ b/src/include/utils/plancache.h
@@ -104,7 +104,6 @@ typedef struct CachedPlanSource
 	ParserSetupHook parserSetup;	/* alternative parameter spec method */
 	void	   *parserSetupArg;
 	int			cursor_options; /* cursor options used for planning */
-	bool		fixed_result;	/* disallow change in result tupdesc? */
 	TupleDesc	resultDesc;		/* result type; NULL = doesn't return tuples */
 	MemoryContext context;		/* memory context holding all above */
 	/* These fields describe the current analyzed-and-rewritten query tree: */
@@ -201,8 +200,7 @@ extern void CompleteCachedPlan(CachedPlanSource *plansource,
 							   int num_params,
 							   ParserSetupHook parserSetup,
 							   void *parserSetupArg,
-							   int cursor_options,
-							   bool fixed_result);
+							   int cursor_options);
 
 extern void SaveCachedPlan(CachedPlanSource *plansource);
 extern void DropCachedPlan(CachedPlanSource *plansource);
-- 
2.34.1

