Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
F.38. pg_stat_statements
Prev UpAppendix F. Additional Supplied ModulesHome Next

F.38. pg_stat_statements

Thepg_stat_statements module provides a means for tracking execution statistics of all SQL statements executed by a server.

The module must be loaded by addingpg_stat_statements toshared_preload_libraries inpostgresql.conf, because it requires additional shared memory. This means that a server restart is needed to add or remove the module.

Whenpg_stat_statements is loaded, it tracks statistics across all databases of the server. To access and manipulate these statistics, the module provides a view,pg_stat_statements, and the utility functionspg_stat_statements_reset andpg_stat_statements. These are not available globally but can be enabled for a specific database withCREATE EXTENSION pg_stat_statements.

F.38.1. Thepg_stat_statements View

The statistics gathered by the module are made available via a view namedpg_stat_statements. This view contains one row for each distinct database ID, user ID and query ID (up to the maximum number of distinct statements that the module can track). The columns of the view are shown inTable F.22.

Table F.22. pg_stat_statements Columns

NameTypeReferencesDescription
useridoidpg_authid.oidOID of user who executed the statement
dbidoidpg_database.oidOID of database in which the statement was executed
queryidbigint Internal hash code, computed from the statement's parse tree
querytext Text of a representative statement
callsbigint Number of times executed
total_timedouble precision Total time spent in the statement, in milliseconds
min_timedouble precision Minimum time spent in the statement, in milliseconds
max_timedouble precision Maximum time spent in the statement, in milliseconds
mean_timedouble precision Mean time spent in the statement, in milliseconds
stddev_timedouble precision Population standard deviation of time spent in the statement, in milliseconds
rowsbigint Total number of rows retrieved or affected by the statement
shared_blks_hitbigint Total number of shared block cache hits by the statement
shared_blks_readbigint Total number of shared blocks read by the statement
shared_blks_dirtiedbigint Total number of shared blocks dirtied by the statement
shared_blks_writtenbigint Total number of shared blocks written by the statement
local_blks_hitbigint Total number of local block cache hits by the statement
local_blks_readbigint Total number of local blocks read by the statement
local_blks_dirtiedbigint Total number of local blocks dirtied by the statement
local_blks_writtenbigint Total number of local blocks written by the statement
temp_blks_readbigint Total number of temp blocks read by the statement
temp_blks_writtenbigint Total number of temp blocks written by the statement
blk_read_timedouble precision  Total time the statement spent reading blocks, in milliseconds (iftrack_io_timing is enabled, otherwise zero)
blk_write_timedouble precision  Total time the statement spent writing blocks, in milliseconds (iftrack_io_timing is enabled, otherwise zero)

For security reasons, only superusers and members of thepg_read_all_stats role are allowed to see the SQL text andqueryid 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, andDELETE) are combined into a singlepg_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. Utility commands (that is, all other commands) are compared strictly on the basis of their textual query strings, however.

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 thepg_stat_statements display. The rest of the query text is that of the first query that had the particularqueryid hash value associated with thepg_stat_statements entry.

In some cases, queries with visibly different texts might get merged into a singlepg_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 thequeryid 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 differentsearch_path settings.

Consumers ofpg_stat_statements may wish to usequeryid (perhaps in combination withdbid anduserid) 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 thequeryid 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 thatqueryid will be stable across major versions ofPostgres Pro.

As a rule of thumb,queryid values can be assumed to be stable and comparable only so long as the underlying server version and catalog metadata details stay exactly the same. Two servers participating in replication based on physical WAL replay can be expected to have identicalqueryid values for the same query. However, logical replication schemes do not promise to keep replicas identical in all relevant details, soqueryid will not be a useful identifier for accumulating costs across a set of logical replicas. If in doubt, direct testing is recommended.

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 aPL/pgSQL statement likeSELECT i + 1 INTO j would have representative text likeSELECT 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 thepg_stat_statements view will show nullquery fields, though the statistics associated with eachqueryid are preserved. If this happens, consider reducingpg_stat_statements.max to prevent recurrences.

