F.30. pg_stat_statements — track statistics of SQL planning and execution

The pg_stat_statements module provides a means for tracking planning and execution statistics of all SQL statements executed by a server.

When pg_stat_statements is active, it tracks statistics across all databases of the server. To access and manipulate these statistics, the module provides views pg_stat_statements and pg_stat_statements_info, and the utility functions pg_stat_statements_reset and pg_stat_statements. These are not available globally but can be enabled for a specific database with CREATE EXTENSION pg_stat_statements.

F.30.1. The pg_stat_statements View 

The statistics gathered by the module are made available via a view named pg_stat_statements. This view contains one row for each distinct combination of database ID, user ID, query ID and whether it's a top-level statement or not (up to the maximum number of distinct statements that the module can track). The columns of the view are shown in Table F.21.

Table F.21. pg_stat_statements Columns

Column Type Description
userid oid 
 OID of user who executed the statement
dbid oid
 OID of database in which the statement was executed
toplevel bool
 True if the query was executed as a top-level statement (always true if pg_stat_statements.track is set to top)
queryid bigint
 Hash code to identify identical normalized queries.
query text
 Text of a representative statement
plans bigint
 Number of times the statement was planned (if pg_stat_statements.track_planning is enabled, otherwise zero)
total_plan_time double precision
 Total time spent planning the statement, in milliseconds (if pg_stat_statements.track_planning is enabled, otherwise zero)
min_plan_time double precision
 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.
max_plan_time double precision
 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.
mean_plan_time double precision
 Mean time spent planning the statement, in milliseconds (if pg_stat_statements.track_planning is enabled, otherwise zero)
stddev_plan_time double precision
 Population standard deviation of time spent planning the statement, in milliseconds (if pg_stat_statements.track_planning is enabled, otherwise zero)
calls bigint
 Number of times the statement was executed
total_exec_time double precision
 Total time spent executing the statement, in milliseconds
min_exec_time double precision
 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
max_exec_time double precision
 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
mean_exec_time double precision
 Mean time spent executing the statement, in milliseconds
stddev_exec_time double precision
 Population standard deviation of time spent executing the statement, in milliseconds
rows bigint
 Total number of rows retrieved or affected by the statement
shared_blks_hit bigint
 Total number of shared block cache hits by the statement. Not available yet.
shared_blks_read bigint
 Total number of shared blocks read by the statement. Not available yet.
shared_blks_dirtied bigint
 Total number of shared blocks dirtied by the statement. Not available yet.
shared_blks_written bigint
 Total number of shared blocks written by the statement. Not available yet.
local_blks_hit bigint
 Total number of local block cache hits by the statement. Not available yet.
local_blks_read bigint
 Total number of local blocks read by the statement. Not available yet.
local_blks_dirtied bigint
 Total number of local blocks dirtied by the statement. Not available yet.
local_blks_written bigint
 Total number of local blocks written by the statement. Not available yet.
temp_blks_read bigint
 Total number of temp blocks read by the statement. Not available yet.
temp_blks_written bigint
 Total number of temp blocks written by the statement. Not available yet.
blk_read_time double precision
 Total time the statement spent reading blocks, in milliseconds. Not available yet.
blk_write_time double precision
 Total time the statement spent writing blocks, in milliseconds. Not available yet.
wal_records bigint
 Total number of WAL records generated by the statement. Not available yet.
wal_fpi bigint
 Total number of WAL full page images generated by the statement. Not available yet.
wal_bytes numeric
 Total amount of WAL generated by the statement in bytes. Not available yet.

For security reasons, only superusers and roles with privileges of the pg_read_all_stats role are allowed to see the SQL text and queryid of queries executed by other users. Other users can see the statistics, however, if the view has been installed in their database.

Plannable queries (that is, SELECTINSERTUPDATEDELETE, and MERGE) and utility commands are combined into a single pg_stat_statements entry whenever they have identical query structures according to an internal hash calculation. Typically, two queries will be considered the same for this purpose if they are semantically equivalent except for the values of literal constants appearing in the query.

When a constant's value has been ignored for purposes of matching the query to other queries, the constant is replaced by a parameter symbol, such as $1, in the pg_stat_statements display. The rest of the query text is that of the first query that had the particular queryid hash value associated with the pg_stat_statements entry.

In some cases, queries with visibly different texts might get merged into a single pg_stat_statements entry. Normally this will happen only for semantically equivalent queries, but there is a small chance of hash collisions causing unrelated queries to be merged into one entry. (This cannot happen for queries belonging to different users or databases, however.)

Since the queryid hash value is computed on the post-parse-analysis representation of the queries, the opposite is also possible: queries with identical texts might appear as separate entries, if they have different meanings as a result of factors such as different search_path settings.

Consumers of pg_stat_statements may wish to use queryid (perhaps in combination with dbid and userid) as a more stable and reliable identifier for each entry than its query text. However, it is important to understand that there are only limited guarantees around the stability of the queryid hash value. Since the identifier is derived from the post-parse-analysis tree, its value is a function of, among other things, the internal object identifiers appearing in this representation. This has some counterintuitive implications. For example, pg_stat_statements will consider two apparently-identical queries to be distinct, if they reference a table that was dropped and recreated between the executions of the two queries. The hashing process is also sensitive to differences in machine architecture and other facets of the platform. Furthermore, it is not safe to assume that queryid will be stable across major versions of ProtonBase.

