Skip to content

Commit 4061027

Browse files
author
Commitfest Bot
committed
[CF 5634] v5 - track generic and custom plans in pg_stat_statements
This branch was automatically generated by a robot using patches from an email thread registered at: https://commitfest.postgresql.org/patch/5634 The branch will be overwritten each time a new patch version is posted to the thread, and also periodically to check for bitrot caused by changes on the master branch. Patch(es): https://www.postgresql.org/message-id/CAA5RZ0sxV3APLzmnH1Jt0ehmjzYAStNqcZeEAma_D0VkmjkX8w@mail.gmail.com Author(s): Sami Imseih
2 parents 9d924db + cd698fa commit 4061027

File tree

10 files changed

+303
-14
lines changed

10 files changed

+303
-14
lines changed

contrib/pg_stat_statements/Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ OBJS = \
77

88
EXTENSION = pg_stat_statements
99
DATA = pg_stat_statements--1.4.sql \
10+
pg_stat_statements--1.12--1.13.sql \
1011
pg_stat_statements--1.11--1.12.sql pg_stat_statements--1.10--1.11.sql \
1112
pg_stat_statements--1.9--1.10.sql pg_stat_statements--1.8--1.9.sql \
1213
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))
2021
REGRESS_OPTS = --temp-config $(top_srcdir)/contrib/pg_stat_statements/pg_stat_statements.conf
2122
REGRESS = select dml cursors utility level_tracking planning \
2223
user_activity wal entry_timestamp privileges extended \
23-
parallel cleanup oldextversions squashing
24+
parallel cleanup oldextversions squashing plan_cache
2425
# Disabled because these tests require "shared_preload_libraries=pg_stat_statements",
2526
# which typical installcheck users do not have (e.g. buildfarm clients).
2627
NO_INSTALLCHECK = 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
--
2+
-- Information related to plan cache
3+
--
4+
SELECT pg_stat_statements_reset() IS NOT NULL AS t;
5+
t
6+
---
7+
t
8+
(1 row)
9+
10+
-- plan cache counters for prepared statements
11+
PREPARE p1 AS SELECT $1;
12+
-- plan cache auto
13+
SET plan_cache_mode TO auto;
14+
EXECUTE p1(1);
15+
?column?
16+
----------
17+
1
18+
(1 row)
19+
20+
-- force generic plan
21+
SET plan_cache_mode TO force_generic_plan;
22+
EXECUTE p1(1);
23+
?column?
24+
----------
25+
1
26+
(1 row)
27+
28+
-- force custom plan
29+
SET plan_cache_mode TO force_custom_plan;
30+
EXECUTE p1(1);
31+
?column?
32+
----------
33+
1
34+
(1 row)
35+
36+
-- plan cache counters for functions and procedures
37+
SET pg_stat_statements.track = 'all';
38+
CREATE OR REPLACE FUNCTION select_one_func(int) RETURNS VOID AS $$
39+
DECLARE
40+
ret INT;
41+
BEGIN
42+
SELECT $1 INTO ret;
43+
END;
44+
$$ LANGUAGE plpgsql;
45+
CREATE OR REPLACE PROCEDURE select_one_proc(int) AS $$
46+
DECLARE
47+
ret INT;
48+
BEGIN
49+
select $1 INTO ret;
50+
END;
51+
$$ LANGUAGE plpgsql;
52+
-- plan cache auto
53+
SET plan_cache_mode TO auto;
54+
SELECT select_one_func(1);
55+
select_one_func
56+
-----------------
57+
58+
(1 row)
59+
60+
CALL select_one_proc(1);
61+
-- force generic plan
62+
SET plan_cache_mode TO force_generic_plan;
63+
SELECT select_one_func(1);
64+
select_one_func
65+
-----------------
66+
67+
(1 row)
68+
69+
CALL select_one_proc(1);
70+
-- force custom plan
71+
SET plan_cache_mode TO force_custom_plan;
72+
SELECT select_one_func(1);
73+
select_one_func
74+
-----------------
75+
76+
(1 row)
77+
78+
CALL select_one_proc(1);
79+
-- get the plan cache counters
80+
SELECT calls, generic_plan_calls, custom_plan_calls, toplevel, calls, query FROM pg_stat_statements
81+
WHERE query = 'SELECT $1' ORDER BY query COLLATE "C";
82+
calls | generic_plan_calls | custom_plan_calls | toplevel | calls | query
83+
-------+--------------------+-------------------+----------+-------+-----------
84+
6 | 2 | 4 | f | 6 | SELECT $1
85+
3 | 1 | 2 | t | 3 | SELECT $1
86+
(2 rows)
87+

