2 F.32. pg_stat_statements — track statistics of SQL planning and execution #
4 F.32.1. The pg_stat_statements View
5 F.32.2. The pg_stat_statements_info View
7 F.32.4. Configuration Parameters
11 The pg_stat_statements module provides a means for tracking planning
12 and execution statistics of all SQL statements executed by a server.
14 The module must be loaded by adding pg_stat_statements to
15 shared_preload_libraries in postgresql.conf, because it requires
16 additional shared memory. This means that a server restart is needed to
17 add or remove the module. In addition, query identifier calculation
18 must be enabled in order for the module to be active, which is done
19 automatically if compute_query_id is set to auto or on, or any
20 third-party module that calculates query identifiers is loaded.
22 When pg_stat_statements is active, it tracks statistics across all
23 databases of the server. To access and manipulate these statistics, the
24 module provides views pg_stat_statements and pg_stat_statements_info,
25 and the utility functions pg_stat_statements_reset and
26 pg_stat_statements. These are not available globally but can be enabled
27 for a specific database with CREATE EXTENSION pg_stat_statements.
29 F.32.1. The pg_stat_statements View #
31 The statistics gathered by the module are made available via a view
32 named pg_stat_statements. This view contains one row for each distinct
33 combination of database ID, user ID, query ID and whether it's a
34 top-level statement or not (up to the maximum number of distinct
35 statements that the module can track). The columns of the view are
38 Table F.22. pg_stat_statements Columns
44 userid oid (references pg_authid.oid)
46 OID of user who executed the statement
48 dbid oid (references pg_database.oid)
50 OID of database in which the statement was executed
54 True if the query was executed as a top-level statement (always true if
55 pg_stat_statements.track is set to top)
59 Hash code to identify identical normalized queries.
63 Text of a representative statement
67 Number of times the statement was planned (if
68 pg_stat_statements.track_planning is enabled, otherwise zero)
70 total_plan_time double precision
72 Total time spent planning the statement, in milliseconds (if
73 pg_stat_statements.track_planning is enabled, otherwise zero)
75 min_plan_time double precision
77 Minimum time spent planning the statement, in milliseconds. This field
78 will be zero if pg_stat_statements.track_planning is disabled, or if
79 the counter has been reset using the pg_stat_statements_reset function
80 with the minmax_only parameter set to true and never been planned
83 max_plan_time double precision
85 Maximum time spent planning the statement, in milliseconds. This field
86 will be zero if pg_stat_statements.track_planning is disabled, or if
87 the counter has been reset using the pg_stat_statements_reset function
88 with the minmax_only parameter set to true and never been planned
91 mean_plan_time double precision
93 Mean time spent planning the statement, in milliseconds (if
94 pg_stat_statements.track_planning is enabled, otherwise zero)
96 stddev_plan_time double precision
98 Population standard deviation of time spent planning the statement, in
99 milliseconds (if pg_stat_statements.track_planning is enabled,
104 Number of times the statement was executed
106 total_exec_time double precision
108 Total time spent executing the statement, in milliseconds
110 min_exec_time double precision
112 Minimum time spent executing the statement, in milliseconds, this field
113 will be zero until this statement is executed first time after reset
114 performed by the pg_stat_statements_reset function with the minmax_only
115 parameter set to true
117 max_exec_time double precision
119 Maximum time spent executing the statement, in milliseconds, this field
120 will be zero until this statement is executed first time after reset
121 performed by the pg_stat_statements_reset function with the minmax_only
122 parameter set to true
124 mean_exec_time double precision
126 Mean time spent executing the statement, in milliseconds
128 stddev_exec_time double precision
130 Population standard deviation of time spent executing the statement, in
135 Total number of rows retrieved or affected by the statement
137 shared_blks_hit bigint
139 Total number of shared block cache hits by the statement
141 shared_blks_read bigint
143 Total number of shared blocks read by the statement
145 shared_blks_dirtied bigint
147 Total number of shared blocks dirtied by the statement
149 shared_blks_written bigint
151 Total number of shared blocks written by the statement
153 local_blks_hit bigint
155 Total number of local block cache hits by the statement
157 local_blks_read bigint
159 Total number of local blocks read by the statement
161 local_blks_dirtied bigint
163 Total number of local blocks dirtied by the statement
165 local_blks_written bigint
167 Total number of local blocks written by the statement
169 temp_blks_read bigint
171 Total number of temp blocks read by the statement
173 temp_blks_written bigint
175 Total number of temp blocks written by the statement
177 shared_blk_read_time double precision
179 Total time the statement spent reading shared blocks, in milliseconds
180 (if track_io_timing is enabled, otherwise zero)
182 shared_blk_write_time double precision
184 Total time the statement spent writing shared blocks, in milliseconds
185 (if track_io_timing is enabled, otherwise zero)
187 local_blk_read_time double precision
189 Total time the statement spent reading local blocks, in milliseconds
190 (if track_io_timing is enabled, otherwise zero)
192 local_blk_write_time double precision
194 Total time the statement spent writing local blocks, in milliseconds
195 (if track_io_timing is enabled, otherwise zero)
197 temp_blk_read_time double precision
199 Total time the statement spent reading temporary file blocks, in
200 milliseconds (if track_io_timing is enabled, otherwise zero)
202 temp_blk_write_time double precision
204 Total time the statement spent writing temporary file blocks, in
205 milliseconds (if track_io_timing is enabled, otherwise zero)
209 Total number of WAL records generated by the statement
213 Total number of WAL full page images generated by the statement
217 Total amount of WAL generated by the statement in bytes
219 wal_buffers_full bigint
221 Number of times the WAL buffers became full
225 Total number of functions JIT-compiled by the statement
227 jit_generation_time double precision
229 Total time spent by the statement on generating JIT code, in
232 jit_inlining_count bigint
234 Number of times functions have been inlined
236 jit_inlining_time double precision
238 Total time spent by the statement on inlining functions, in
241 jit_optimization_count bigint
243 Number of times the statement has been optimized
245 jit_optimization_time double precision
247 Total time spent by the statement on optimizing, in milliseconds
249 jit_emission_count bigint
251 Number of times code has been emitted
253 jit_emission_time double precision
255 Total time spent by the statement on emitting code, in milliseconds
257 jit_deform_count bigint
259 Total number of tuple deform functions JIT-compiled by the statement
261 jit_deform_time double precision
263 Total time spent by the statement on JIT-compiling tuple deform
264 functions, in milliseconds
266 parallel_workers_to_launch bigint
268 Number of parallel workers planned to be launched
270 parallel_workers_launched bigint
272 Number of parallel workers actually launched
274 stats_since timestamp with time zone
276 Time at which statistics gathering started for this statement
278 minmax_stats_since timestamp with time zone
280 Time at which min/max statistics gathering started for this statement
281 (fields min_plan_time, max_plan_time, min_exec_time and max_exec_time)
283 For security reasons, only superusers and roles with privileges of the
284 pg_read_all_stats role are allowed to see the SQL text and queryid of
285 queries executed by other users. Other users can see the statistics,
286 however, if the view has been installed in their database.
288 Plannable queries (that is, SELECT, INSERT, UPDATE, DELETE, and MERGE)
289 and utility commands are combined into a single pg_stat_statements
290 entry whenever they have identical query structures according to an
291 internal hash calculation. Typically, two queries will be considered
292 the same for this purpose if they are semantically equivalent except
293 for the values of literal constants appearing in the query.
297 The following details about constant replacement and queryid only apply
298 when compute_query_id is enabled. If you use an external module instead
299 to compute queryid, you should refer to its documentation for details.
301 When a constant's value has been ignored for purposes of matching the
302 query to other queries, the constant is replaced by a parameter symbol,
303 such as $1, in the pg_stat_statements display. The rest of the query
304 text is that of the first query that had the particular queryid hash
305 value associated with the pg_stat_statements entry.
307 Queries on which normalization can be applied may be observed with
308 constant values in pg_stat_statements, especially when there is a high
309 rate of entry deallocations. To reduce the likelihood of this
310 happening, consider increasing pg_stat_statements.max. The
311 pg_stat_statements_info view, discussed below in Section F.32.2,
312 provides statistics about entry deallocations.
314 In some cases, queries with visibly different texts might get merged
315 into a single pg_stat_statements entry; as explained above, this is
316 expected to happen for semantically equivalent queries. In addition, if
317 the only difference between queries is the number of elements in a list
318 of constants, the list will get squashed down to a single element but
319 shown with a commented-out list indicator:
320 =# SELECT pg_stat_statements_reset();
321 =# SELECT * FROM test WHERE a IN (1, 2, 3, 4, 5, 6, 7);
322 =# SELECT * FROM test WHERE a IN (1, 2, 3, 4, 5, 6, 7, 8);
323 =# SELECT query, calls FROM pg_stat_statements
324 WHERE query LIKE 'SELECT%';
325 -[ RECORD 1 ]------------------------------
326 query | SELECT * FROM test WHERE a IN ($1 /*, ... */)
329 In addition to these cases, there is a small chance of hash collisions
330 causing unrelated queries to be merged into one entry. (This cannot
331 happen for queries belonging to different users or databases, however.)
333 Since the queryid hash value is computed on the post-parse-analysis
334 representation of the queries, the opposite is also possible: queries
335 with identical texts might appear as separate entries, if they have
336 different meanings as a result of factors such as different search_path
339 Consumers of pg_stat_statements may wish to use queryid (perhaps in
340 combination with dbid and userid) as a more stable and reliable
341 identifier for each entry than its query text. However, it is important
342 to understand that there are only limited guarantees around the
343 stability of the queryid hash value. Since the identifier is derived
344 from the post-parse-analysis tree, its value is a function of, among
345 other things, the internal object identifiers appearing in this
346 representation. This has some counterintuitive implications. For
347 example, pg_stat_statements will consider two apparently-identical
348 queries to be distinct, if they reference for example a function that
349 was dropped and recreated between the executions of the two queries.
350 Conversely, if a table is dropped and recreated between the executions
351 of queries, two apparently-identical queries may be considered the
352 same. However, if the alias for a table is different for
353 otherwise-similar queries, these queries will be considered distinct.
354 The hashing process is also sensitive to differences in machine
355 architecture and other facets of the platform. Furthermore, it is not
356 safe to assume that queryid will be stable across major versions of
359 Two servers participating in replication based on physical WAL replay
360 can be expected to have identical queryid values for the same query.
361 However, logical replication schemes do not promise to keep replicas
362 identical in all relevant details, so queryid will not be a useful
363 identifier for accumulating costs across a set of logical replicas. If
364 in doubt, direct testing is recommended.
366 Generally, it can be assumed that queryid values are stable between
367 minor version releases of PostgreSQL, providing that instances are
368 running on the same machine architecture and the catalog metadata
369 details match. Compatibility will only be broken between minor versions
372 The parameter symbols used to replace constants in representative query
373 texts start from the next number after the highest $n parameter in the
374 original query text, or $1 if there was none. It's worth noting that in
375 some cases there may be hidden parameter symbols that affect this
376 numbering. For example, PL/pgSQL uses hidden parameter symbols to
377 insert values of function local variables into queries, so that a
378 PL/pgSQL statement like SELECT i + 1 INTO j would have representative
379 text like SELECT i + $2.
381 The representative query texts are kept in an external disk file, and
382 do not consume shared memory. Therefore, even very lengthy query texts
383 can be stored successfully. However, if many long query texts are
384 accumulated, the external file might grow unmanageably large. As a
385 recovery method if that happens, pg_stat_statements may choose to
386 discard the query texts, whereupon all existing entries in the
387 pg_stat_statements view will show null query fields, though the
388 statistics associated with each queryid are preserved. If this happens,
389 consider reducing pg_stat_statements.max to prevent recurrences.
391 plans and calls aren't always expected to match because planning and
392 execution statistics are updated at their respective end phase, and
393 only for successful operations. For example, if a statement is
394 successfully planned but fails during the execution phase, only its
395 planning statistics will be updated. If planning is skipped because a
396 cached plan is used, only its execution statistics will be updated.
398 F.32.2. The pg_stat_statements_info View #
400 The statistics of the pg_stat_statements module itself are tracked and
401 made available via a view named pg_stat_statements_info. This view
402 contains only a single row. The columns of the view are shown in
405 Table F.23. pg_stat_statements_info Columns
413 Total number of times pg_stat_statements entries about the
414 least-executed statements were deallocated because more distinct
415 statements than pg_stat_statements.max were observed
417 stats_reset timestamp with time zone
419 Time at which all statistics in the pg_stat_statements view were last
424 pg_stat_statements_reset(userid Oid, dbid Oid, queryid bigint,
425 minmax_only boolean) returns timestamp with time zone
426 pg_stat_statements_reset discards statistics gathered so far by
427 pg_stat_statements corresponding to the specified userid, dbid
428 and queryid. If any of the parameters are not specified, the
429 default value 0(invalid) is used for each of them and the
430 statistics that match with other parameters will be reset. If no
431 parameter is specified or all the specified parameters are
432 0(invalid), it will discard all statistics. If all statistics in
433 the pg_stat_statements view are discarded, it will also reset
434 the statistics in the pg_stat_statements_info view. When
435 minmax_only is true only the values of minimum and maximum
436 planning and execution time will be reset (i.e. min_plan_time,
437 max_plan_time, min_exec_time and max_exec_time fields). The
438 default value for minmax_only parameter is false. Time of last
439 min/max reset performed is shown in minmax_stats_since field of
440 the pg_stat_statements view. This function returns the time of a
441 reset. This time is saved to stats_reset field of
442 pg_stat_statements_info view or to minmax_stats_since field of
443 the pg_stat_statements view if the corresponding reset was
444 actually performed. By default, this function can only be
445 executed by superusers. Access may be granted to others using
448 pg_stat_statements(showtext boolean) returns setof record
449 The pg_stat_statements view is defined in terms of a function
450 also named pg_stat_statements. It is possible for clients to
451 call the pg_stat_statements function directly, and by specifying
452 showtext := false have query text be omitted (that is, the OUT
453 argument that corresponds to the view's query column will return
454 nulls). This feature is intended to support external tools that
455 might wish to avoid the overhead of repeatedly retrieving query
456 texts of indeterminate length. Such tools can instead cache the
457 first query text observed for each entry themselves, since that
458 is all pg_stat_statements itself does, and then retrieve query
459 texts only as needed. Since the server stores query texts in a
460 file, this approach may reduce physical I/O for repeated
461 examination of the pg_stat_statements data.
463 F.32.4. Configuration Parameters #
465 pg_stat_statements.max (integer)
466 pg_stat_statements.max is the maximum number of statements
467 tracked by the module (i.e., the maximum number of rows in the
468 pg_stat_statements view). If more distinct statements than that
469 are observed, information about the least-executed statements is
470 discarded. The number of times such information was discarded
471 can be seen in the pg_stat_statements_info view. The default
472 value is 5000. This parameter can only be set at server start.
474 pg_stat_statements.track (enum)
475 pg_stat_statements.track controls which statements are counted
476 by the module. Specify top to track top-level statements (those
477 issued directly by clients), all to also track nested statements
478 (such as statements invoked within functions), or none to
479 disable statement statistics collection. The default value is
480 top. Only superusers can change this setting.
482 pg_stat_statements.track_utility (boolean)
483 pg_stat_statements.track_utility controls whether utility
484 commands are tracked by the module. Utility commands are all
485 those other than SELECT, INSERT, UPDATE, DELETE, and MERGE. The
486 default value is on. Only superusers can change this setting.
488 pg_stat_statements.track_planning (boolean)
489 pg_stat_statements.track_planning controls whether planning
490 operations and duration are tracked by the module. Enabling this
491 parameter may incur a noticeable performance penalty, especially
492 when statements with identical query structure are executed by
493 many concurrent connections which compete to update a small
494 number of pg_stat_statements entries. The default value is off.
495 Only superusers can change this setting.
497 pg_stat_statements.save (boolean)
498 pg_stat_statements.save specifies whether to save statement
499 statistics across server shutdowns. If it is off then statistics
500 are not saved at shutdown nor reloaded at server start. The
501 default value is on. This parameter can only be set in the
502 postgresql.conf file or on the server command line.
504 The module requires additional shared memory proportional to
505 pg_stat_statements.max. Note that this memory is consumed whenever the
506 module is loaded, even if pg_stat_statements.track is set to none.
508 These parameters must be set in postgresql.conf. Typical usage might
511 shared_preload_libraries = 'pg_stat_statements'
513 compute_query_id = on
514 pg_stat_statements.max = 10000
515 pg_stat_statements.track = all
517 F.32.5. Sample Output #
519 bench=# SELECT pg_stat_statements_reset();
522 $ pgbench -c10 -t300 bench
525 bench=# SELECT query, calls, total_exec_time, rows, 100.0 * shared_blks_hit /
526 nullif(shared_blks_hit + shared_blks_read, 0) AS hit_percent
527 FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 5;
528 -[ RECORD 1 ]---+--------------------------------------------------------------
530 query | UPDATE pgbench_branches SET bbalance = bbalance + $1 WHERE bid
533 total_exec_time | 25565.855387
535 hit_percent | 100.0000000000000000
536 -[ RECORD 2 ]---+--------------------------------------------------------------
538 query | UPDATE pgbench_tellers SET tbalance = tbalance + $1 WHERE tid
541 total_exec_time | 20756.669379
543 hit_percent | 100.0000000000000000
544 -[ RECORD 3 ]---+--------------------------------------------------------------
546 query | copy pgbench_accounts from stdin
548 total_exec_time | 291.865911
550 hit_percent | 100.0000000000000000
551 -[ RECORD 4 ]---+--------------------------------------------------------------
553 query | UPDATE pgbench_accounts SET abalance = abalance + $1 WHERE aid
556 total_exec_time | 271.232977
558 hit_percent | 98.8454011741682975
559 -[ RECORD 5 ]---+--------------------------------------------------------------
561 query | alter table pgbench_accounts add primary key (aid)
563 total_exec_time | 160.588563
565 hit_percent | 100.0000000000000000
568 bench=# SELECT pg_stat_statements_reset(0,0,s.queryid) FROM pg_stat_statements A
570 WHERE s.query = 'UPDATE pgbench_branches SET bbalance = bbalance + $
573 bench=# SELECT query, calls, total_exec_time, rows, 100.0 * shared_blks_hit /
574 nullif(shared_blks_hit + shared_blks_read, 0) AS hit_percent
575 FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 5;
576 -[ RECORD 1 ]---+--------------------------------------------------------------
578 query | UPDATE pgbench_tellers SET tbalance = tbalance + $1 WHERE tid
581 total_exec_time | 20756.669379
583 hit_percent | 100.0000000000000000
584 -[ RECORD 2 ]---+--------------------------------------------------------------
586 query | copy pgbench_accounts from stdin
588 total_exec_time | 291.865911
590 hit_percent | 100.0000000000000000
591 -[ RECORD 3 ]---+--------------------------------------------------------------
593 query | UPDATE pgbench_accounts SET abalance = abalance + $1 WHERE aid
596 total_exec_time | 271.232977
598 hit_percent | 98.8454011741682975
599 -[ RECORD 4 ]---+--------------------------------------------------------------
601 query | alter table pgbench_accounts add primary key (aid)
603 total_exec_time | 160.588563
605 hit_percent | 100.0000000000000000
606 -[ RECORD 5 ]---+--------------------------------------------------------------
608 query | vacuum analyze pgbench_accounts
610 total_exec_time | 136.448116
612 hit_percent | 99.9201915403032721
614 bench=# SELECT pg_stat_statements_reset(0,0,0);
616 bench=# SELECT query, calls, total_exec_time, rows, 100.0 * shared_blks_hit /
617 nullif(shared_blks_hit + shared_blks_read, 0) AS hit_percent
618 FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 5;
619 -[ RECORD 1 ]---+--------------------------------------------------------------
621 query | SELECT pg_stat_statements_reset(0,0,0)
623 total_exec_time | 0.189497
626 -[ RECORD 2 ]---+--------------------------------------------------------------
628 query | SELECT query, calls, total_exec_time, rows, $1 * shared_blks_h
630 | nullif(shared_blks_hit + shared_blks_read, $2)
632 | FROM pg_stat_statements ORDER BY total_exec_time DES
642 Takahiro Itagaki <itagaki.takahiro@oss.ntt.co.jp>. Query normalization
643 added by Peter Geoghegan <peter@2ndquadrant.com>.