From e998e296a3730938e1bef634c40e43480849b9cc Mon Sep 17 00:00:00 2001
From: Sami Imseih <simseih@amazon.com>
Date: Mon, 7 Apr 2025 15:14:05 -0500
Subject: [PATCH v5 1/1] Add plan_cache counters to pg_stat_statements.

This adds the ability for users to track how many
times a prepared statement was executed using either
a generic plan or a custom plan, by introducing two new
counters in pg_stat_statements: generic_plan_calls and
custom_plan_calls.

Discussion: https://www.postgresql.org/message-id/flat/CAA5RZ0uFw8Y9GCFvafhC=OA8NnMqVZyzXPfv_EePOt+iv1T-qQ@mail.gmail.com
Reviewers: Greg Sabino Mullane, Ilia Evdokimov
---
 contrib/pg_stat_statements/Makefile           |  3 +-
 .../expected/plan_cache.out                   | 87 +++++++++++++++++++
 contrib/pg_stat_statements/meson.build        |  2 +
 .../pg_stat_statements--1.12--1.13.sql        | 78 +++++++++++++++++
 .../pg_stat_statements/pg_stat_statements.c   | 55 ++++++++++--
 .../pg_stat_statements.control                |  2 +-
 contrib/pg_stat_statements/sql/plan_cache.sql | 50 +++++++++++
 doc/src/sgml/pgstatstatements.sgml            | 18 ++++
 src/backend/utils/cache/plancache.c           | 10 ++-
 src/include/utils/plancache.h                 | 12 ++-
 10 files changed, 303 insertions(+), 14 deletions(-)
 create mode 100644 contrib/pg_stat_statements/expected/plan_cache.out
 create mode 100644 contrib/pg_stat_statements/pg_stat_statements--1.12--1.13.sql
 create mode 100644 contrib/pg_stat_statements/sql/plan_cache.sql

diff --git a/contrib/pg_stat_statements/Makefile b/contrib/pg_stat_statements/Makefile
index b2bd8794d2a..fc395e16e2c 100644
--- a/contrib/pg_stat_statements/Makefile
+++ b/contrib/pg_stat_statements/Makefile
@@ -7,6 +7,7 @@ OBJS = \
 
 EXTENSION = pg_stat_statements
 DATA = pg_stat_statements--1.4.sql \
+	pg_stat_statements--1.12--1.13.sql \
 	pg_stat_statements--1.11--1.12.sql pg_stat_statements--1.10--1.11.sql \
 	pg_stat_statements--1.9--1.10.sql pg_stat_statements--1.8--1.9.sql \
 	pg_stat_statements--1.7--1.8.sql pg_stat_statements--1.6--1.7.sql \
@@ -20,7 +21,7 @@ LDFLAGS_SL += $(filter -lm, $(LIBS))
 REGRESS_OPTS = --temp-config $(top_srcdir)/contrib/pg_stat_statements/pg_stat_statements.conf
 REGRESS = select dml cursors utility level_tracking planning \
 	user_activity wal entry_timestamp privileges extended \
-	parallel cleanup oldextversions squashing
+	parallel cleanup oldextversions squashing plan_cache
 # Disabled because these tests require "shared_preload_libraries=pg_stat_statements",
 # which typical installcheck users do not have (e.g. buildfarm clients).
 NO_INSTALLCHECK = 1