contrib/pg_stat_statements/meson.build

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ contrib_targets += pg_stat_statements
2121
install_data(
2222
'pg_stat_statements.control',
2323
'pg_stat_statements--1.4.sql',
24+
'pg_stat_statements--1.12--1.13.sql',
2425
'pg_stat_statements--1.11--1.12.sql',
2526
'pg_stat_statements--1.10--1.11.sql',
2627
'pg_stat_statements--1.9--1.10.sql',
@@ -57,6 +58,7 @@ tests += {
5758
'cleanup',
5859
'oldextversions',
5960
'squashing',
61+
'plan_cache',
6062
],
6163
'regress_args': ['--temp-config', files('pg_stat_statements.conf')],
6264
# Disabled because these tests require
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/* contrib/pg_stat_statements/pg_stat_statements--1.12--1.13.sql */
2+
3+
-- complain if script is sourced in psql, rather than via ALTER EXTENSION
4+
\echo Use "ALTER EXTENSION pg_stat_statements UPDATE TO '1.13'" to load this file. \quit
5+
6+
/* First we have to remove them from the extension */
7+
ALTER EXTENSION pg_stat_statements DROP VIEW pg_stat_statements;
8+
ALTER EXTENSION pg_stat_statements DROP FUNCTION pg_stat_statements(boolean);
9+
10+
/* Then we can drop them */
11+
DROP VIEW pg_stat_statements;
12+
DROP FUNCTION pg_stat_statements(boolean);
13+
14+
/* Now redefine */
15+
CREATE FUNCTION pg_stat_statements(IN showtext boolean,
16+
OUT userid oid,
17+
OUT dbid oid,
18+
OUT toplevel bool,
19+
OUT queryid bigint,
20+
OUT query text,
21+
OUT plans int8,
22+
OUT total_plan_time float8,
23+
OUT min_plan_time float8,
24+
OUT max_plan_time float8,
25+
OUT mean_plan_time float8,
26+
OUT stddev_plan_time float8,
27+
OUT calls int8,
28+
OUT total_exec_time float8,
29+
OUT min_exec_time float8,
30+
OUT max_exec_time float8,
31+
OUT mean_exec_time float8,
32+
OUT stddev_exec_time float8,
33+
OUT rows int8,
34+
OUT shared_blks_hit int8,
35+
OUT shared_blks_read int8,
36+
OUT shared_blks_dirtied int8,
37+
OUT shared_blks_written int8,
38+
OUT local_blks_hit int8,
39+
OUT local_blks_read int8,
40+
OUT local_blks_dirtied int8,
41+
OUT local_blks_written int8,
42+
OUT temp_blks_read int8,
43+
OUT temp_blks_written int8,
44+
OUT shared_blk_read_time float8,
45+
OUT shared_blk_write_time float8,
46+
OUT local_blk_read_time float8,
47+
OUT local_blk_write_time float8,
48+
OUT temp_blk_read_time float8,
49+
OUT temp_blk_write_time float8,
50+
OUT wal_records int8,
51+
OUT wal_fpi int8,
52+
OUT wal_bytes numeric,
53+
OUT wal_buffers_full int8,
54+
OUT jit_functions int8,
55+
OUT jit_generation_time float8,
56+
OUT jit_inlining_count int8,
57+
OUT jit_inlining_time float8,
58+
OUT jit_optimization_count int8,
59+
OUT jit_optimization_time float8,
60+
OUT jit_emission_count int8,
61+
OUT jit_emission_time float8,
62+
OUT jit_deform_count int8,
63+
OUT jit_deform_time float8,
64+
OUT parallel_workers_to_launch int8,
65+
OUT parallel_workers_launched int8,
66+
OUT stats_since timestamp with time zone,
67+
OUT minmax_stats_since timestamp with time zone,
68+
OUT generic_plan_calls int8,
69+
OUT custom_plan_calls int8
70+
)
71+
RETURNS SETOF record
72+
AS 'MODULE_PATHNAME', 'pg_stat_statements_1_13'
73+
LANGUAGE C STRICT VOLATILE PARALLEL SAFE;
74+
75+
CREATE VIEW pg_stat_statements AS
76+
SELECT * FROM pg_stat_statements(true);
77+
78+
GRANT SELECT ON pg_stat_statements TO PUBLIC;

contrib/pg_stat_statements/pg_stat_statements.c

+48-7
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
#include "utils/acl.h"
7070
#include "utils/builtins.h"
7171
#include "utils/memutils.h"
72+
#include "utils/plancache.h"
7273
#include "utils/timestamp.h"
7374

7475
PG_MODULE_MAGIC_EXT(
@@ -114,6 +115,7 @@ typedef enum pgssVersion
114115
PGSS_V1_10,
115116
PGSS_V1_11,
116117
PGSS_V1_12,
118+
PGSS_V1_13,
117119
} pgssVersion;
118120

119121
typedef enum pgssStoreKind
@@ -210,6 +212,8 @@ typedef struct Counters
210212
* to be launched */
211213
int64 parallel_workers_launched; /* # of parallel workers actually
212214
* launched */
215+
int64 generic_plan_calls; /* number of calls using a generic plan */
216+
int64 custom_plan_calls; /* number of calls using a custom plan */
213217
} Counters;
214218

