RefLib

Reference library for AI

Back

postgresql_16:9.6.32.1 The pg_stat_statements View

PostgreSQL 16 > Appendixes > Additional Supplied Modules and Extensions > pg_stat_statements — track statistics of SQL planning and execution > 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 (see pgstatstatements-columns).

| Column Type Description | | --- | | userid oid (references pg_authid (see postgresql_16:8.2.8).oid) OID of user who executed the statement | | dbid oid (references pg_database (see postgresql_16:8.2.15).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 (if pg_stat_statements.track_planning is enabled, otherwise zero) | | max_plan_time double precision Maximum time spent planning the statement, in milliseconds (if pg_stat_statements.track_planning is enabled, otherwise zero) | | 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 | | max_exec_time double precision Maximum time spent executing the statement, in milliseconds | | 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 | | shared_blks_read bigint Total number of shared blocks read by the statement | | shared_blks_dirtied bigint Total number of shared blocks dirtied by the statement | | shared_blks_written bigint Total number of shared blocks written by the statement | | local_blks_hit bigint Total number of local block cache hits by the statement | | local_blks_read bigint Total number of local blocks read by the statement | | local_blks_dirtied bigint Total number of local blocks dirtied by the statement | | local_blks_written bigint Total number of local blocks written by the statement | | temp_blks_read bigint Total number of temp blocks read by the statement | | temp_blks_written bigint Total number of temp blocks written by the statement | | blk_read_time double precision Total time the statement spent reading data file blocks, in milliseconds (if (see guc-track-io-timing) is enabled, otherwise zero) | | blk_write_time double precision Total time the statement spent writing data file blocks, in milliseconds (if (see guc-track-io-timing) is enabled, otherwise zero) | | temp_blk_read_time double precision Total time the statement spent reading temporary file blocks, in milliseconds (if (see guc-track-io-timing) is enabled, otherwise zero) | | temp_blk_write_time double precision Total time the statement spent writing temporary file blocks, in milliseconds (if (see guc-track-io-timing) is enabled, otherwise zero) | | wal_records bigint Total number of WAL records generated by the statement | | wal_fpi bigint Total number of WAL full page images generated by the statement | | wal_bytes numeric Total amount of WAL generated by the statement in bytes | | jit_functions bigint Total number of functions JIT-compiled by the statement | | jit_generation_time double precision Total time spent by the statement on generating JIT code, in milliseconds | | jit_inlining_count bigint Number of times functions have been inlined | | jit_inlining_time double precision Total time spent by the statement on inlining functions, in milliseconds | | jit_optimization_count bigint Number of times the statement has been optimized | | jit_optimization_time double precision Total time spent by the statement on optimizing, in milliseconds | | jit_emission_count bigint Number of times code has been emitted | | jit_emission_time double precision Total time spent by the statement on emitting code, in milliseconds |

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, SELECT, INSERT, UPDATE, DELETE, 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.

Note

The following details about constant replacement and queryid only apply when (see guc-compute-query-id) is enabled. If you use an external module instead to compute queryid, you should refer to its documentation for details.

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.

Queries on which normalization can be applied may be observed with constant values in pg_stat_statements, especially when there is a high rate of entry deallocations. To reduce the likelihood of this happening, consider increasing pg_stat_statements.max. The pg_stat_statements_info view, discussed below in (see postgresql_16:9.6.32.2), provides statistics about entry deallocations.

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 PostgreSQL.

Two servers participating in replication based on physical WAL replay can be expected to have identical queryid values for the same query. However, logical replication schemes do not promise to keep replicas identical in all relevant details, so queryid will not be a useful identifier for accumulating costs across a set of logical replicas. If in doubt, direct testing is recommended.

Generally, it can be assumed that queryid values are stable between minor version releases of PostgreSQL, 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.

Back