diff --git a/contrib/pg_stat_statements/expected/plan_cache.out b/contrib/pg_stat_statements/expected/plan_cache.out
new file mode 100644
index 00000000000..bf11794528c
--- /dev/null
+++ b/contrib/pg_stat_statements/expected/plan_cache.out
@@ -0,0 +1,87 @@
+--
+-- Information related to plan cache
+--
+SELECT pg_stat_statements_reset() IS NOT NULL AS t;
+ t 
+---
+ t
+(1 row)
+
+-- plan cache counters for prepared statements
+PREPARE p1 AS SELECT $1;
+-- plan cache auto
+SET plan_cache_mode TO auto;
+EXECUTE p1(1);
+ ?column? 
+----------
+ 1
+(1 row)
+
+-- force generic plan
+SET plan_cache_mode TO force_generic_plan;
+EXECUTE p1(1);
+ ?column? 
+----------
+ 1
+(1 row)
+
+-- force custom plan
+SET plan_cache_mode TO force_custom_plan;
+EXECUTE p1(1);
+ ?column? 
+----------
+ 1
+(1 row)
+
+-- plan cache counters for functions and procedures
+SET pg_stat_statements.track = 'all';
+CREATE OR REPLACE FUNCTION select_one_func(int) RETURNS VOID AS $$
+DECLARE
+    ret INT;
+BEGIN
+    SELECT $1 INTO ret;
+END;
+$$ LANGUAGE plpgsql;
+CREATE OR REPLACE PROCEDURE select_one_proc(int) AS $$
+DECLARE
+    ret INT;
+BEGIN
+    select $1 INTO ret;
+END;
+$$ LANGUAGE plpgsql;
+-- plan cache auto
+SET plan_cache_mode TO auto;
+SELECT select_one_func(1);
+ select_one_func 
+-----------------
+ 
+(1 row)
+
+CALL select_one_proc(1);
+-- force generic plan
+SET plan_cache_mode TO force_generic_plan;
+SELECT select_one_func(1);
+ select_one_func 
+-----------------
+ 
+(1 row)
+
+CALL select_one_proc(1);
+-- force custom plan
+SET plan_cache_mode TO force_custom_plan;
+SELECT select_one_func(1);
+ select_one_func 
+-----------------
+ 
+(1 row)
+
+CALL select_one_proc(1);
+-- get the plan cache counters
+SELECT calls, generic_plan_calls, custom_plan_calls, toplevel, calls, query FROM pg_stat_statements
+  WHERE query = 'SELECT $1' ORDER BY query COLLATE "C";
+ calls | generic_plan_calls | custom_plan_calls | toplevel | calls |   query   
+-------+--------------------+-------------------+----------+-------+-----------
+     6 |                  2 |                 4 | f        |     6 | SELECT $1
+     3 |                  1 |                 2 | t        |     3 | SELECT $1
+(2 rows)
+
diff --git a/contrib/pg_stat_statements/meson.build b/contrib/pg_stat_statements/meson.build
index 01a6cbdcf61..3246c03106a 100644
--- a/contrib/pg_stat_statements/meson.build
+++ b/contrib/pg_stat_statements/meson.build
@@ -21,6 +21,7 @@ contrib_targets += pg_stat_statements
 install_data(
   'pg_stat_statements.control',
   'pg_stat_statements--1.4.sql',
+  'pg_stat_statements--1.12--1.13.sql',
   'pg_stat_statements--1.11--1.12.sql',
   'pg_stat_statements--1.10--1.11.sql',
   'pg_stat_statements--1.9--1.10.sql',
@@ -57,6 +58,7 @@ tests += {
       'cleanup',
       'oldextversions',
       'squashing',
+      'plan_cache',
     ],
     'regress_args': ['--temp-config', files('pg_stat_statements.conf')],
     # Disabled because these tests require
diff --git a/contrib/pg_stat_statements/pg_stat_statements--1.12--1.13.sql b/contrib/pg_stat_statements/pg_stat_statements--1.12--1.13.sql
new file mode 100644
index 00000000000..67f2f7dbca3
--- /dev/null
+++ b/contrib/pg_stat_statements/pg_stat_statements--1.12--1.13.sql
@@ -0,0 +1,78 @@
+/* contrib/pg_stat_statements/pg_stat_statements--1.12--1.13.sql */
+
+-- complain if script is sourced in psql, rather than via ALTER EXTENSION
+\echo Use "ALTER EXTENSION pg_stat_statements UPDATE TO '1.13'" to load this file. \quit
+
+/* First we have to remove them from the extension */
+ALTER EXTENSION pg_stat_statements DROP VIEW pg_stat_statements;
+ALTER EXTENSION pg_stat_statements DROP FUNCTION pg_stat_statements(boolean);
+
+/* Then we can drop them */
+DROP VIEW pg_stat_statements;
+DROP FUNCTION pg_stat_statements(boolean);
+
+/* Now redefine */
+CREATE FUNCTION pg_stat_statements(IN showtext boolean,
+    OUT userid oid,
+    OUT dbid oid,
+    OUT toplevel bool,
+    OUT queryid bigint,
+    OUT query text,
+    OUT plans int8,
+    OUT total_plan_time float8,
+    OUT min_plan_time float8,
+    OUT max_plan_time float8,
+    OUT mean_plan_time float8,
+    OUT stddev_plan_time float8,
+    OUT calls int8,
+    OUT total_exec_time float8,
+    OUT min_exec_time float8,
+    OUT max_exec_time float8,
+    OUT mean_exec_time float8,
+    OUT stddev_exec_time float8,
+    OUT rows int8,
+    OUT shared_blks_hit int8,
+    OUT shared_blks_read int8,
+    OUT shared_blks_dirtied int8,
+    OUT shared_blks_written int8,
+    OUT local_blks_hit int8,
+    OUT local_blks_read int8,
+    OUT local_blks_dirtied int8,
+    OUT local_blks_written int8,
+    OUT temp_blks_read int8,
+    OUT temp_blks_written int8,
+    OUT shared_blk_read_time float8,
+    OUT shared_blk_write_time float8,
+    OUT local_blk_read_time float8,
+    OUT local_blk_write_time float8,
+    OUT temp_blk_read_time float8,
+    OUT temp_blk_write_time float8,
+    OUT wal_records int8,
+    OUT wal_fpi int8,
+    OUT wal_bytes numeric,
+    OUT wal_buffers_full int8,
+    OUT jit_functions int8,
+    OUT jit_generation_time float8,
+    OUT jit_inlining_count int8,
+    OUT jit_inlining_time float8,
+    OUT jit_optimization_count int8,
+    OUT jit_optimization_time float8,
+    OUT jit_emission_count int8,
+    OUT jit_emission_time float8,
+    OUT jit_deform_count int8,
+    OUT jit_deform_time float8,
+    OUT parallel_workers_to_launch int8,
+    OUT parallel_workers_launched int8,
+    OUT stats_since timestamp with time zone,
+    OUT minmax_stats_since timestamp with time zone,
+    OUT generic_plan_calls int8,
+    OUT custom_plan_calls int8
+)
+RETURNS SETOF record
+AS 'MODULE_PATHNAME', 'pg_stat_statements_1_13'
+LANGUAGE C STRICT VOLATILE PARALLEL SAFE;
+
+CREATE VIEW pg_stat_statements AS
+  SELECT * FROM pg_stat_statements(true);
+
+GRANT SELECT ON pg_stat_statements TO PUBLIC;
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 9778407cba3..26a956bf917 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -69,6 +69,7 @@
 #include "utils/acl.h"
 #include "utils/builtins.h"
 #include "utils/memutils.h"
+#include "utils/plancache.h"
 #include "utils/timestamp.h"
 
 PG_MODULE_MAGIC_EXT(
@@ -114,6 +115,7 @@ typedef enum pgssVersion
 	PGSS_V1_10,
 	PGSS_V1_11,
 	PGSS_V1_12,
+	PGSS_V1_13,
 } pgssVersion;
 
 typedef enum pgssStoreKind
@@ -210,6 +212,8 @@ typedef struct Counters
 											 * to be launched */
 	int64		parallel_workers_launched;	/* # of parallel workers actually
 											 * launched */
+	int64		generic_plan_calls;		/* number of calls using a generic plan */
+	int64		custom_plan_calls;		/* number of calls using a custom plan */
 } Counters;
 
 /*
@@ -323,6 +327,7 @@ PG_FUNCTION_INFO_V1(pg_stat_statements_1_9);
 PG_FUNCTION_INFO_V1(pg_stat_statements_1_10);
 PG_FUNCTION_INFO_V1(pg_stat_statements_1_11);
 PG_FUNCTION_INFO_V1(pg_stat_statements_1_12);
+PG_FUNCTION_INFO_V1(pg_stat_statements_1_13);
 PG_FUNCTION_INFO_V1(pg_stat_statements);
 PG_FUNCTION_INFO_V1(pg_stat_statements_info);
 
@@ -355,7 +360,8 @@ static void pgss_store(const char *query, uint64 queryId,
 					   const struct JitInstrumentation *jitusage,
 					   JumbleState *jstate,
 					   int parallel_workers_to_launch,
-					   int parallel_workers_launched);
+					   int parallel_workers_launched,
+					   CachedPlan *cplan);
 static void pg_stat_statements_internal(FunctionCallInfo fcinfo,
 										pgssVersion api_version,
 										bool showtext);
@@ -877,7 +883,8 @@ pgss_post_parse_analyze(ParseState *pstate, Query *query, JumbleState *jstate)
 				   NULL,
 				   jstate,
 				   0,
-				   0);
+				   0,
+				   NULL);
 }
 
 /*
@@ -957,7 +964,8 @@ pgss_planner(Query *parse,
 				   NULL,
 				   NULL,
 				   0,
-				   0);
+				   0,
+				   NULL);
 	}
 	else
 	{
@@ -1099,7 +1107,8 @@ pgss_ExecutorEnd(QueryDesc *queryDesc)
 				   queryDesc->estate->es_jit ? &queryDesc->estate->es_jit->instr : NULL,
 				   NULL,
 				   queryDesc->estate->es_parallel_workers_to_launch,
-				   queryDesc->estate->es_parallel_workers_launched);
+				   queryDesc->estate->es_parallel_workers_launched,
+				   queryDesc->cplan);
 	}
 
 	if (prev_ExecutorEnd)
@@ -1232,7 +1241,8 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
 				   NULL,
 				   NULL,
 				   0,
-				   0);
+				   0,
+				   NULL);
 	}
 	else
 	{
@@ -1295,7 +1305,8 @@ pgss_store(const char *query, uint64 queryId,
 		   const struct JitInstrumentation *jitusage,
 		   JumbleState *jstate,
 		   int parallel_workers_to_launch,
-		   int parallel_workers_launched)
+		   int parallel_workers_launched,
+		   CachedPlan *cplan)
 {
 	pgssHashKey key;
 	pgssEntry  *entry;
@@ -1503,6 +1514,15 @@ pgss_store(const char *query, uint64 queryId,
 		entry->counters.parallel_workers_to_launch += parallel_workers_to_launch;
 		entry->counters.parallel_workers_launched += parallel_workers_launched;
 
+		if (cplan)
+		{
+			if (cplan->status == PLAN_CACHE_STATUS_GENERIC_PLAN_BUILD ||
+				cplan->status == PLAN_CACHE_STATUS_GENERIC_PLAN_REUSE)
+				entry->counters.generic_plan_calls++;
+			if (cplan->status == PLAN_CACHE_STATUS_CUSTOM_PLAN)
+				entry->counters.custom_plan_calls++;
+		}
+
 		SpinLockRelease(&entry->mutex);
 	}
 
@@ -1570,7 +1590,8 @@ pg_stat_statements_reset(PG_FUNCTION_ARGS)
 #define PG_STAT_STATEMENTS_COLS_V1_10	43
 #define PG_STAT_STATEMENTS_COLS_V1_11	49
 #define PG_STAT_STATEMENTS_COLS_V1_12	52
-#define PG_STAT_STATEMENTS_COLS			52	/* maximum of above */
+#define PG_STAT_STATEMENTS_COLS_V1_13	54
+#define PG_STAT_STATEMENTS_COLS			54	/* maximum of above */
 
 /*
  * Retrieve statement statistics.
@@ -1582,6 +1603,16 @@ pg_stat_statements_reset(PG_FUNCTION_ARGS)
  * expected API version is identified by embedding it in the C name of the
  * function.  Unfortunately we weren't bright enough to do that for 1.1.
  */