F.38.2. Functions

pg_stat_statements_reset() returns void

pg_stat_statements_reset discards all statistics gathered so far bypg_stat_statements. By default, this function can only be executed by superusers.

pg_stat_statements(showtext boolean) returns setof record

Thepg_stat_statements view is defined in terms of a function also namedpg_stat_statements. It is possible for clients to call thepg_stat_statements function directly, and by specifyingshowtext := false have query text be omitted (that is, theOUT argument that corresponds to the view'squery 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 allpg_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 thepg_stat_statements data.

F.38.3. Configuration Parameters

pg_stat_statements.max (integer)

pg_stat_statements.max is the maximum number of statements tracked by the module (i.e., the maximum number of rows in thepg_stat_statements view). If more distinct statements than that are observed, information about the least-executed statements is discarded. The default value is 5000. This parameter can only be set at server start.

pg_stat_statements.track (enum)

pg_stat_statements.track controls which statements are counted by the module. Specifytop to track top-level statements (those issued directly by clients),all to also track nested statements (such as statements invoked within functions), ornone to disable statement statistics collection. The default value istop. Only superusers can change this setting.

pg_stat_statements.track_utility (boolean)

pg_stat_statements.track_utility controls whether utility commands are tracked by the module. Utility commands are all those other thanSELECT,INSERT,UPDATE andDELETE. The default value ison. Only superusers can change this setting.

pg_stat_statements.save (boolean)

pg_stat_statements.save specifies whether to save statement statistics across server shutdowns. If it isoff then statistics are not saved at shutdown nor reloaded at server start. The default value ison. This parameter can only be set in thepostgresql.conf file or on the server command line.

The module requires additional shared memory proportional topg_stat_statements.max. Note that this memory is consumed whenever the module is loaded, even ifpg_stat_statements.track is set tonone.

These parameters must be set inpostgresql.conf. Typical usage might be:

# postgresql.confshared_preload_libraries = 'pg_stat_statements'pg_stat_statements.max = 10000pg_stat_statements.track = all

F.38.4. Sample Output

bench=# SELECT pg_stat_statements_reset();$ pgbench -i bench$ pgbench -c10 -t300 benchbench=# \xbench=# SELECT query, calls, total_time, rows, 100.0 * shared_blks_hit /               nullif(shared_blks_hit + shared_blks_read, 0) AS hit_percent          FROM pg_stat_statements ORDER BY total_time DESC LIMIT 5;-[ RECORD 1 ]---------------------------------------------------------------------query       | UPDATE pgbench_branches SET bbalance = bbalance + $1 WHERE bid = $2;calls       | 3000total_time  | 9609.00100000002rows        | 2836hit_percent | 99.9778970000200936-[ RECORD 2 ]---------------------------------------------------------------------query       | UPDATE pgbench_tellers SET tbalance = tbalance + $1 WHERE tid = $2;calls       | 3000total_time  | 8015.156rows        | 2990hit_percent | 99.9731126579631345-[ RECORD 3 ]---------------------------------------------------------------------query       | copy pgbench_accounts from stdincalls       | 1total_time  | 310.624rows        | 100000hit_percent | 0.30395136778115501520-[ RECORD 4 ]---------------------------------------------------------------------query       | UPDATE pgbench_accounts SET abalance = abalance + $1 WHERE aid = $2;calls       | 3000total_time  | 271.741999999997rows        | 3000hit_percent | 93.7968855088209426-[ RECORD 5 ]---------------------------------------------------------------------query       | alter table pgbench_accounts add primary key (aid)calls       | 1total_time  | 81.42rows        | 0hit_percent | 34.4947735191637631

F.38.5. Authors

Takahiro Itagaki<itagaki.takahiro@oss.ntt.co.jp>. Query normalization added by Peter Geoghegan<peter@2ndquadrant.com>.


Prev Up Next
F.37. pgrowlocks Home F.39. pgstattuple
epubpdf
Go to Postgres Pro Standard 11
By continuing to browse this website, you agree to the use of cookies. Go toPrivacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp