v12-0001-Add-counters-for-generic-and-custom-plan-executi.patch
application/octet-stream
Filename: v12-0001-Add-counters-for-generic-and-custom-plan-executi.patch
Type: application/octet-stream
Part: 0
Patch
Format: format-patch
Series: patch v12-0001
Subject: Add counters for generic and custom plan executions to pg_stat_statements
| File | + | − |
|---|---|---|
| contrib/pg_stat_statements/expected/plancache.out | 295 | 0 |
| contrib/pg_stat_statements/Makefile | 2 | 1 |
| contrib/pg_stat_statements/meson.build | 2 | 0 |
| contrib/pg_stat_statements/pg_stat_statements--1.12--1.13.sql | 78 | 0 |
| contrib/pg_stat_statements/pg_stat_statements.c | 60 | 7 |
| contrib/pg_stat_statements/pg_stat_statements.control | 1 | 1 |
| contrib/pg_stat_statements/sql/plancache.sql | 129 | 0 |
| doc/src/sgml/pgstatstatements.sgml | 18 | 0 |
| src/backend/commands/explain.c | 9 | 2 |
| src/backend/commands/prepare.c | 1 | 1 |
| src/backend/executor/execUtils.c | 3 | 0 |
| src/backend/executor/functions.c | 6 | 0 |
| src/backend/executor/spi.c | 11 | 3 |
| src/backend/tcop/pquery.c | 17 | 4 |
| src/backend/utils/cache/plancache.c | 3 | 0 |
| src/include/commands/explain.h | 3 | 1 |
| src/include/nodes/execnodes.h | 4 | 0 |
| src/include/utils/plancache.h | 1 | 0 |
From 1508510c47e8c5189db5efc7feb2afbc91acfcdb Mon Sep 17 00:00:00 2001
From: Sami Imseih <simseih@amazon.com>
Date: Mon, 21 Jul 2025 14:43:01 -0500
Subject: [PATCH v12 1/1] Add counters for generic and custom plan executions
to pg_stat_statements
This patch adds two new counters to pg_stat_statements:
- generic_plan_calls
- custom_plan_calls
These track how many times a prepared statement was executed using a
generic or custom plan, respectively.
To support this, two new fields are added to QueryDesc->estate:
- es_cached_plan
- es_is_generic_plan
These fields are set when a CachedPlan is used, with es_cached_plan
set to true and es_is_generic_plan set to cplan->is_generic_plan.
A new is_generic_plan field is also added to CachedPlan and set
during GetCachedPlan.
---
contrib/pg_stat_statements/Makefile | 3 +-
.../pg_stat_statements/expected/plancache.out | 295 ++++++++++++++++++
contrib/pg_stat_statements/meson.build | 2 +
.../pg_stat_statements--1.12--1.13.sql | 78 +++++
.../pg_stat_statements/pg_stat_statements.c | 67 +++-
.../pg_stat_statements.control | 2 +-
contrib/pg_stat_statements/sql/plancache.sql | 129 ++++++++
doc/src/sgml/pgstatstatements.sgml | 18 ++
src/backend/commands/explain.c | 11 +-
src/backend/commands/prepare.c | 2 +-
src/backend/executor/execUtils.c | 3 +
src/backend/executor/functions.c | 6 +
src/backend/executor/spi.c | 14 +-
src/backend/tcop/pquery.c | 21 +-
src/backend/utils/cache/plancache.c | 3 +
src/include/commands/explain.h | 4 +-
src/include/nodes/execnodes.h | 4 +
src/include/utils/plancache.h | 1 +
18 files changed, 643 insertions(+), 20 deletions(-)
create mode 100644 contrib/pg_stat_statements/expected/plancache.out
create mode 100644 contrib/pg_stat_statements/pg_stat_statements--1.12--1.13.sql
create mode 100644 contrib/pg_stat_statements/sql/plancache.sql
diff --git a/contrib/pg_stat_statements/Makefile b/contrib/pg_stat_statements/Makefile
index b2bd8794d2a..996a5fac448 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 plancache
# 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/plancache.out b/contrib/pg_stat_statements/expected/plancache.out
new file mode 100644
index 00000000000..7b05d5a425f
--- /dev/null
+++ b/contrib/pg_stat_statements/expected/plancache.out
@@ -0,0 +1,295 @@
+--
+-- Information related to plan cache
+--
+--
+-- Setup
+--
+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 counters for prepared statements
+--
+SELECT pg_stat_statements_reset() IS NOT NULL AS t;
+ t
+---
+ t
+(1 row)
+
+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)
+
+SELECT calls, generic_plan_calls, custom_plan_calls, toplevel, query FROM pg_stat_statements
+ ORDER BY query COLLATE "C";
+ calls | generic_plan_calls | custom_plan_calls | toplevel | query
+-------+--------------------+-------------------+----------+----------------------------------------------------
+ 3 | 1 | 2 | t | PREPARE p1 AS SELECT $1
+ 1 | 0 | 0 | t | SELECT pg_stat_statements_reset() IS NOT NULL AS t
+ 3 | 0 | 0 | t | SET plan_cache_mode TO $1
+(3 rows)
+
+DEALLOCATE p1;
+--
+-- plan cache counters for extended query protocol
+--
+SELECT pg_stat_statements_reset() IS NOT NULL AS t;
+ t
+---
+ t
+(1 row)
+
+SELECT $1 \parse p1
+SET plan_cache_mode TO auto;
+\bind_named p1 1
+;
+ ?column?
+----------
+ 1
+(1 row)
+
+SET plan_cache_mode TO force_generic_plan;
+\bind_named p1 1
+;
+ ?column?
+----------
+ 1
+(1 row)
+
+SET plan_cache_mode TO force_custom_plan;
+\bind_named p1 1
+;
+ ?column?
+----------
+ 1
+(1 row)
+
+SELECT calls, generic_plan_calls, custom_plan_calls, toplevel, query FROM pg_stat_statements
+ ORDER BY query COLLATE "C";
+ calls | generic_plan_calls | custom_plan_calls | toplevel | query
+-------+--------------------+-------------------+----------+----------------------------------------------------
+ 3 | 1 | 2 | t | SELECT $1
+ 1 | 0 | 0 | t | SELECT pg_stat_statements_reset() IS NOT NULL AS t
+ 3 | 0 | 0 | t | SET plan_cache_mode TO $1
+(3 rows)
+
+\close_prepared p1
+--
+-- plan cache counters for explain|explain (analyze) with prepared statements
+--
+SET pg_stat_statements.track = 'all';
+SELECT pg_stat_statements_reset() IS NOT NULL AS t;
+ t
+---
+ t
+(1 row)
+
+PREPARE p1 AS SELECT $1;
+-- plan cache auto
+SET plan_cache_mode TO auto;
+EXPLAIN (COSTS OFF) EXECUTE p1(1);
+ QUERY PLAN
+------------
+ Result
+(1 row)
+
+EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, BUFFERS OFF) EXECUTE p1(1);
+ QUERY PLAN
+-----------------------------------
+ Result (actual rows=1.00 loops=1)
+(1 row)
+
+-- force generic plan
+SET plan_cache_mode TO force_generic_plan;
+EXPLAIN (COSTS OFF) EXECUTE p1(1);
+ QUERY PLAN
+------------
+ Result
+(1 row)
+
+EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, BUFFERS OFF) EXECUTE p1(1);
+ QUERY PLAN
+-----------------------------------
+ Result (actual rows=1.00 loops=1)
+(1 row)
+
+-- force custom plan
+SET plan_cache_mode TO force_custom_plan;
+EXPLAIN (COSTS OFF) EXECUTE p1(1);
+ QUERY PLAN
+------------
+ Result
+(1 row)
+
+EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, BUFFERS OFF) EXECUTE p1(1);
+ QUERY PLAN
+-----------------------------------
+ Result (actual rows=1.00 loops=1)
+(1 row)
+
+SELECT calls, generic_plan_calls, custom_plan_calls, toplevel, query FROM pg_stat_statements
+ ORDER BY query COLLATE "C";
+ calls | generic_plan_calls | custom_plan_calls | toplevel | query
+-------+--------------------+-------------------+----------+----------------------------------------------------------------------------------
+ 3 | 0 | 0 | t | EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, BUFFERS OFF) EXECUTE p1(1)
+ 3 | 0 | 0 | t | EXPLAIN (COSTS OFF) EXECUTE p1(1)
+ 6 | 2 | 4 | f | PREPARE p1 AS SELECT $1
+ 1 | 0 | 0 | t | SELECT pg_stat_statements_reset() IS NOT NULL AS t
+ 3 | 0 | 0 | t | SET plan_cache_mode TO $1
+(5 rows)
+
+RESET pg_stat_statements.track;
+DEALLOCATE p1;
+--
+-- plan cache counters for functions and procedures
+--
+SET pg_stat_statements.track = 'all';
+SELECT pg_stat_statements_reset() IS NOT NULL AS t;
+ t
+---
+ t
+(1 row)
+
+-- 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);
+SELECT calls, generic_plan_calls, custom_plan_calls, toplevel, query FROM pg_stat_statements
+ ORDER BY query COLLATE "C";
+ calls | generic_plan_calls | custom_plan_calls | toplevel | query
+-------+--------------------+-------------------+----------+----------------------------------------------------
+ 3 | 0 | 0 | t | CALL select_one_proc($1)
+ 6 | 2 | 4 | f | SELECT $1
+ 1 | 0 | 0 | t | SELECT pg_stat_statements_reset() IS NOT NULL AS t
+ 3 | 0 | 0 | t | SELECT select_one_func($1)
+ 3 | 0 | 0 | t | SET plan_cache_mode TO $1
+(5 rows)
+
+--
+-- plan cache counters for explain|explain (analyze) with functions and procedures
+--
+SET pg_stat_statements.track = 'all';
+SELECT pg_stat_statements_reset() IS NOT NULL AS t;
+ t
+---
+ t
+(1 row)
+
+-- plan cache auto
+SET plan_cache_mode TO auto;
+EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, BUFFERS OFF) SELECT select_one_func(1);
+ QUERY PLAN
+-----------------------------------
+ Result (actual rows=1.00 loops=1)
+(1 row)
+
+EXPLAIN (COSTS OFF) SELECT select_one_func(1);
+ QUERY PLAN
+------------
+ Result
+(1 row)
+
+CALL select_one_proc(1);
+-- force generic plan
+SET plan_cache_mode TO force_generic_plan;
+EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, BUFFERS OFF) SELECT select_one_func(1);
+ QUERY PLAN
+-----------------------------------
+ Result (actual rows=1.00 loops=1)
+(1 row)
+
+EXPLAIN (COSTS OFF) SELECT select_one_func(1);
+ QUERY PLAN
+------------
+ Result
+(1 row)
+
+CALL select_one_proc(1);
+-- force custom plan
+SET plan_cache_mode TO force_custom_plan;
+EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, BUFFERS OFF) SELECT select_one_func(1);
+ QUERY PLAN
+-----------------------------------
+ Result (actual rows=1.00 loops=1)
+(1 row)
+
+EXPLAIN (COSTS OFF) SELECT select_one_func(1);
+ QUERY PLAN
+------------
+ Result
+(1 row)
+
+CALL select_one_proc(1);
+SELECT calls, generic_plan_calls, custom_plan_calls, toplevel, query FROM pg_stat_statements
+ ORDER BY query COLLATE "C";
+ calls | generic_plan_calls | custom_plan_calls | toplevel | query
+-------+--------------------+-------------------+----------+------------------------------------------------------------------------------------------------
+ 3 | 0 | 0 | t | CALL select_one_proc($1)
+ 3 | 0 | 0 | t | EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, BUFFERS OFF) SELECT select_one_func($1)
+ 6 | 0 | 0 | f | EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, BUFFERS OFF) SELECT select_one_func($1);
+ 3 | 0 | 0 | t | EXPLAIN (COSTS OFF) SELECT select_one_func($1)
+ 6 | 2 | 4 | f | SELECT $1
+ 1 | 0 | 0 | t | SELECT pg_stat_statements_reset() IS NOT NULL AS t
+ 3 | 0 | 0 | t | SET plan_cache_mode TO $1
+(7 rows)
+
+--
+-- Cleanup
+--
+DROP FUNCTION select_one_func(int);
+DROP PROCEDURE select_one_proc(int);
diff --git a/contrib/pg_stat_statements/meson.build b/contrib/pg_stat_statements/meson.build
index 01a6cbdcf61..110fb82fe12 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',
+ 'plancache',
],
'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..2f0eaf14ec3
--- /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 generic_plan_calls int8,
+ OUT custom_plan_calls int8,
+ OUT stats_since timestamp with time zone,
+ OUT minmax_stats_since timestamp with time zone
+)
+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 e7857f81ec0..9fea390bc29 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -114,6 +114,7 @@ typedef enum pgssVersion
PGSS_V1_10,
PGSS_V1_11,
PGSS_V1_12,
+ PGSS_V1_13,
} pgssVersion;
typedef enum pgssStoreKind
@@ -131,6 +132,13 @@ typedef enum pgssStoreKind
#define PGSS_NUMKIND (PGSS_EXEC + 1)
+typedef enum pgssCachedPlanMode
+{
+ PGSS_PLAN_CACHE_MODE_INVALID = 0,
+ PGSS_PLAN_CACHE_MODE_GENERIC = 1,
+ PGSS_PLAN_CACHE_MODE_CUSTOM = 2,
+} pgssCachedPlanMode;
+
/*
* Hashtable key that defines the identity of a hashtable entry. We separate
* queries by user and by database even if they are otherwise identical.
@@ -210,6 +218,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 +333,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 +366,8 @@ static void pgss_store(const char *query, int64 queryId,
const struct JitInstrumentation *jitusage,
JumbleState *jstate,
int parallel_workers_to_launch,
- int parallel_workers_launched);
+ int parallel_workers_launched,
+ pgssCachedPlanMode plan_cache_mode);
static void pg_stat_statements_internal(FunctionCallInfo fcinfo,
pgssVersion api_version,
bool showtext);
@@ -877,7 +889,8 @@ pgss_post_parse_analyze(ParseState *pstate, Query *query, JumbleState *jstate)
NULL,
jstate,
0,
- 0);
+ 0,
+ PGSS_PLAN_CACHE_MODE_INVALID);
}
/*
@@ -957,7 +970,8 @@ pgss_planner(Query *parse,
NULL,
NULL,
0,
- 0);
+ 0,
+ PGSS_PLAN_CACHE_MODE_INVALID);
}
else
{
@@ -1069,10 +1083,17 @@ static void
pgss_ExecutorEnd(QueryDesc *queryDesc)
{
int64 queryId = queryDesc->plannedstmt->queryId;
+ pgssCachedPlanMode plan_cache_mode = PGSS_PLAN_CACHE_MODE_INVALID;
if (queryId != INT64CONST(0) && queryDesc->totaltime &&
pgss_enabled(nesting_level))
{
+ /* set plan cache mode */
+ if (queryDesc->estate->es_cached_plan)
+ plan_cache_mode = queryDesc->estate->es_is_generic_plan ?
+ PGSS_PLAN_CACHE_MODE_GENERIC :
+ PGSS_PLAN_CACHE_MODE_CUSTOM;
+
/*
* Make sure stats accumulation is done. (Note: it's okay if several
* levels of hook all do this.)
@@ -1091,7 +1112,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,
+ plan_cache_mode);
}
if (prev_ExecutorEnd)
@@ -1224,7 +1246,8 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
NULL,
NULL,
0,
- 0);
+ 0,
+ PGSS_PLAN_CACHE_MODE_INVALID);
}
else
{
@@ -1287,7 +1310,8 @@ pgss_store(const char *query, int64 queryId,
const struct JitInstrumentation *jitusage,
JumbleState *jstate,
int parallel_workers_to_launch,
- int parallel_workers_launched)
+ int parallel_workers_launched,
+ pgssCachedPlanMode plan_cache_mode)
{
pgssHashKey key;
pgssEntry *entry;
@@ -1495,6 +1519,14 @@ pgss_store(const char *query, int64 queryId,
entry->counters.parallel_workers_to_launch += parallel_workers_to_launch;
entry->counters.parallel_workers_launched += parallel_workers_launched;
+ if (plan_cache_mode > PGSS_PLAN_CACHE_MODE_INVALID)
+ {
+ if (plan_cache_mode == PGSS_PLAN_CACHE_MODE_GENERIC)
+ entry->counters.generic_plan_calls++;
+ else
+ entry->counters.custom_plan_calls++;
+ }
+
SpinLockRelease(&entry->mutex);
}
@@ -1562,7 +1594,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.
@@ -1574,6 +1607,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)
{
@@ -1732,6 +1775,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");
}
@@ -1984,6 +2031,11 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo,
values[i++] = Int64GetDatumFast(tmp.parallel_workers_to_launch);
values[i++] = Int64GetDatumFast(tmp.parallel_workers_launched);
}
+ if (api_version >= PGSS_V1_13)
+ {
+ values[i++] = Int64GetDatumFast(tmp.generic_plan_calls);
+ values[i++] = Int64GetDatumFast(tmp.custom_plan_calls);
+ }
if (api_version >= PGSS_V1_11)
{
values[i++] = TimestampTzGetDatum(stats_since);
@@ -1999,6 +2051,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/plancache.sql b/contrib/pg_stat_statements/sql/plancache.sql
new file mode 100644
index 00000000000..f3878889ea6
--- /dev/null
+++ b/contrib/pg_stat_statements/sql/plancache.sql
@@ -0,0 +1,129 @@
+--
+-- Information related to plan cache
+--
+
+--
+-- Setup
+--
+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 counters for prepared statements
+--
+SELECT pg_stat_statements_reset() IS NOT NULL AS t;
+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);
+SELECT calls, generic_plan_calls, custom_plan_calls, toplevel, query FROM pg_stat_statements
+ ORDER BY query COLLATE "C";
+DEALLOCATE p1;
+
+--
+-- plan cache counters for extended query protocol
+--
+SELECT pg_stat_statements_reset() IS NOT NULL AS t;
+SELECT $1 \parse p1
+SET plan_cache_mode TO auto;
+\bind_named p1 1
+;
+SET plan_cache_mode TO force_generic_plan;
+\bind_named p1 1
+;
+SET plan_cache_mode TO force_custom_plan;
+\bind_named p1 1
+;
+SELECT calls, generic_plan_calls, custom_plan_calls, toplevel, query FROM pg_stat_statements
+ ORDER BY query COLLATE "C";
+\close_prepared p1
+
+--
+-- plan cache counters for explain|explain (analyze) with prepared statements
+--
+SET pg_stat_statements.track = 'all';
+SELECT pg_stat_statements_reset() IS NOT NULL AS t;
+PREPARE p1 AS SELECT $1;
+-- plan cache auto
+SET plan_cache_mode TO auto;
+EXPLAIN (COSTS OFF) EXECUTE p1(1);
+EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, BUFFERS OFF) EXECUTE p1(1);
+-- force generic plan
+SET plan_cache_mode TO force_generic_plan;
+EXPLAIN (COSTS OFF) EXECUTE p1(1);
+EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, BUFFERS OFF) EXECUTE p1(1);
+-- force custom plan
+SET plan_cache_mode TO force_custom_plan;
+EXPLAIN (COSTS OFF) EXECUTE p1(1);
+EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, BUFFERS OFF) EXECUTE p1(1);
+SELECT calls, generic_plan_calls, custom_plan_calls, toplevel, query FROM pg_stat_statements
+ ORDER BY query COLLATE "C";
+RESET pg_stat_statements.track;
+DEALLOCATE p1;
+
+--
+-- plan cache counters for functions and procedures
+--
+SET pg_stat_statements.track = 'all';
+SELECT pg_stat_statements_reset() IS NOT NULL AS t;
+-- 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);
+SELECT calls, generic_plan_calls, custom_plan_calls, toplevel, query FROM pg_stat_statements
+ ORDER BY query COLLATE "C";
+
+--
+-- plan cache counters for explain|explain (analyze) with functions and procedures
+--
+SET pg_stat_statements.track = 'all';
+SELECT pg_stat_statements_reset() IS NOT NULL AS t;
+-- plan cache auto
+SET plan_cache_mode TO auto;
+EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, BUFFERS OFF) SELECT select_one_func(1);
+EXPLAIN (COSTS OFF) SELECT select_one_func(1);
+CALL select_one_proc(1);
+-- force generic plan
+SET plan_cache_mode TO force_generic_plan;
+EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, BUFFERS OFF) SELECT select_one_func(1);
+EXPLAIN (COSTS OFF) SELECT select_one_func(1);
+CALL select_one_proc(1);
+-- force custom plan
+SET plan_cache_mode TO force_custom_plan;
+EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, BUFFERS OFF) SELECT select_one_func(1);
+EXPLAIN (COSTS OFF) SELECT select_one_func(1);
+CALL select_one_proc(1);
+SELECT calls, generic_plan_calls, custom_plan_calls, toplevel, query FROM pg_stat_statements
+ ORDER BY query COLLATE "C";
+
+--
+-- Cleanup
+--
+DROP FUNCTION select_one_func(int);
+DROP PROCEDURE select_one_proc(int);
diff --git a/doc/src/sgml/pgstatstatements.sgml b/doc/src/sgml/pgstatstatements.sgml
index 7baa07dcdbf..08b5fec9e10 100644
--- a/doc/src/sgml/pgstatstatements.sgml
+++ b/doc/src/sgml/pgstatstatements.sgml
@@ -554,6 +554,24 @@
</para></entry>
</row>
+ <row>
+ <entry role="catalog_table_entry"><para role="column_definition">
+ <structfield>generic_plan_calls</structfield> <type>bigint</type>
+ </para>
+ <para>
+ Number of times the statement was 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>
+ Number of times the statement was executed using a custom plan
+ </para></entry>
+ </row>
+
<row>
<entry role="catalog_table_entry"><para role="column_definition">
<structfield>stats_since</structfield> <type>timestamp with time zone</type>
diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c
index 7e2792ead71..49109290719 100644
--- a/src/backend/commands/explain.c
+++ b/src/backend/commands/explain.c
@@ -371,7 +371,7 @@ standard_ExplainOneQuery(Query *query, int cursorOptions,
/* run it (if needed) and produce output */
ExplainOnePlan(plan, into, es, queryString, params, queryEnv,
&planduration, (es->buffers ? &bufusage : NULL),
- es->memory ? &mem_counters : NULL);
+ es->memory ? &mem_counters : NULL, NULL);
}
/*
@@ -495,7 +495,8 @@ ExplainOnePlan(PlannedStmt *plannedstmt, IntoClause *into, ExplainState *es,
const char *queryString, ParamListInfo params,
QueryEnvironment *queryEnv, const instr_time *planduration,
const BufferUsage *bufusage,
- const MemoryContextCounters *mem_counters)
+ const MemoryContextCounters *mem_counters,
+ CachedPlan *cplan)
{
DestReceiver *dest;
QueryDesc *queryDesc;
@@ -564,6 +565,12 @@ ExplainOnePlan(PlannedStmt *plannedstmt, IntoClause *into, ExplainState *es,
/* call ExecutorStart to prepare the plan for execution */
ExecutorStart(queryDesc, eflags);
+ if (cplan)
+ {
+ queryDesc->estate->es_cached_plan = true;
+ queryDesc->estate->es_is_generic_plan = cplan->is_generic_plan;
+ }
+
/* Execute the plan for statistics if asked for */
if (es->analyze)
{
diff --git a/src/backend/commands/prepare.c b/src/backend/commands/prepare.c
index 34b6410d6a2..55e08b2a63f 100644
--- a/src/backend/commands/prepare.c
+++ b/src/backend/commands/prepare.c
@@ -659,7 +659,7 @@ ExplainExecuteQuery(ExecuteStmt *execstmt, IntoClause *into, ExplainState *es,
if (pstmt->commandType != CMD_UTILITY)
ExplainOnePlan(pstmt, into, es, query_string, paramLI, pstate->p_queryEnv,
&planduration, (es->buffers ? &bufusage : NULL),
- es->memory ? &mem_counters : NULL);
+ es->memory ? &mem_counters : NULL, cplan);
else
ExplainOneUtility(pstmt->utilityStmt, into, es, pstate, paramLI);
diff --git a/src/backend/executor/execUtils.c b/src/backend/executor/execUtils.c
index fdc65c2b42b..14d320b8c18 100644
--- a/src/backend/executor/execUtils.c
+++ b/src/backend/executor/execUtils.c
@@ -165,6 +165,9 @@ CreateExecutorState(void)
estate->es_jit_flags = 0;
estate->es_jit = NULL;
+ estate->es_cached_plan = false;
+ estate->es_is_generic_plan = false;
+
/*
* Return the executor state structure
*/
diff --git a/src/backend/executor/functions.c b/src/backend/executor/functions.c
index 359aafea681..1722f142dba 100644
--- a/src/backend/executor/functions.c
+++ b/src/backend/executor/functions.c
@@ -1364,6 +1364,12 @@ postquel_start(execution_state *es, SQLFunctionCachePtr fcache)
else
eflags = 0; /* default run-to-completion flags */
ExecutorStart(es->qd, eflags);
+
+ if (fcache->cplan)
+ {
+ es->qd->estate->es_cached_plan = true;
+ es->qd->estate->es_is_generic_plan = fcache->cplan->is_generic_plan;
+ }
}
es->status = F_EXEC_RUN;
diff --git a/src/backend/executor/spi.c b/src/backend/executor/spi.c
index ecb2e4ccaa1..51401d61de1 100644
--- a/src/backend/executor/spi.c
+++ b/src/backend/executor/spi.c
@@ -70,7 +70,8 @@ static int _SPI_execute_plan(SPIPlanPtr plan, const SPIExecuteOptions *options,
static ParamListInfo _SPI_convert_params(int nargs, Oid *argtypes,
Datum *Values, const char *Nulls);
-static int _SPI_pquery(QueryDesc *queryDesc, bool fire_triggers, uint64 tcount);
+static int _SPI_pquery(QueryDesc *queryDesc, CachedPlan *cplan,
+ bool fire_triggers, uint64 tcount);
static void _SPI_error_callback(void *arg);
@@ -2696,7 +2697,7 @@ _SPI_execute_plan(SPIPlanPtr plan, const SPIExecuteOptions *options,
options->params,
_SPI_current->queryEnv,
0);
- res = _SPI_pquery(qdesc, fire_triggers,
+ res = _SPI_pquery(qdesc, cplan, fire_triggers,
canSetTag ? options->tcount : 0);
FreeQueryDesc(qdesc);
}
@@ -2871,7 +2872,8 @@ _SPI_convert_params(int nargs, Oid *argtypes,
}
static int
-_SPI_pquery(QueryDesc *queryDesc, bool fire_triggers, uint64 tcount)
+_SPI_pquery(QueryDesc *queryDesc, CachedPlan *cplan,
+ bool fire_triggers, uint64 tcount)
{
int operation = queryDesc->operation;
int eflags;
@@ -2929,6 +2931,12 @@ _SPI_pquery(QueryDesc *queryDesc, bool fire_triggers, uint64 tcount)
ExecutorStart(queryDesc, eflags);
+ if (cplan)
+ {
+ queryDesc->estate->es_cached_plan = true;
+ queryDesc->estate->es_is_generic_plan = cplan->is_generic_plan;
+ }
+
ExecutorRun(queryDesc, ForwardScanDirection, tcount);
_SPI_current->processed = queryDesc->estate->es_processed;
diff --git a/src/backend/tcop/pquery.c b/src/backend/tcop/pquery.c
index 08791b8f75e..5d7a52a9532 100644
--- a/src/backend/tcop/pquery.c
+++ b/src/backend/tcop/pquery.c
@@ -41,7 +41,7 @@ static void ProcessQuery(PlannedStmt *plan,
ParamListInfo params,
QueryEnvironment *queryEnv,
DestReceiver *dest,
- QueryCompletion *qc);
+ QueryCompletion *qc, CachedPlan *cplan);
static void FillPortalStore(Portal portal, bool isTopLevel);
static uint64 RunFromStore(Portal portal, ScanDirection direction, uint64 count,
DestReceiver *dest);
@@ -139,7 +139,8 @@ ProcessQuery(PlannedStmt *plan,
ParamListInfo params,
QueryEnvironment *queryEnv,
DestReceiver *dest,
- QueryCompletion *qc)
+ QueryCompletion *qc,
+ CachedPlan *cplan)
{
QueryDesc *queryDesc;
@@ -155,6 +156,12 @@ ProcessQuery(PlannedStmt *plan,
*/
ExecutorStart(queryDesc, 0);
+ if (cplan)
+ {
+ queryDesc->estate->es_cached_plan = true;
+ queryDesc->estate->es_is_generic_plan = cplan->is_generic_plan;
+ }
+
/*
* Run the plan to completion.
*/
@@ -517,6 +524,12 @@ PortalStart(Portal portal, ParamListInfo params,
*/
ExecutorStart(queryDesc, myeflags);
+ if (portal->cplan)
+ {
+ queryDesc->estate->es_cached_plan = true;
+ queryDesc->estate->es_is_generic_plan = portal->cplan->is_generic_plan;
+ }
+
/*
* This tells PortalCleanup to shut down the executor
*/
@@ -1273,7 +1286,7 @@ PortalRunMulti(Portal portal,
portal->sourceText,
portal->portalParams,
portal->queryEnv,
- dest, qc);
+ dest, qc, portal->cplan);
}
else
{
@@ -1282,7 +1295,7 @@ PortalRunMulti(Portal portal,
portal->sourceText,
portal->portalParams,
portal->queryEnv,
- altdest, NULL);
+ altdest, NULL, portal->cplan);
}
if (log_executor_stats)
diff --git a/src/backend/utils/cache/plancache.c b/src/backend/utils/cache/plancache.c
index 89a1c79e984..d6750b8d1bd 100644
--- a/src/backend/utils/cache/plancache.c
+++ b/src/backend/utils/cache/plancache.c
@@ -1140,6 +1140,7 @@ BuildCachedPlan(CachedPlanSource *plansource, List *qlist,
plan->is_oneshot = plansource->is_oneshot;
plan->is_saved = false;
plan->is_valid = true;
+ plan->is_generic_plan = false;
/* assign generation number to new plan */
plan->generation = ++(plansource->generation);
@@ -1358,10 +1359,12 @@ GetCachedPlan(CachedPlanSource *plansource, ParamListInfo boundParams,
plansource->total_custom_cost += cached_plan_cost(plan, true);
plansource->num_custom_plans++;
+ plan->is_generic_plan = false;
}
else
{
plansource->num_generic_plans++;
+ plan->is_generic_plan = true;
}
Assert(plan != NULL);
diff --git a/src/include/commands/explain.h b/src/include/commands/explain.h
index 3b122f79ed8..bf8e5f5d90d 100644
--- a/src/include/commands/explain.h
+++ b/src/include/commands/explain.h
@@ -15,6 +15,7 @@
#include "executor/executor.h"
#include "parser/parse_node.h"
+#include "utils/plancache.h"
struct ExplainState; /* defined in explain_state.h */
@@ -68,7 +69,8 @@ extern void ExplainOnePlan(PlannedStmt *plannedstmt, IntoClause *into,
ParamListInfo params, QueryEnvironment *queryEnv,
const instr_time *planduration,
const BufferUsage *bufusage,
- const MemoryContextCounters *mem_counters);
+ const MemoryContextCounters *mem_counters,
+ CachedPlan *cplan);
extern void ExplainPrintPlan(struct ExplainState *es, QueryDesc *queryDesc);
extern void ExplainPrintTriggers(struct ExplainState *es,
diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.h
index e107d6e5f81..877c7663a04 100644
--- a/src/include/nodes/execnodes.h
+++ b/src/include/nodes/execnodes.h
@@ -774,6 +774,10 @@ typedef struct EState
*/
List *es_insert_pending_result_relations;
List *es_insert_pending_modifytables;
+
+ /* Cached Plan statistics. */
+ bool es_cached_plan;
+ bool es_is_generic_plan;
} EState;
diff --git a/src/include/utils/plancache.h b/src/include/utils/plancache.h
index 1baa6d50bfd..9b3e8994ed9 100644
--- a/src/include/utils/plancache.h
+++ b/src/include/utils/plancache.h
@@ -163,6 +163,7 @@ typedef struct CachedPlan
bool is_oneshot; /* is it a "oneshot" plan? */
bool is_saved; /* is CachedPlan in a long-lived context? */
bool is_valid; /* is the stmt_list currently valid? */
+ bool is_generic_plan; /* is the plan generic */
Oid planRoleId; /* Role ID the plan was created for */
bool dependsOnRole; /* is plan specific to that role? */
TransactionId saved_xmin; /* if valid, replan when TransactionXmin
--
2.39.5 (Apple Git-154)