+Datum
+pg_stat_statements_1_13(PG_FUNCTION_ARGS)
+{
+	bool		showtext = PG_GETARG_BOOL(0);
+
+	pg_stat_statements_internal(fcinfo, PGSS_V1_13, showtext);
+
+	return (Datum) 0;
+}
+
 Datum
 pg_stat_statements_1_12(PG_FUNCTION_ARGS)
 {
@@ -1740,6 +1771,10 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo,
 			if (api_version != PGSS_V1_12)
 				elog(ERROR, "incorrect number of output arguments");
 			break;
+		case PG_STAT_STATEMENTS_COLS_V1_13:
+			if (api_version != PGSS_V1_13)
+				elog(ERROR, "incorrect number of output arguments");
+			break;
 		default:
 			elog(ERROR, "incorrect number of output arguments");
 	}
@@ -1997,6 +2032,11 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo,
 			values[i++] = TimestampTzGetDatum(stats_since);
 			values[i++] = TimestampTzGetDatum(minmax_stats_since);
 		}
+		if (api_version >= PGSS_V1_13)
+		{
+			values[i++] = Int64GetDatumFast(tmp.generic_plan_calls);
+			values[i++] = Int64GetDatumFast(tmp.custom_plan_calls);
+		}
 
 		Assert(i == (api_version == PGSS_V1_0 ? PG_STAT_STATEMENTS_COLS_V1_0 :
 					 api_version == PGSS_V1_1 ? PG_STAT_STATEMENTS_COLS_V1_1 :
@@ -2007,6 +2047,7 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo,
 					 api_version == PGSS_V1_10 ? PG_STAT_STATEMENTS_COLS_V1_10 :
 					 api_version == PGSS_V1_11 ? PG_STAT_STATEMENTS_COLS_V1_11 :
 					 api_version == PGSS_V1_12 ? PG_STAT_STATEMENTS_COLS_V1_12 :
+					 api_version == PGSS_V1_13 ? PG_STAT_STATEMENTS_COLS_V1_13 :
 					 -1 /* fail if you forget to update this assert */ ));
 
 		tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