Generally, it can be assumed that queryid values are stable between minor version releases of ProtonBase, providing that instances are running on the same machine architecture and the catalog metadata details match. Compatibility will only be broken between minor versions as a last resort.

The parameter symbols used to replace constants in representative query texts start from the next number after the highest $n parameter in the original query text, or $1 if there was none. It's worth noting that in some cases there may be hidden parameter symbols that affect this numbering. For example, PL/pgSQL uses hidden parameter symbols to insert values of function local variables into queries, so that a PL/pgSQL statement like SELECT i + 1 INTO j would have representative text like SELECT i + $2.

The representative query texts are kept in an external disk file, and do not consume shared memory. Therefore, even very lengthy query texts can be stored successfully. However, if many long query texts are accumulated, the external file might grow unmanageably large. As a recovery method if that happens, pg_stat_statements may choose to discard the query texts, whereupon all existing entries in the pg_stat_statements view will show null query fields, though the statistics associated with each queryid are preserved. If this happens, consider reducing pg_stat_statements.max to prevent recurrences.

plans and calls aren't always expected to match because planning and execution statistics are updated at their respective end phase, and only for successful operations. For example, if a statement is successfully planned but fails during the execution phase, only its planning statistics will be updated. If planning is skipped because a cached plan is used, only its execution statistics will be updated.

F.30.2. The pg_stat_statements_info View

The statistics of the pg_stat_statements module itself are tracked and made available via a view named pg_stat_statements_info. This view contains only a single row. The columns of the view are shown in Table F.22.

Table F.22. pg_stat_statements_info Columns

Column TypeDescription
dealloc bigintTotal number of times pg_stat_statements entries about the least-executed statements were deallocated because more distinct statements than pg_stat_statements.max were observed
stats_reset timestamp with time zoneTime at which all statistics in the pg_stat_statements view were last reset.

F.30.3. Functions

pg_stat_statements_reset(userid Oid, dbid Oid, queryid bigint, minmax_only boolean) returns timestamp with time zone

pg_stat_statements_reset discards statistics gathered so far by pg_stat_statements corresponding to the specified useriddbid and queryid. If any of the parameters are not specified, the default value 0(invalid) is used for each of them and the statistics that match with other parameters will be reset. If no parameter is specified or all the specified parameters are 0(invalid), it will discard all statistics. 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 minimum and maximum planning and execution time will be reset (i.e. min_plan_timemax_plan_timemin_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.

pg_stat_statements(showtext boolean) returns setof record

 The pg_stat_statements view is defined in terms of a function also named pg_stat_statements. It is possible for clients to call the pg_stat_statements function directly, and by specifying showtext := false have query text be omitted (that is, the OUT argument that corresponds to the view's query column will return nulls). This feature is intended to support external tools that might wish to avoid the overhead of repeatedly retrieving query texts of indeterminate length. Such tools can instead cache the first query text observed for each entry themselves, since that is all pg_stat_statements itself does, and then retrieve query texts only as needed. Since the server stores query texts in a file, this approach may reduce physical I/O for repeated examination of the pg_stat_statements data.

F.30.5. Sample Output

Prepare the sample data:

bench=# SELECT pg_stat_statements_reset();
pgbench -h [host] -p 5432 -U [username] -i bench -I dtGvp

Run the test:

pgbench -h [host] -p 5432 -U [username] -c10 -t300 bench
bench=# SELECT query, calls, total_exec_time, rows
 FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 5;
                                              query                                              | calls |  total_exec_time   | rows
-------------------------------------------------------------------------------------------------+-------+--------------------+------
 UPDATE pgbench_tellers SET tbalance = tbalance + _ WHERE tid = _                                |  3000 | 1576.7059285039973 | 3000
 UPDATE pgbench_branches SET bbalance = bbalance + _ WHERE bid = _                               |  3000 | 43.194362225000056 | 3000
 UPDATE pgbench_accounts SET abalance = abalance + _ WHERE aid = _                               |  3000 |   7.73518531400001 | 3000
 INSERT INTO pgbench_history(tid, bid, aid, delta, mtime) VALUES (_, _, _, _, current_timestamp) |  3000 |  4.496987373999999 | 3000
 SELECT abalance FROM pgbench_accounts WHERE aid = _                                             |  3000 | 2.6564216599999972 | 3000
(5 rows)
 
bench=# SELECT pg_stat_statements_reset();
bench=# SELECT query, calls, total_exec_time, rows
 FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 5;
               query               | calls | total_exec_time | rows
-----------------------------------+-------+-----------------+------
 SELECT pg_stat_statements_reset() |     1 |     0.012120068 |    1
(1 row)

F.30.6. Authors

Takahiro Itagaki <[itagaki.takahiro@oss.ntt.co.jp](mailto:itagaki.takahiro@oss.ntt.co.jp)>. Query normalization added by Peter Geoghegan <[peter@2ndquadrant.com](mailto:peter@2ndquadrant.com)>.