215219
/*
@@ -323,6 +327,7 @@ PG_FUNCTION_INFO_V1(pg_stat_statements_1_9);
323327
PG_FUNCTION_INFO_V1(pg_stat_statements_1_10);
324328
PG_FUNCTION_INFO_V1(pg_stat_statements_1_11);
325329
PG_FUNCTION_INFO_V1(pg_stat_statements_1_12);
330+
PG_FUNCTION_INFO_V1(pg_stat_statements_1_13);
326331
PG_FUNCTION_INFO_V1(pg_stat_statements);
327332
PG_FUNCTION_INFO_V1(pg_stat_statements_info);
328333

@@ -355,7 +360,8 @@ static void pgss_store(const char *query, uint64 queryId,
355360
const struct JitInstrumentation *jitusage,
356361
JumbleState *jstate,
357362
int parallel_workers_to_launch,
358-
int parallel_workers_launched);
363+
int parallel_workers_launched,
364+
CachedPlan *cplan);
359365
static void pg_stat_statements_internal(FunctionCallInfo fcinfo,
360366
pgssVersion api_version,
361367
bool showtext);
@@ -877,7 +883,8 @@ pgss_post_parse_analyze(ParseState *pstate, Query *query, JumbleState *jstate)
877883
NULL,
878884
jstate,
879885
0,
880-
0);
886+
0,
887+
NULL);
881888
}
882889

883890
/*
@@ -957,7 +964,8 @@ pgss_planner(Query *parse,
957964
NULL,
958965
NULL,
959966
0,
960-
0);
967+
0,
968+
NULL);
961969
}
962970
else
963971
{
@@ -1099,7 +1107,8 @@ pgss_ExecutorEnd(QueryDesc *queryDesc)
10991107
queryDesc->estate->es_jit ? &queryDesc->estate->es_jit->instr : NULL,
11001108
NULL,
11011109
queryDesc->estate->es_parallel_workers_to_launch,
1102-
queryDesc->estate->es_parallel_workers_launched);
1110+
queryDesc->estate->es_parallel_workers_launched,
1111+
queryDesc->cplan);
11031112
}
11041113

11051114
if (prev_ExecutorEnd)
@@ -1232,7 +1241,8 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
12321241
NULL,
12331242
NULL,
12341243
0,
1235-
0);
1244+
0,
1245+
NULL);
12361246
}
12371247
else
12381248
{
@@ -1295,7 +1305,8 @@ pgss_store(const char *query, uint64 queryId,
12951305
const struct JitInstrumentation *jitusage,
12961306
JumbleState *jstate,
12971307
int parallel_workers_to_launch,
1298-
int parallel_workers_launched)
1308+
int parallel_workers_launched,
1309+
CachedPlan *cplan)
12991310
{
13001311
pgssHashKey key;
13011312
pgssEntry *entry;
@@ -1503,6 +1514,15 @@ pgss_store(const char *query, uint64 queryId,
15031514
entry->counters.parallel_workers_to_launch += parallel_workers_to_launch;
15041515
entry->counters.parallel_workers_launched += parallel_workers_launched;
15051516

1517+
if (cplan)
1518+
{
1519+
if (cplan->status == PLAN_CACHE_STATUS_GENERIC_PLAN_BUILD ||
1520+
cplan->status == PLAN_CACHE_STATUS_GENERIC_PLAN_REUSE)
1521+
entry->counters.generic_plan_calls++;
1522+
if (cplan->status == PLAN_CACHE_STATUS_CUSTOM_PLAN)
1523+
entry->counters.custom_plan_calls++;
1524+
}
1525+
15061526
SpinLockRelease(&entry->mutex);
15071527
}
15081528

@@ -1570,7 +1590,8 @@ pg_stat_statements_reset(PG_FUNCTION_ARGS)
15701590
#define PG_STAT_STATEMENTS_COLS_V1_10 43
15711591
#define PG_STAT_STATEMENTS_COLS_V1_11 49
15721592
#define PG_STAT_STATEMENTS_COLS_V1_12 52
1573-
#define PG_STAT_STATEMENTS_COLS 52 /* maximum of above */
1593+
#define PG_STAT_STATEMENTS_COLS_V1_13 54
1594+
#define PG_STAT_STATEMENTS_COLS 54 /* maximum of above */
15741595