diff --git a/contrib/pg_stat_statements/pg_stat_statements.control b/contrib/pg_stat_statements/pg_stat_statements.control
index d45ebc12e36..2eee0ceffa8 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.control
+++ b/contrib/pg_stat_statements/pg_stat_statements.control
@@ -1,5 +1,5 @@
 # pg_stat_statements extension
 comment = 'track planning and execution statistics of all SQL statements executed'
-default_version = '1.12'
+default_version = '1.13'
 module_pathname = '$libdir/pg_stat_statements'
 relocatable = true
diff --git a/contrib/pg_stat_statements/sql/plan_cache.sql b/contrib/pg_stat_statements/sql/plan_cache.sql
new file mode 100644
index 00000000000..729ca488da2
--- /dev/null
+++ b/contrib/pg_stat_statements/sql/plan_cache.sql
@@ -0,0 +1,50 @@
+--
+-- Information related to plan cache
+--
+
+SELECT pg_stat_statements_reset() IS NOT NULL AS t;
+
+-- plan cache counters for prepared statements
+PREPARE p1 AS SELECT $1;
+-- plan cache auto
+SET plan_cache_mode TO auto;
+EXECUTE p1(1);
+-- force generic plan
+SET plan_cache_mode TO force_generic_plan;
+EXECUTE p1(1);
+-- force custom plan
+SET plan_cache_mode TO force_custom_plan;
+EXECUTE p1(1);
+
+-- plan cache counters for functions and procedures
+SET pg_stat_statements.track = 'all';
+CREATE OR REPLACE FUNCTION select_one_func(int) RETURNS VOID AS $$
+DECLARE
+    ret INT;
+BEGIN
+    SELECT $1 INTO ret;
+END;
+$$ LANGUAGE plpgsql;
+CREATE OR REPLACE PROCEDURE select_one_proc(int) AS $$
+DECLARE
+    ret INT;
+BEGIN
+    select $1 INTO ret;
+END;
+$$ LANGUAGE plpgsql;
+-- plan cache auto
+SET plan_cache_mode TO auto;
+SELECT select_one_func(1);
+CALL select_one_proc(1);
+-- force generic plan
+SET plan_cache_mode TO force_generic_plan;
+SELECT select_one_func(1);
+CALL select_one_proc(1);
+-- force custom plan
+SET plan_cache_mode TO force_custom_plan;
+SELECT select_one_func(1);
+CALL select_one_proc(1);
+
+-- get the plan cache counters
+SELECT calls, generic_plan_calls, custom_plan_calls, toplevel, calls, query FROM pg_stat_statements
+  WHERE query = 'SELECT $1' ORDER BY query COLLATE "C";
diff --git a/doc/src/sgml/pgstatstatements.sgml b/doc/src/sgml/pgstatstatements.sgml
index 7baa07dcdbf..cc1e0a1c00a 100644
--- a/doc/src/sgml/pgstatstatements.sgml
+++ b/doc/src/sgml/pgstatstatements.sgml
@@ -575,6 +575,24 @@
        <structfield>max_exec_time</structfield>)
       </para></entry>
      </row>
