Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
pgstatstatements.html625 linesDownload Raw Back to html
1<?xml version="1.0" encoding="UTF-8" standalone="no"?>2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>F.32. pg_stat_statements — track statistics of SQL planning and execution</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="pgrowlocks.html" title="F.31. pgrowlocks — show a table's row locking information" /><link rel="next" href="pgstattuple.html" title="F.33. pgstattuple — obtain tuple-level statistics" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">F.32. pg_stat_statements — track statistics of SQL planning and execution</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="pgrowlocks.html" title="F.31. pgrowlocks — show a table's row locking information">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="contrib.html" title="Appendix F. Additional Supplied Modules and Extensions">Up</a></td><th width="60%" align="center">Appendix F. Additional Supplied Modules and Extensions</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="pgstattuple.html" title="F.33. pgstattuple — obtain tuple-level statistics">Next</a></td></tr></table><hr /></div><div class="sect1" id="PGSTATSTATEMENTS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">F.32. pg_stat_statements — track statistics of SQL planning and execution <a href="#PGSTATSTATEMENTS" class="id_link">#</a></h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="pgstatstatements.html#PGSTATSTATEMENTS-PG-STAT-STATEMENTS">F.32.1. The <code class="structname">pg_stat_statements</code> View</a></span></dt><dt><span class="sect2"><a href="pgstatstatements.html#PGSTATSTATEMENTS-PG-STAT-STATEMENTS-INFO">F.32.2. The <code class="structname">pg_stat_statements_info</code> View</a></span></dt><dt><span class="sect2"><a href="pgstatstatements.html#PGSTATSTATEMENTS-FUNCS">F.32.3. Functions</a></span></dt><dt><span class="sect2"><a href="pgstatstatements.html#PGSTATSTATEMENTS-CONFIG-PARAMS">F.32.4. Configuration Parameters</a></span></dt><dt><span class="sect2"><a href="pgstatstatements.html#PGSTATSTATEMENTS-SAMPLE-OUTPUT">F.32.5. Sample Output</a></span></dt><dt><span class="sect2"><a href="pgstatstatements.html#PGSTATSTATEMENTS-AUTHORS">F.32.6. Authors</a></span></dt></dl></div><a id="id-1.11.7.42.2" class="indexterm"></a><p>3  The <code class="filename">pg_stat_statements</code> module provides a means for4  tracking planning and execution statistics of all SQL statements executed by5  a server.6 </p><p>7  The module must be loaded by adding <code class="literal">pg_stat_statements</code> to8  <a class="xref" href="runtime-config-client.html#GUC-SHARED-PRELOAD-LIBRARIES">shared_preload_libraries</a> in9  <code class="filename">postgresql.conf</code>, because it requires additional shared memory.10  This means that a server restart is needed to add or remove the module.11  In addition, query identifier calculation must be enabled in order for the12  module to be active, which is done automatically if <a class="xref" href="runtime-config-statistics.html#GUC-COMPUTE-QUERY-ID">compute_query_id</a>13  is set to <code class="literal">auto</code> or <code class="literal">on</code>, or any third-party14  module that calculates query identifiers is loaded.15 </p><p>16   When <code class="filename">pg_stat_statements</code> is active, it tracks17   statistics across all databases of the server.  To access and manipulate18   these statistics, the module provides views19   <code class="structname">pg_stat_statements</code> and20   <code class="structname">pg_stat_statements_info</code>,21   and the utility functions <code class="function">pg_stat_statements_reset</code> and22   <code class="function">pg_stat_statements</code>.  These are not available globally but23   can be enabled for a specific database with24   <code class="command">CREATE EXTENSION pg_stat_statements</code>.25 </p><div class="sect2" id="PGSTATSTATEMENTS-PG-STAT-STATEMENTS"><div class="titlepage"><div><div><h3 class="title">F.32.1. The <code class="structname">pg_stat_statements</code> View <a href="#PGSTATSTATEMENTS-PG-STAT-STATEMENTS" class="id_link">#</a></h3></div></div></div><p>26   The statistics gathered by the module are made available via a27   view named <code class="structname">pg_stat_statements</code>.  This view28   contains one row for each distinct combination of database ID, user29   ID, query ID and whether it's a top-level statement or not (up to30   the maximum number of distinct statements that the module can track).31   The columns of the view are shown in32   <a class="xref" href="pgstatstatements.html#PGSTATSTATEMENTS-COLUMNS" title="Table F.22. pg_stat_statements Columns">Table F.22</a>.33  </p><div class="table" id="PGSTATSTATEMENTS-COLUMNS"><p class="title"><strong>Table F.22. <code class="structname">pg_stat_statements</code> Columns</strong></p><div class="table-contents"><table class="table" summary="pg_stat_statements Columns" border="1"><colgroup><col /></colgroup><thead><tr><th class="catalog_table_entry"><p class="column_definition">34       Column Type35      </p>36      <p>37       Description38      </p></th></tr></thead><tbody><tr><td class="catalog_table_entry"><p class="column_definition">39       <code class="structfield">userid</code> <code class="type">oid</code>40       (references <a class="link" href="catalog-pg-authid.html" title="53.8. pg_authid"><code class="structname">pg_authid</code></a>.<code class="structfield">oid</code>)41      </p>42      <p>43       OID of user who executed the statement44      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">45       <code class="structfield">dbid</code> <code class="type">oid</code>46       (references <a class="link" href="catalog-pg-database.html" title="53.15. pg_database"><code class="structname">pg_database</code></a>.<code class="structfield">oid</code>)47      </p>48      <p>49       OID of database in which the statement was executed50      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">51       <code class="structfield">toplevel</code> <code class="type">bool</code>52      </p>53      <p>54       True if the query was executed as a top-level statement55       (always true if <code class="varname">pg_stat_statements.track</code> is set to56       <code class="literal">top</code>)57      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">58       <code class="structfield">queryid</code> <code class="type">bigint</code>59      </p>60      <p>61       Hash code to identify identical normalized queries.62      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">63       <code class="structfield">query</code> <code class="type">text</code>64      </p>65      <p>66       Text of a representative statement67      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">68       <code class="structfield">plans</code> <code class="type">bigint</code>69      </p>70      <p>71       Number of times the statement was planned72       (if <code class="varname">pg_stat_statements.track_planning</code> is enabled,73       otherwise zero)74      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">75       <code class="structfield">total_plan_time</code> <code class="type">double precision</code>76      </p>77      <p>78       Total time spent planning the statement, in milliseconds79       (if <code class="varname">pg_stat_statements.track_planning</code> is enabled,80       otherwise zero)81      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">82       <code class="structfield">min_plan_time</code> <code class="type">double precision</code>83      </p>84      <p>85       Minimum time spent planning the statement, in milliseconds86       (if <code class="varname">pg_stat_statements.track_planning</code> is enabled,87       otherwise zero)88      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">89       <code class="structfield">max_plan_time</code> <code class="type">double precision</code>90      </p>91      <p>92       Maximum time spent planning the statement, in milliseconds93       (if <code class="varname">pg_stat_statements.track_planning</code> is enabled,94       otherwise zero)95      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">96       <code class="structfield">mean_plan_time</code> <code class="type">double precision</code>97      </p>98      <p>99       Mean time spent planning the statement, in milliseconds100       (if <code class="varname">pg_stat_statements.track_planning</code> is enabled,101       otherwise zero)102      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">103       <code class="structfield">stddev_plan_time</code> <code class="type">double precision</code>104      </p>105      <p>106       Population standard deviation of time spent planning the statement,107       in milliseconds108       (if <code class="varname">pg_stat_statements.track_planning</code> is enabled,109       otherwise zero)110      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">111       <code class="structfield">calls</code> <code class="type">bigint</code>112      </p>113      <p>114       Number of times the statement was executed115      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">116       <code class="structfield">total_exec_time</code> <code class="type">double precision</code>117      </p>118      <p>119       Total time spent executing the statement, in milliseconds120      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">121       <code class="structfield">min_exec_time</code> <code class="type">double precision</code>122      </p>123      <p>124       Minimum time spent executing the statement, in milliseconds125      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">126       <code class="structfield">max_exec_time</code> <code class="type">double precision</code>127      </p>128      <p>129       Maximum time spent executing the statement, in milliseconds130      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">131       <code class="structfield">mean_exec_time</code> <code class="type">double precision</code>132      </p>133      <p>134       Mean time spent executing the statement, in milliseconds135      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">136       <code class="structfield">stddev_exec_time</code> <code class="type">double precision</code>137      </p>138      <p>139       Population standard deviation of time spent executing the statement, in milliseconds140      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">141       <code class="structfield">rows</code> <code class="type">bigint</code>142      </p>143      <p>144       Total number of rows retrieved or affected by the statement145      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">146       <code class="structfield">shared_blks_hit</code> <code class="type">bigint</code>147      </p>148      <p>149       Total number of shared block cache hits by the statement150      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">151       <code class="structfield">shared_blks_read</code> <code class="type">bigint</code>152      </p>153      <p>154       Total number of shared blocks read by the statement155      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">156       <code class="structfield">shared_blks_dirtied</code> <code class="type">bigint</code>157      </p>158      <p>159       Total number of shared blocks dirtied by the statement160      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">161       <code class="structfield">shared_blks_written</code> <code class="type">bigint</code>162      </p>163      <p>164       Total number of shared blocks written by the statement165      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">166       <code class="structfield">local_blks_hit</code> <code class="type">bigint</code>167      </p>168      <p>169       Total number of local block cache hits by the statement170      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">171       <code class="structfield">local_blks_read</code> <code class="type">bigint</code>172      </p>173      <p>174       Total number of local blocks read by the statement175      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">176       <code class="structfield">local_blks_dirtied</code> <code class="type">bigint</code>177      </p>178      <p>179       Total number of local blocks dirtied by the statement180      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">181       <code class="structfield">local_blks_written</code> <code class="type">bigint</code>182      </p>183      <p>184       Total number of local blocks written by the statement185      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">186       <code class="structfield">temp_blks_read</code> <code class="type">bigint</code>187      </p>188      <p>189       Total number of temp blocks read by the statement190      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">191       <code class="structfield">temp_blks_written</code> <code class="type">bigint</code>192      </p>193      <p>194       Total number of temp blocks written by the statement195      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">196       <code class="structfield">blk_read_time</code> <code class="type">double precision</code>197      </p>198      <p>199       Total time the statement spent reading data file blocks, in milliseconds200       (if <a class="xref" href="runtime-config-statistics.html#GUC-TRACK-IO-TIMING">track_io_timing</a> is enabled, otherwise zero)201      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">202       <code class="structfield">blk_write_time</code> <code class="type">double precision</code>203      </p>204      <p>205       Total time the statement spent writing data file blocks, in milliseconds206       (if <a class="xref" href="runtime-config-statistics.html#GUC-TRACK-IO-TIMING">track_io_timing</a> is enabled, otherwise zero)207      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">208       <code class="structfield">temp_blk_read_time</code> <code class="type">double precision</code>209      </p>210      <p>211       Total time the statement spent reading temporary file blocks, in212       milliseconds (if <a class="xref" href="runtime-config-statistics.html#GUC-TRACK-IO-TIMING">track_io_timing</a> is enabled,213       otherwise zero)214      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">215       <code class="structfield">temp_blk_write_time</code> <code class="type">double precision</code>216      </p>217      <p>218       Total time the statement spent writing temporary file blocks, in219       milliseconds (if <a class="xref" href="runtime-config-statistics.html#GUC-TRACK-IO-TIMING">track_io_timing</a> is enabled,220       otherwise zero)221      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">222       <code class="structfield">wal_records</code> <code class="type">bigint</code>223      </p>224      <p>225       Total number of WAL records generated by the statement226      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">227       <code class="structfield">wal_fpi</code> <code class="type">bigint</code>228      </p>229      <p>230       Total number of WAL full page images generated by the statement231      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">232       <code class="structfield">wal_bytes</code> <code class="type">numeric</code>233      </p>234      <p>235       Total amount of WAL generated by the statement in bytes236      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">237       <code class="structfield">jit_functions</code> <code class="type">bigint</code>238      </p>239      <p>240       Total number of functions JIT-compiled by the statement241      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">242       <code class="structfield">jit_generation_time</code> <code class="type">double precision</code>243      </p>244      <p>245       Total time spent by the statement on generating JIT code, in milliseconds246      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">247       <code class="structfield">jit_inlining_count</code> <code class="type">bigint</code>248      </p>249      <p>250       Number of times functions have been inlined251      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">252       <code class="structfield">jit_inlining_time</code> <code class="type">double precision</code>253      </p>254      <p>255       Total time spent by the statement on inlining functions, in milliseconds256      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">257       <code class="structfield">jit_optimization_count</code> <code class="type">bigint</code>258      </p>259      <p>260       Number of times the statement has been optimized261      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">262       <code class="structfield">jit_optimization_time</code> <code class="type">double precision</code>263      </p>264      <p>265       Total time spent by the statement on optimizing, in milliseconds266      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">267       <code class="structfield">jit_emission_count</code> <code class="type">bigint</code>268      </p>269      <p>270       Number of times code has been emitted271      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">272       <code class="structfield">jit_emission_time</code> <code class="type">double precision</code>273      </p>274      <p>275       Total time spent by the statement on emitting code, in milliseconds276      </p></td></tr></tbody></table></div></div><br class="table-break" /><p>277   For security reasons, only superusers and roles with privileges of the278   <code class="literal">pg_read_all_stats</code> role are allowed to see the SQL text and279   <code class="structfield">queryid</code> of queries executed by other users.280   Other users can see the statistics, however, if the view has been installed281   in their database.282  </p><p>283   Plannable queries (that is, <code class="command">SELECT</code>, <code class="command">INSERT</code>,284   <code class="command">UPDATE</code>, <code class="command">DELETE</code>, and <code class="command">MERGE</code>)285   and utility commands are combined into a single286   <code class="structname">pg_stat_statements</code> entry whenever they have identical query287   structures according to an internal hash calculation.  Typically, two288   queries will be considered the same for this purpose if they are289   semantically equivalent except for the values of literal constants290   appearing in the query.291  </p><div class="note"><h3 class="title">Note</h3><p>292    The following details about constant replacement and293    <code class="structfield">queryid</code> only apply when <a class="xref" href="runtime-config-statistics.html#GUC-COMPUTE-QUERY-ID">compute_query_id</a> is enabled.  If you use an external294    module instead to compute <code class="structfield">queryid</code>, you295    should refer to its documentation for details.296   </p></div><p>297   When a constant's value has been ignored for purposes of matching the query298   to other queries, the constant is replaced by a parameter symbol, such299   as <code class="literal">$1</code>, in the <code class="structname">pg_stat_statements</code>300   display.301   The rest of the query text is that of the first query that had the302   particular <code class="structfield">queryid</code> hash value associated with the303   <code class="structname">pg_stat_statements</code> entry.304  </p><p>305   Queries on which normalization can be applied may be observed with constant306   values in <code class="structname">pg_stat_statements</code>, especially when there307   is a high rate of entry deallocations. To reduce the likelihood of this308   happening, consider increasing <code class="varname">pg_stat_statements.max</code>.309   The <code class="structname">pg_stat_statements_info</code> view, discussed below310   in <a class="xref" href="pgstatstatements.html#PGSTATSTATEMENTS-PG-STAT-STATEMENTS-INFO" title="F.32.2. The pg_stat_statements_info View">Section F.32.2</a>,311   provides statistics about entry deallocations.312  </p><p>313   In some cases, queries with visibly different texts might get merged into a314   single <code class="structname">pg_stat_statements</code> entry.  Normally this will happen315   only for semantically equivalent queries, but there is a small chance of316   hash collisions causing unrelated queries to be merged into one entry.317   (This cannot happen for queries belonging to different users or databases,318   however.)319  </p><p>320   Since the <code class="structfield">queryid</code> hash value is computed on the321   post-parse-analysis representation of the queries, the opposite is322   also possible: queries with identical texts might appear as323   separate entries, if they have different meanings as a result of324   factors such as different <code class="varname">search_path</code> settings.325  </p><p>326   Consumers of <code class="structname">pg_stat_statements</code> may wish to use327   <code class="structfield">queryid</code> (perhaps in combination with328   <code class="structfield">dbid</code> and <code class="structfield">userid</code>) as a more stable329   and reliable identifier for each entry than its query text.330   However, it is important to understand that there are only limited331   guarantees around the stability of the <code class="structfield">queryid</code> hash332   value.  Since the identifier is derived from the333   post-parse-analysis tree, its value is a function of, among other334   things, the internal object identifiers appearing in this representation.335   This has some counterintuitive implications.  For example,336   <code class="filename">pg_stat_statements</code> will consider two apparently-identical337   queries to be distinct, if they reference a table that was dropped338   and recreated between the executions of the two queries.339   The hashing process is also sensitive to differences in340   machine architecture and other facets of the platform.341   Furthermore, it is not safe to assume that <code class="structfield">queryid</code>342   will be stable across major versions of <span class="productname">PostgreSQL</span>.343  </p><p>344   Two servers participating in replication based on physical WAL replay can345   be expected to have identical <code class="structfield">queryid</code> values for346   the same query.  However, logical replication schemes do not promise to347   keep replicas identical in all relevant details, so348   <code class="structfield">queryid</code> will not be a useful identifier for349   accumulating costs across a set of logical replicas.350   If in doubt, direct testing is recommended.351  </p><p>352   Generally, it can be assumed that <code class="structfield">queryid</code> values353   are stable between minor version releases of <span class="productname">PostgreSQL</span>,354   providing that instances are running on the same machine architecture and355   the catalog metadata details match.  Compatibility will only be broken356   between minor versions as a last resort.357  </p><p>358   The parameter symbols used to replace constants in359   representative query texts start from the next number after the360   highest <code class="literal">$</code><em class="replaceable"><code>n</code></em> parameter in the original query361   text, or <code class="literal">$1</code> if there was none.  It's worth noting that in362   some cases there may be hidden parameter symbols that affect this363   numbering.  For example, <span class="application">PL/pgSQL</span> uses hidden parameter364   symbols to insert values of function local variables into queries, so that365   a <span class="application">PL/pgSQL</span> statement like <code class="literal">SELECT i + 1 INTO j</code>366   would have representative text like <code class="literal">SELECT i + $2</code>.367  </p><p>368   The representative query texts are kept in an external disk file, and do369   not consume shared memory.  Therefore, even very lengthy query texts can370   be stored successfully.  However, if many long query texts are371   accumulated, the external file might grow unmanageably large.  As a372   recovery method if that happens, <code class="filename">pg_stat_statements</code> may373   choose to discard the query texts, whereupon all existing entries in374   the <code class="structname">pg_stat_statements</code> view will show375   null <code class="structfield">query</code> fields, though the statistics associated with376   each <code class="structfield">queryid</code> are preserved.  If this happens, consider377   reducing <code class="varname">pg_stat_statements.max</code> to prevent378   recurrences.379  </p><p>380   <code class="structfield">plans</code> and <code class="structfield">calls</code> aren't381   always expected to match because planning and execution statistics are382   updated at their respective end phase, and only for successful operations.383   For example, if a statement is successfully planned but fails during384   the execution phase, only its planning statistics will be updated.385   If planning is skipped because a cached plan is used, only its execution386   statistics will be updated.387  </p></div><div class="sect2" id="PGSTATSTATEMENTS-PG-STAT-STATEMENTS-INFO"><div class="titlepage"><div><div><h3 class="title">F.32.2. The <code class="structname">pg_stat_statements_info</code> View <a href="#PGSTATSTATEMENTS-PG-STAT-STATEMENTS-INFO" class="id_link">#</a></h3></div></div></div><a id="id-1.11.7.42.7.2" class="indexterm"></a><p>388   The statistics of the <code class="filename">pg_stat_statements</code> module389   itself are tracked and made available via a view named390   <code class="structname">pg_stat_statements_info</code>.  This view contains391   only a single row.  The columns of the view are shown in392   <a class="xref" href="pgstatstatements.html#PGSTATSTATEMENTSINFO-COLUMNS" title="Table F.23. pg_stat_statements_info Columns">Table F.23</a>.393  </p><div class="table" id="PGSTATSTATEMENTSINFO-COLUMNS"><p class="title"><strong>Table F.23. <code class="structname">pg_stat_statements_info</code> Columns</strong></p><div class="table-contents"><table class="table" summary="pg_stat_statements_info Columns" border="1"><colgroup><col /></colgroup><thead><tr><th class="catalog_table_entry"><p class="column_definition">394       Column Type395      </p>396      <p>397       Description398      </p></th></tr></thead><tbody><tr><td class="catalog_table_entry"><p class="column_definition">399       <code class="structfield">dealloc</code> <code class="type">bigint</code>400      </p>401      <p>402       Total number of times <code class="structname">pg_stat_statements</code>403       entries about the least-executed statements were deallocated404       because more distinct statements than405       <code class="varname">pg_stat_statements.max</code> were observed406      </p></td></tr><tr><td class="catalog_table_entry"><p class="column_definition">407       <code class="structfield">stats_reset</code> <code class="type">timestamp with time zone</code>408      </p>409      <p>410       Time at which all statistics in the411       <code class="structname">pg_stat_statements</code> view were last reset.412      </p></td></tr></tbody></table></div></div><br class="table-break" /></div><div class="sect2" id="PGSTATSTATEMENTS-FUNCS"><div class="titlepage"><div><div><h3 class="title">F.32.3. Functions <a href="#PGSTATSTATEMENTS-FUNCS" class="id_link">#</a></h3></div></div></div><div class="variablelist"><dl class="variablelist"><dt><span class="term">413     <code class="function">pg_stat_statements_reset(userid Oid, dbid Oid, queryid bigint) returns void</code>414     <a id="id-1.11.7.42.8.2.1.1.2" class="indexterm"></a>415    </span></dt><dd><p>416      <code class="function">pg_stat_statements_reset</code> discards statistics417      gathered so far by <code class="filename">pg_stat_statements</code> corresponding418      to the specified <code class="structfield">userid</code>, <code class="structfield">dbid</code>419      and <code class="structfield">queryid</code>.  If any of the parameters are not420      specified, the default value <code class="literal">0</code>(invalid) is used for421      each of them and the statistics that match with other parameters will be422      reset.  If no parameter is specified or all the specified parameters are423      <code class="literal">0</code>(invalid), it will discard all statistics.424      If all statistics in the <code class="filename">pg_stat_statements</code>425      view are discarded, it will also reset the statistics in the426      <code class="structname">pg_stat_statements_info</code> view.427      By default, this function can only be executed by superusers.428      Access may be granted to others using <code class="command">GRANT</code>.429     </p></dd><dt><span class="term">430     <code class="function">pg_stat_statements(showtext boolean) returns setof record</code>431     <a id="id-1.11.7.42.8.2.2.1.2" class="indexterm"></a>432    </span></dt><dd><p>433      The <code class="structname">pg_stat_statements</code> view is defined in434      terms of a function also named <code class="function">pg_stat_statements</code>.435      It is possible for clients to call436      the <code class="function">pg_stat_statements</code> function directly, and by437      specifying <code class="literal">showtext := false</code> have query text be438      omitted (that is, the <code class="literal">OUT</code> argument that corresponds439      to the view's <code class="structfield">query</code> column will return nulls).  This440      feature is intended to support external tools that might wish to avoid441      the overhead of repeatedly retrieving query texts of indeterminate442      length.  Such tools can instead cache the first query text observed443      for each entry themselves, since that is444      all <code class="filename">pg_stat_statements</code> itself does, and then retrieve445      query texts only as needed.  Since the server stores query texts in a446      file, this approach may reduce physical I/O for repeated examination447      of the <code class="structname">pg_stat_statements</code> data.448     </p></dd></dl></div></div><div class="sect2" id="PGSTATSTATEMENTS-CONFIG-PARAMS"><div class="titlepage"><div><div><h3 class="title">F.32.4. Configuration Parameters <a href="#PGSTATSTATEMENTS-CONFIG-PARAMS" class="id_link">#</a></h3></div></div></div><div class="variablelist"><dl class="variablelist"><dt><span class="term">449     <code class="varname">pg_stat_statements.max</code> (<code class="type">integer</code>)450     <a id="id-1.11.7.42.9.2.1.1.3" class="indexterm"></a>451    </span></dt><dd><p>452      <code class="varname">pg_stat_statements.max</code> is the maximum number of453      statements tracked by the module (i.e., the maximum number of rows454      in the <code class="structname">pg_stat_statements</code> view).  If more distinct455      statements than that are observed, information about the least-executed456      statements is discarded.  The number of times such information was457      discarded can be seen in the458      <code class="structname">pg_stat_statements_info</code> view.459      The default value is 5000.460      This parameter can only be set at server start.461     </p></dd><dt><span class="term">462     <code class="varname">pg_stat_statements.track</code> (<code class="type">enum</code>)463     <a id="id-1.11.7.42.9.2.2.1.3" class="indexterm"></a>464    </span></dt><dd><p>465      <code class="varname">pg_stat_statements.track</code> controls which statements466      are counted by the module.467      Specify <code class="literal">top</code> to track top-level statements (those issued468      directly by clients), <code class="literal">all</code> to also track nested statements469      (such as statements invoked within functions), or <code class="literal">none</code> to470      disable statement statistics collection.471      The default value is <code class="literal">top</code>.472      Only superusers can change this setting.473     </p></dd><dt><span class="term">474     <code class="varname">pg_stat_statements.track_utility</code> (<code class="type">boolean</code>)475     <a id="id-1.11.7.42.9.2.3.1.3" class="indexterm"></a>476    </span></dt><dd><p>477      <code class="varname">pg_stat_statements.track_utility</code> controls whether478      utility commands are tracked by the module.  Utility commands are479      all those other than <code class="command">SELECT</code>, <code class="command">INSERT</code>,480      <code class="command">UPDATE</code>, <code class="command">DELETE</code>, and <code class="command">MERGE</code>.481      The default value is <code class="literal">on</code>.482      Only superusers can change this setting.483     </p></dd><dt><span class="term">484     <code class="varname">pg_stat_statements.track_planning</code> (<code class="type">boolean</code>)485     <a id="id-1.11.7.42.9.2.4.1.3" class="indexterm"></a>486    </span></dt><dd><p>487      <code class="varname">pg_stat_statements.track_planning</code> controls whether488      planning operations and duration are tracked by the module.489      Enabling this parameter may incur a noticeable performance penalty,490      especially when statements with identical query structure are executed491      by many concurrent connections which compete to update a small number of492      <code class="structname">pg_stat_statements</code> entries.493      The default value is <code class="literal">off</code>.494      Only superusers can change this setting.495     </p></dd><dt><span class="term">496     <code class="varname">pg_stat_statements.save</code> (<code class="type">boolean</code>)497     <a id="id-1.11.7.42.9.2.5.1.3" class="indexterm"></a>498    </span></dt><dd><p>499      <code class="varname">pg_stat_statements.save</code> specifies whether to500      save statement statistics across server shutdowns.501      If it is <code class="literal">off</code> then statistics are not saved at502      shutdown nor reloaded at server start.503      The default value is <code class="literal">on</code>.504      This parameter can only be set in the <code class="filename">postgresql.conf</code>505      file or on the server command line.506     </p></dd></dl></div><p>507   The module requires additional shared memory proportional to508   <code class="varname">pg_stat_statements.max</code>.  Note that this509   memory is consumed whenever the module is loaded, even if510   <code class="varname">pg_stat_statements.track</code> is set to <code class="literal">none</code>.511  </p><p>512   These parameters must be set in <code class="filename">postgresql.conf</code>.513   Typical usage might be:514 515</p><pre class="programlisting">516# postgresql.conf517shared_preload_libraries = 'pg_stat_statements'518 519compute_query_id = on520pg_stat_statements.max = 10000521pg_stat_statements.track = all522</pre><p>523  </p></div><div class="sect2" id="PGSTATSTATEMENTS-SAMPLE-OUTPUT"><div class="titlepage"><div><div><h3 class="title">F.32.5. Sample Output <a href="#PGSTATSTATEMENTS-SAMPLE-OUTPUT" class="id_link">#</a></h3></div></div></div><pre class="screen">524bench=# SELECT pg_stat_statements_reset();525 526$ pgbench -i bench527$ pgbench -c10 -t300 bench528 529bench=# \x530bench=# SELECT query, calls, total_exec_time, rows, 100.0 * shared_blks_hit /531               nullif(shared_blks_hit + shared_blks_read, 0) AS hit_percent532          FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 5;533-[ RECORD 1 ]---+--------------------------------------------------​------------------534query           | UPDATE pgbench_branches SET bbalance = bbalance + $1 WHERE bid = $2535calls           | 3000536total_exec_time | 25565.855387537rows            | 3000538hit_percent     | 100.0000000000000000539-[ RECORD 2 ]---+--------------------------------------------------​------------------540query           | UPDATE pgbench_tellers SET tbalance = tbalance + $1 WHERE tid = $2541calls           | 3000542total_exec_time | 20756.669379543rows            | 3000544hit_percent     | 100.0000000000000000545-[ RECORD 3 ]---+--------------------------------------------------​------------------546query           | copy pgbench_accounts from stdin547calls           | 1548total_exec_time | 291.865911549rows            | 100000550hit_percent     | 100.0000000000000000551-[ RECORD 4 ]---+--------------------------------------------------​------------------552query           | UPDATE pgbench_accounts SET abalance = abalance + $1 WHERE aid = $2553calls           | 3000554total_exec_time | 271.232977555rows            | 3000556hit_percent     | 98.8454011741682975557-[ RECORD 5 ]---+--------------------------------------------------​------------------558query           | alter table pgbench_accounts add primary key (aid)559calls           | 1560total_exec_time | 160.588563561rows            | 0562hit_percent     | 100.0000000000000000563 564 565bench=# SELECT pg_stat_statements_reset(0,0,s.queryid) FROM pg_stat_statements AS s566            WHERE s.query = 'UPDATE pgbench_branches SET bbalance = bbalance + $1 WHERE bid = $2';567 568bench=# SELECT query, calls, total_exec_time, rows, 100.0 * shared_blks_hit /569               nullif(shared_blks_hit + shared_blks_read, 0) AS hit_percent570          FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 5;571-[ RECORD 1 ]---+--------------------------------------------------​------------------572query           | UPDATE pgbench_tellers SET tbalance = tbalance + $1 WHERE tid = $2573calls           | 3000574total_exec_time | 20756.669379575rows            | 3000576hit_percent     | 100.0000000000000000577-[ RECORD 2 ]---+--------------------------------------------------​------------------578query           | copy pgbench_accounts from stdin579calls           | 1580total_exec_time | 291.865911581rows            | 100000582hit_percent     | 100.0000000000000000583-[ RECORD 3 ]---+--------------------------------------------------​------------------584query           | UPDATE pgbench_accounts SET abalance = abalance + $1 WHERE aid = $2585calls           | 3000586total_exec_time | 271.232977587rows            | 3000588hit_percent     | 98.8454011741682975589-[ RECORD 4 ]---+--------------------------------------------------​------------------590query           | alter table pgbench_accounts add primary key (aid)591calls           | 1592total_exec_time | 160.588563593rows            | 0594hit_percent     | 100.0000000000000000595-[ RECORD 5 ]---+--------------------------------------------------​------------------596query           | vacuum analyze pgbench_accounts597calls           | 1598total_exec_time | 136.448116599rows            | 0600hit_percent     | 99.9201915403032721601 602bench=# SELECT pg_stat_statements_reset(0,0,0);603 604bench=# SELECT query, calls, total_exec_time, rows, 100.0 * shared_blks_hit /605               nullif(shared_blks_hit + shared_blks_read, 0) AS hit_percent606          FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 5;607-[ RECORD 1 ]---+--------------------------------------------------​---------------------------608query           | SELECT pg_stat_statements_reset(0,0,0)609calls           | 1610total_exec_time | 0.189497611rows            | 1612hit_percent     |613-[ RECORD 2 ]---+--------------------------------------------------​---------------------------614query           | SELECT query, calls, total_exec_time, rows, $1 * shared_blks_hit /          +615                |                nullif(shared_blks_hit + shared_blks_read, $2) AS hit_percent+616                |           FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT $3617calls           | 0618total_exec_time | 0619rows            | 0620hit_percent     |621 622</pre></div><div class="sect2" id="PGSTATSTATEMENTS-AUTHORS"><div class="titlepage"><div><div><h3 class="title">F.32.6. Authors <a href="#PGSTATSTATEMENTS-AUTHORS" class="id_link">#</a></h3></div></div></div><p>623   Takahiro Itagaki <code class="email">&lt;<a class="email" href="mailto:itagaki.takahiro@oss.ntt.co.jp">itagaki.takahiro@oss.ntt.co.jp</a>&gt;</code>.624   Query normalization added by Peter Geoghegan <code class="email">&lt;<a class="email" href="mailto:peter@2ndquadrant.com">peter@2ndquadrant.com</a>&gt;</code>.625  </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="pgrowlocks.html" title="F.31. pgrowlocks — show a table's row locking information">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="contrib.html" title="Appendix F. Additional Supplied Modules and Extensions">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="pgstattuple.html" title="F.33. pgstattuple — obtain tuple-level statistics">Next</a></td></tr><tr><td width="40%" align="left" valign="top">F.31. pgrowlocks — show a table's row locking information </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> F.33. pgstattuple — obtain tuple-level statistics</td></tr></table></div></body></html>