15751596
/*
15761597
* Retrieve statement statistics.
@@ -1582,6 +1603,16 @@ pg_stat_statements_reset(PG_FUNCTION_ARGS)
15821603
* expected API version is identified by embedding it in the C name of the
15831604
* function. Unfortunately we weren't bright enough to do that for 1.1.
15841605
*/
1606+
Datum
1607+
pg_stat_statements_1_13(PG_FUNCTION_ARGS)
1608+
{
1609+
bool showtext = PG_GETARG_BOOL(0);
1610+
1611+
pg_stat_statements_internal(fcinfo, PGSS_V1_13, showtext);
1612+
1613+
return (Datum) 0;
1614+
}
1615+
15851616
Datum
15861617
pg_stat_statements_1_12(PG_FUNCTION_ARGS)
15871618
{
@@ -1740,6 +1771,10 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo,
17401771
if (api_version != PGSS_V1_12)
17411772
elog(ERROR, "incorrect number of output arguments");
17421773
break;
1774+
case PG_STAT_STATEMENTS_COLS_V1_13:
1775+
if (api_version != PGSS_V1_13)
1776+
elog(ERROR, "incorrect number of output arguments");
1777+
break;
17431778
default:
17441779
elog(ERROR, "incorrect number of output arguments");
17451780
}
@@ -1997,6 +2032,11 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo,
19972032
values[i++] = TimestampTzGetDatum(stats_since);
19982033
values[i++] = TimestampTzGetDatum(minmax_stats_since);
19992034
}
2035+
if (api_version >= PGSS_V1_13)
2036+
{
2037+
values[i++] = Int64GetDatumFast(tmp.generic_plan_calls);
2038+
values[i++] = Int64GetDatumFast(tmp.custom_plan_calls);
2039+
}
20002040

20012041
Assert(i == (api_version == PGSS_V1_0 ? PG_STAT_STATEMENTS_COLS_V1_0 :
20022042
api_version == PGSS_V1_1 ? PG_STAT_STATEMENTS_COLS_V1_1 :
@@ -2007,6 +2047,7 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo,
20072047
api_version == PGSS_V1_10 ? PG_STAT_STATEMENTS_COLS_V1_10 :
20082048
api_version == PGSS_V1_11 ? PG_STAT_STATEMENTS_COLS_V1_11 :
20092049
api_version == PGSS_V1_12 ? PG_STAT_STATEMENTS_COLS_V1_12 :
2050+
api_version == PGSS_V1_13 ? PG_STAT_STATEMENTS_COLS_V1_13 :
20102051
-1 /* fail if you forget to update this assert */ ));
20112052

20122053
tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# pg_stat_statements extension
22
comment = 'track planning and execution statistics of all SQL statements executed'
3-
default_version = '1.12'
3+
default_version = '1.13'
44
module_pathname = '$libdir/pg_stat_statements'
55
relocatable = true

0 commit comments

Comments
 (0)