Postgresql 中文操作指南
F.32. pg_stat_statements — track statistics of SQL planning and execution #
pg_stat_statements 模块提供了一种机制,用于跟踪服务器执行的所有 SQL 语句的计划和执行统计信息。
The pg_stat_statements module provides a means for tracking planning and execution statistics of all SQL statements executed by a server.
必须通过向 postgresql.conf 中的 shared_preload_libraries 添加 pg_stat_statements 来加载模块,因为它需要额外的共享内存。这意味着添加或删除模块需要重新启动服务器。此外,必须启用查询标识符计算才能使模块处于活动状态,如果将 compute_query_id 设置为 auto 或 on,或者加载任何计算查询标识符的第三方模块,则会自动执行此操作。
The module must be loaded by adding pg_stat_statements to shared_preload_libraries in postgresql.conf, because it requires additional shared memory. This means that a server restart is needed to add or remove the module. In addition, query identifier calculation must be enabled in order for the module to be active, which is done automatically if compute_query_id is set to auto or on, or any third-party module that calculates query identifiers is loaded.
当激活 pg_stat_statements 时,它追踪服务器所有数据库的统计信息。为了访问和操控这些统计信息,该模块提供了视图 pg_stat_statements 和 pg_stat_statements_info,以及实用函数 pg_stat_statements_reset 和 pg_stat_statements。它们无法全局使用,但可以使用 CREATE EXTENSION pg_stat_statements 为特定数据库启用它们。
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.32.1. The pg_stat_statements View #
模块收集的统计信息通过名为 pg_stat_statements 的视图提供。此视图为数据库 ID、用户 ID、查询 ID 及其是否为顶级语句(最多为模块可以跟踪的不同语句的最大数量)的每个不同组合包含一行。视图的列显示在 Table F.22 中。
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.22.
Table F.22. pg_stat_statements Columns
Table F.22. pg_stat_statements Columns
Column Type Description |
userid oid (references pg_authid.oid) OID of user who executed the statement |
dbid oid (references pg_database.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 track_io_timing is enabled, otherwise zero) |
blk_write_time double precision Total time the statement spent writing data file blocks, in milliseconds (if 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 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 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 |
出于安全考虑,只有超级用户和具有 @{5} 角色权限的角色才能查看其他用户执行的查询的 SQL 文本和 @{6}。但如果在自己的数据库中安装了视图,其他用户也可以查看统计信息。
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.
可规划查询(即 @{7}、@{8}、@{9}、@{10} 和 @{11})和实用程序命令会根据内部哈希计算合并到一个 @{12} 条目中,只要它们具有相同的查询结构。通常,如果具有相同的语义等价性(查询中出现文本常量的值除外),将考虑两个查询是否相同。
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
以下有关常量替换和 queryid 的详细信息仅当 compute_query_id 启用时才适用。如果您使用外部模块来计算 queryid,则您应参阅其文档以获取详细信息。
The following details about constant replacement and queryid only apply when compute_query_id is enabled. If you use an external module instead to compute queryid, you should refer to its documentation for details.
当常量值被忽略以匹配查询与其他查询时,常量会被 @{13} 等参数符号替换,显示为 @{14}。查询文本的其余部分是第一个与该 @{16} 条目相关联的 @{15} 哈希值相匹配的查询部分。
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.
可应用规范化的查询可能会在 @{17} 中观察到常量值,尤其是在条目释放率较高时。为了减少发生这种情况的可能性,请考虑增加 @{18}。在 @{20} 中讨论的 @{19} 视图提供了有关条目释放的统计信息。
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 Section F.32.2, provides statistics about entry deallocations.
在某些情况下,具有明显不同文本的查询可能会合并到单个 @{21} 条目中。通常,只会对具有相同语义等价性的查询执行此操作,但哈希冲突会导致不相关的查询合并到一个条目中的可能性很小。(然而,属于不同用户或数据库的查询不会出现这种情况。)
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.)
由于 @{22} 哈希值是在查询的后解析分析表示上计算的,因此反过来也可能:具有相同文本的查询可能会显示为单独的条目,如果它们的含义由于 @{23} 等不同因素而不同。
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.
@{24} 的使用者可能希望将 @{25}(可能与 @{26} 和 @{27} 结合使用)用作比其查询文本更稳定、更可靠的每个条目的标识符。然而,了解围绕 @{28} 哈希值的稳定性只有有限的担保是很重要的。由于标识符源自后解析分析树,因此其值不仅取决于此表示中出现的内部对象标识符。这有一些违反直觉的含义。例如,如果在执行两次查询之间引用一个在两次执行之间被删除并重新创建的表,@{29} 会将两个看似相同的查询视作不同的查询。哈希过程还对机器架构和平台的其他方面中的差异敏感。此外,无法确保 @{30} 在 PostgreSQL 的主要版本之间是稳定的。
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.
可以预期参与基于物理 WAL 重放的复制的两个服务器将为相同的查询拥有相同的 @{31} 值。但是,逻辑复制方案并不会承诺使副本在所有相关细节上保持相同,因此 @{32} 不会成为用于累积一系列逻辑副本的成本的有用标识符。如有疑问,建议进行直接测试。
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.
通常,可以假设 @{33} 值在 PostgreSQL 的次要版本发布之间是稳定的,前提是实例在同一机器架构上运行,并且目录元数据详细信息匹配。次要版本之间的兼容性只会作为最后手段才能中断。
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.
用于替换代表性查询文本中的常量的参数符号从原始查询文本中的 @{34} 参数后的下一个数字开始,如果没有则从 @{35} 开始。值得注意的是,在某些情况下可能存在影响此编号的隐藏参数符号。例如,PL/pgSQL 使用隐藏参数符号将函数局部变量的值插入到查询中,以便像 @{36} 这样的 PL/pgSQL 语句的代表文本为 @{37}。
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.
代表性查询文本保存在外部磁盘文件中,并且不使用共享内存。因此,甚至非常长的查询文本也可以成功存储。然而,如果积累了许多长查询文本,外部文件可能会变得难以管理的大。如果发生这种情况,一种恢复方法是 @{38} 可以选择丢弃查询文本,此时 @{39} 视图中的所有现有条目将显示为 null @{40} 字段,尽管与每个 @{41} 关联的统计信息得以保留。如果出现这种情况,请考虑减少 @{42} 以防止复发。
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.
并不要总期望 @{43} 和 @{44} 匹配,因为规划和执行统计信息是在它们各自的结束阶段更新的,并且仅针对成功操作更新。例如,如果一个语句已成功规划但在执行阶段失败,则只会更新其规划统计信息。如果由于使用了已缓存的计划而跳过规划,则只会更新其执行统计信息。
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.32.2. The pg_stat_statements_info View #
@{45} 模块本身的统计信息会被跟踪并通过名为 @{46} 的视图公开。此视图只包含一行。视图的列显示在 @{47} 中。
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.23.
Table F.23. pg_stat_statements_info Columns
Table F.23. pg_stat_statements_info Columns
Column Type Description |
dealloc bigint Total 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 zone Time at which all statistics in the pg_stat_statements view were last reset. |
F.32.3. Functions #
-
pg_stat_statements_reset(userid Oid, dbid Oid, queryid bigint) returns void
-
pg_stat_statements_reset discards statistics gathered so far by pg_stat_statements corresponding to the specified userid, dbid 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. 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.32.4. 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 the pg_stat_statements view). If more distinct statements than that are observed, information about the least-executed statements is discarded. The number of times such information was discarded can be seen in the pg_stat_statements_info view. 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. Specify top to track top-level statements (those issued directly by clients), all to also track nested statements (such as statements invoked within functions), or none to disable statement statistics collection. The default value is top. 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 than SELECT, INSERT, UPDATE, DELETE, and MERGE. The default value is on. Only superusers can change this setting.
-
-
pg_stat_statements.track_planning (boolean)
-
pg_stat_statements.track_planning controls whether planning operations and duration are tracked by the module. Enabling this parameter may incur a noticeable performance penalty, especially when statements with identical query structure are executed by many concurrent connections which compete to update a small number of pg_stat_statements entries. The default value is off. 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 is off then statistics are not saved at shutdown nor reloaded at server start. The default value is on. This parameter can only be set in the postgresql.conf file or on the server command line.
-
此模块需要与 pg_stat_statements.max 成正比的附加共享内存。请注意,只要加载模块,就会消耗此内存,即使 pg_stat_statements.track 设置为 none 也是如此。
The module requires additional shared memory proportional to pg_stat_statements.max. Note that this memory is consumed whenever the module is loaded, even if pg_stat_statements.track is set to none.
这些参数必须在 postgresql.conf 中设置。典型用法可能是:
These parameters must be set in postgresql.conf. Typical usage might be:
# postgresql.conf
shared_preload_libraries = 'pg_stat_statements'
compute_query_id = on
pg_stat_statements.max = 10000
pg_stat_statements.track = all
F.32.5. Sample Output #
bench=# SELECT pg_stat_statements_reset();
$ pgbench -i bench
$ pgbench -c10 -t300 bench
bench=# \x
bench=# SELECT query, calls, total_exec_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_exec_time DESC LIMIT 5;
-[ RECORD 1 ]---+--------------------------------------------------------------------
query | UPDATE pgbench_branches SET bbalance = bbalance + $1 WHERE bid = $2
calls | 3000
total_exec_time | 25565.855387
rows | 3000
hit_percent | 100.0000000000000000
-[ RECORD 2 ]---+--------------------------------------------------------------------
query | UPDATE pgbench_tellers SET tbalance = tbalance + $1 WHERE tid = $2
calls | 3000
total_exec_time | 20756.669379
rows | 3000
hit_percent | 100.0000000000000000
-[ RECORD 3 ]---+--------------------------------------------------------------------
query | copy pgbench_accounts from stdin
calls | 1
total_exec_time | 291.865911
rows | 100000
hit_percent | 100.0000000000000000
-[ RECORD 4 ]---+--------------------------------------------------------------------
query | UPDATE pgbench_accounts SET abalance = abalance + $1 WHERE aid = $2
calls | 3000
total_exec_time | 271.232977
rows | 3000
hit_percent | 98.8454011741682975
-[ RECORD 5 ]---+--------------------------------------------------------------------
query | alter table pgbench_accounts add primary key (aid)
calls | 1
total_exec_time | 160.588563
rows | 0
hit_percent | 100.0000000000000000
bench=# SELECT pg_stat_statements_reset(0,0,s.queryid) FROM pg_stat_statements AS s
WHERE s.query = 'UPDATE pgbench_branches SET bbalance = bbalance + $1 WHERE bid = $2';
bench=# SELECT query, calls, total_exec_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_exec_time DESC LIMIT 5;
-[ RECORD 1 ]---+--------------------------------------------------------------------
query | UPDATE pgbench_tellers SET tbalance = tbalance + $1 WHERE tid = $2
calls | 3000
total_exec_time | 20756.669379
rows | 3000
hit_percent | 100.0000000000000000
-[ RECORD 2 ]---+--------------------------------------------------------------------
query | copy pgbench_accounts from stdin
calls | 1
total_exec_time | 291.865911
rows | 100000
hit_percent | 100.0000000000000000
-[ RECORD 3 ]---+--------------------------------------------------------------------
query | UPDATE pgbench_accounts SET abalance = abalance + $1 WHERE aid = $2
calls | 3000
total_exec_time | 271.232977
rows | 3000
hit_percent | 98.8454011741682975
-[ RECORD 4 ]---+--------------------------------------------------------------------
query | alter table pgbench_accounts add primary key (aid)
calls | 1
total_exec_time | 160.588563
rows | 0
hit_percent | 100.0000000000000000
-[ RECORD 5 ]---+--------------------------------------------------------------------
query | vacuum analyze pgbench_accounts
calls | 1
total_exec_time | 136.448116
rows | 0
hit_percent | 99.9201915403032721
bench=# SELECT pg_stat_statements_reset(0,0,0);
bench=# SELECT query, calls, total_exec_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_exec_time DESC LIMIT 5;
-[ RECORD 1 ]---+-----------------------------------------------------------------------------
query | SELECT pg_stat_statements_reset(0,0,0)
calls | 1
total_exec_time | 0.189497
rows | 1
hit_percent |
-[ RECORD 2 ]---+-----------------------------------------------------------------------------
query | SELECT query, calls, total_exec_time, rows, $1 * shared_blks_hit / +
| nullif(shared_blks_hit + shared_blks_read, $2) AS hit_percent+
| FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT $3
calls | 0
total_exec_time | 0
rows | 0
hit_percent |
F.32.6. Authors #
Peter Geoghegan < link:mailto:peter@2ndquadrant.com[peter@2ndquadrant.com]> 添加了 Takahiro Itagaki < link:mailto:itagaki.takahiro@oss.ntt.co.jp[itagaki.takahiro@oss.ntt.co.jp]> 的查询规范化。
Takahiro Itagaki <link:mailto:itagaki.takahiro@oss.ntt.co.jp[itagaki.takahiro@oss.ntt.co.jp]>. Query normalization added by Peter Geoghegan <link:mailto:peter@2ndquadrant.com[peter@2ndquadrant.com]>.