+
+     <row>
+      <entry role="catalog_table_entry"><para role="column_definition">
+       <structfield>generic_plan_calls</structfield> <type>bigint</type>
+      </para>
+      <para>
+       Total number of statements executed using a generic plan
+      </para></entry>
+     </row>
+
+     <row>
+      <entry role="catalog_table_entry"><para role="column_definition">
+       <structfield>custom_plan_calls</structfield> <type>bigint</type>
+      </para>
+      <para>
+       Total number of statements executed using a custom plan
+      </para></entry>
+     </row>
     </tbody>
    </tgroup>
   </table>
diff --git a/src/backend/utils/cache/plancache.c b/src/backend/utils/cache/plancache.c
index 3b681647060..ee9636f6352 100644
--- a/src/backend/utils/cache/plancache.c
+++ b/src/backend/utils/cache/plancache.c
@@ -1168,7 +1168,7 @@ BuildCachedPlan(CachedPlanSource *plansource, List *qlist,
 	plan->stmt_context = stmt_context;
 	plan->is_oneshot = plansource->is_oneshot;
 	plan->is_saved = false;
-	plan->is_reused = false;
+	plan->status = PLAN_CACHE_STATUS_UNKNOWN;
 	plan->is_valid = true;
 
 	/* assign generation number to new plan */
@@ -1406,7 +1406,8 @@ cached_plan_cost(CachedPlan *plan, bool include_planner)
  * locks are acquired. In such cases, CheckCachedPlan() does not take locks
  * on relations subject to initial runtime pruning; instead, these locks are
  * deferred until execution startup, when ExecDoInitialPruning() performs
- * initial pruning.  The plan's "is_reused" flag is set to indicate that
+ * initial pruning.  The plan's "status" flag is set to
+ * PLAN_CACHE_STATUS_GENERIC_PLAN_REUSE to indicate that
  * CachedPlanRequiresLocking() should return true when called by
  * ExecDoInitialPruning().
  *
@@ -1447,7 +1448,7 @@ GetCachedPlan(CachedPlanSource *plansource, ParamListInfo boundParams,
 			plan = plansource->gplan;
 			Assert(plan->magic == CACHEDPLAN_MAGIC);
 			/* Reusing the existing plan, so not all locks may be acquired. */
-			plan->is_reused = true;
+			plan->status = PLAN_CACHE_STATUS_GENERIC_PLAN_REUSE;
 		}
 		else
 		{
@@ -1491,6 +1492,8 @@ GetCachedPlan(CachedPlanSource *plansource, ParamListInfo boundParams,
 			 * BuildCachedPlan to do that by passing NIL.
 			 */
 			qlist = NIL;
+
+			plan->status = PLAN_CACHE_STATUS_GENERIC_PLAN_BUILD;
 		}
 	}
 
@@ -1502,6 +1505,7 @@ GetCachedPlan(CachedPlanSource *plansource, ParamListInfo boundParams,
 		plansource->total_custom_cost += cached_plan_cost(plan, true);
 
 		plansource->num_custom_plans++;
+		plan->status = PLAN_CACHE_STATUS_CUSTOM_PLAN;
 	}
 	else
 	{
diff --git a/src/include/utils/plancache.h b/src/include/utils/plancache.h
index 07ec5318db7..5ff3ea2e1f2 100644
--- a/src/include/utils/plancache.h
+++ b/src/include/utils/plancache.h
@@ -37,6 +37,14 @@ typedef enum
 	PLAN_CACHE_MODE_FORCE_CUSTOM_PLAN,
 }			PlanCacheMode;
 
+typedef enum
+{
+	PLAN_CACHE_STATUS_UNKNOWN = 0,
+	PLAN_CACHE_STATUS_CUSTOM_PLAN,
+	PLAN_CACHE_STATUS_GENERIC_PLAN_BUILD,
+	PLAN_CACHE_STATUS_GENERIC_PLAN_REUSE
+}			PlanCacheStatus;
+
 /* GUC parameter */
 extern PGDLLIMPORT int plan_cache_mode;
 
@@ -165,7 +173,7 @@ typedef struct CachedPlan
 	List	   *stmt_list;		/* list of PlannedStmts */
 	bool		is_oneshot;		/* is it a "oneshot" plan? */
 	bool		is_saved;		/* is CachedPlan in a long-lived context? */
-	bool		is_reused;		/* is it a reused generic plan? */
+	PlanCacheStatus status;		/* status of the cached plan */
 	bool		is_valid;		/* is the stmt_list currently valid? */
 	Oid			planRoleId;		/* Role ID the plan was created for */
 	bool		dependsOnRole;	/* is plan specific to that role? */
@@ -275,7 +283,7 @@ extern void FreeCachedExpression(CachedExpression *cexpr);
 static inline bool
 CachedPlanRequiresLocking(CachedPlan *cplan)
 {
-	return !cplan->is_oneshot && cplan->is_reused;
+	return !cplan->is_oneshot && (cplan->status == PLAN_CACHE_STATUS_GENERIC_PLAN_REUSE);
 }
 
 /*
-- 
2.39.5 (Apple Git-154)

