From dc9f8a798307244d791b71f666f50de319799e7c Mon Sep 17 00:00:00 2001
From: Alexander Korotkov
Date: Mon, 27 Nov 2023 02:51:18 +0200
Subject: Track statement entry timestamp in contrib/pg_stat_statements
This patch adds 'stats_since' and 'minmax_stats_since' columns to the
pg_stat_statements view and pg_stat_statements() function. The new min/max
reset mode for the pg_stat_stetments_reset() function is controlled by the
parameter minmax_only.
'stat_since' column is populated with the current timestamp when a new
statement is added to the pg_stat_statements hashtable. It provides clean
information about statistics collection time intervals for each statement.
Besides it can be used by sampling solutions to detect situations when a
statement was evicted and stored again between samples.
Such a sampling solution could derive any pg_stat_statements statistic values
for an interval between two samples with the exception of all min/max
statistics. To address this issue this patch adds the ability to reset
min/max statistics independently of the statement reset using the new
minmax_only parameter of the pg_stat_statements_reset(userid oid, dbid oid,
queryid bigint, minmax_only boolean) function. The timestamp of such reset
is stored in the minmax_stats_since field for each statement.
pg_stat_statements_reset() function now returns the timestamp of a reset as the
result.
Discussion: https://postgr.es/m/flat/72e80e7b160a6eb189df9ef6f068cce3765d37f8.camel%40moonset.ru
Author: Andrei Zubkov
Reviewed-by: Julien Rouhaud, Hayato Kuroda, Yuki Seino, Chengxi Sun
Reviewed-by: Anton Melnikov, Darren Rush, Michael Paquier, Sergei Kornilov
Reviewed-by: Alena Rybakina, Andrei Lepikhov
---
doc/src/sgml/pgstatstatements.sgml | 69 +++++++++++++++++++++++++++++++++-----
1 file changed, 60 insertions(+), 9 deletions(-)
(limited to 'doc/src')
diff --git a/doc/src/sgml/pgstatstatements.sgml b/doc/src/sgml/pgstatstatements.sgml
index b66dc7fa30a..acfacf828ce 100644
--- a/doc/src/sgml/pgstatstatements.sgml
+++ b/doc/src/sgml/pgstatstatements.sgml
@@ -140,9 +140,12 @@
min_plan_time double precision
- Minimum time spent planning the statement, in milliseconds
- (if pg_stat_statements.track_planning is enabled,
- otherwise zero)
+ Minimum time spent planning the statement, in milliseconds.
+ This field will be zero if pg_stat_statements.track_planning
+ is disabled, or if the counter has been reset using the
+ pg_stat_statements_reset function with the
+ minmax_only parameter set to true
+ and never been planned since.
@@ -151,9 +154,12 @@
max_plan_time double precision
- Maximum time spent planning the statement, in milliseconds
- (if pg_stat_statements.track_planning is enabled,
- otherwise zero)
+ Maximum time spent planning the statement, in milliseconds.
+ This field will be zero if pg_stat_statements.track_planning
+ is disabled, or if the counter has been reset using the
+ pg_stat_statements_reset function with the
+ minmax_only parameter set to true
+ and never been planned since.
@@ -203,7 +209,11 @@
min_exec_time double precision
- Minimum time spent executing the statement, in milliseconds
+ Minimum time spent executing the statement, in milliseconds,
+ this field will be zero until this statement
+ is executed first time after reset performed by the
+ pg_stat_statements_reset function with the
+ minmax_only parameter set to true
@@ -212,7 +222,11 @@
max_exec_time double precision
- Maximum time spent executing the statement, in milliseconds
+ Maximum time spent executing the statement, in milliseconds,
+ this field will be zero until this statement
+ is executed first time after reset performed by the
+ pg_stat_statements_reset function with the
+ minmax_only parameter set to true
@@ -512,6 +526,28 @@
functions, in milliseconds
+
+
+
+ stats_since timestamp with time zone
+
+
+ Time at which statistics gathering started for this statement
+
+
+
+
+
+ minmax_stats_since timestamp with time zone
+
+
+ Time at which min/max statistics gathering started for this
+ statement (fields min_plan_time,
+ max_plan_time,
+ min_exec_time and
+ max_exec_time)
+
+
@@ -713,7 +749,8 @@
- pg_stat_statements_reset(userid Oid, dbid Oid, queryid bigint) returns void
+ pg_stat_statements_reset(userid Oid, dbid Oid, queryid
+ bigint, minmax_only boolean) returns timestamp with time zone
pg_stat_statements_reset
@@ -732,6 +769,20 @@
If all statistics in the pg_stat_statements
view are discarded, it will also reset the statistics in the
pg_stat_statements_info view.
+ When minmax_only is true only the
+ values of minimun and maximum planning and execution time will be reset (i.e.
+ min_plan_time, max_plan_time,
+ min_exec_time and max_exec_time
+ fields). The default value for minmax_only parameter is
+ false. Time of last min/max reset performed is shown in
+ minmax_stats_since field of the
+ pg_stat_statements view.
+ This function returns the time of a reset. This time is saved to
+ stats_reset field of
+ pg_stat_statements_info view or to
+ minmax_stats_since field of the
+ pg_stat_statements view if the corresponding reset was
+ actually performed.
By default, this function can only be executed by superusers.
Access may be granted to others using GRANT.
--
cgit v1.2.3