2010-09-20 22:08:53 +02:00
|
|
|
<!-- doc/src/sgml/pgstatstatements.sgml -->
|
2009-01-04 23:19:59 +01:00
|
|
|
|
2011-05-08 04:29:20 +02:00
|
|
|
<sect1 id="pgstatstatements" xreflabel="pg_stat_statements">
|
2009-01-04 23:19:59 +01:00
|
|
|
<title>pg_stat_statements</title>
|
|
|
|
|
|
|
|
<indexterm zone="pgstatstatements">
|
|
|
|
<primary>pg_stat_statements</primary>
|
|
|
|
</indexterm>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The <filename>pg_stat_statements</filename> module provides a means for
|
|
|
|
tracking execution statistics of all SQL statements executed by a server.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The module must be loaded by adding <literal>pg_stat_statements</> to
|
|
|
|
<xref linkend="guc-shared-preload-libraries"> in
|
|
|
|
<filename>postgresql.conf</>, because it requires additional shared memory.
|
|
|
|
This means that a server restart is needed to add or remove the module.
|
|
|
|
</para>
|
|
|
|
|
2015-05-09 20:11:31 +02:00
|
|
|
<para>
|
|
|
|
When <filename>pg_stat_statements</filename> is loaded, it tracks
|
|
|
|
statistics across all databases of the server. To access and manipulate
|
|
|
|
these statistics, the module provides a view, <structname>pg_stat_statements</>,
|
|
|
|
and the utility functions <function>pg_stat_statements_reset</> and
|
|
|
|
<function>pg_stat_statements</>. These are not available globally but
|
|
|
|
can be enabled for a specific database with
|
|
|
|
<command>CREATE EXTENSION pg_stat_statements</>.
|
|
|
|
</para>
|
|
|
|
|
2009-01-04 23:19:59 +01:00
|
|
|
<sect2>
|
2011-01-29 19:00:18 +01:00
|
|
|
<title>The <structname>pg_stat_statements</structname> View</title>
|
2009-01-04 23:19:59 +01:00
|
|
|
|
|
|
|
<para>
|
2013-12-07 18:06:02 +01:00
|
|
|
The statistics gathered by the module are made available via a
|
2015-05-09 20:11:31 +02:00
|
|
|
view named <structname>pg_stat_statements</>. This view
|
2013-12-07 18:06:02 +01:00
|
|
|
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 in
|
|
|
|
<xref linkend="pgstatstatements-columns">.
|
2009-01-04 23:19:59 +01:00
|
|
|
</para>
|
|
|
|
|
2009-05-18 13:08:24 +02:00
|
|
|
<table id="pgstatstatements-columns">
|
2011-01-29 19:00:18 +01:00
|
|
|
<title><structname>pg_stat_statements</> Columns</title>
|
2009-01-04 23:19:59 +01:00
|
|
|
|
|
|
|
<tgroup cols="4">
|
|
|
|
<thead>
|
|
|
|
<row>
|
|
|
|
<entry>Name</entry>
|
|
|
|
<entry>Type</entry>
|
|
|
|
<entry>References</entry>
|
|
|
|
<entry>Description</entry>
|
|
|
|
</row>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
<row>
|
|
|
|
<entry><structfield>userid</structfield></entry>
|
|
|
|
<entry><type>oid</type></entry>
|
|
|
|
<entry><literal><link linkend="catalog-pg-authid"><structname>pg_authid</structname></link>.oid</literal></entry>
|
|
|
|
<entry>OID of user who executed the statement</entry>
|
|
|
|
</row>
|
|
|
|
|
|
|
|
<row>
|
|
|
|
<entry><structfield>dbid</structfield></entry>
|
|
|
|
<entry><type>oid</type></entry>
|
|
|
|
<entry><literal><link linkend="catalog-pg-database"><structname>pg_database</structname></link>.oid</literal></entry>
|
|
|
|
<entry>OID of database in which the statement was executed</entry>
|
|
|
|
</row>
|
|
|
|
|
2013-12-07 18:06:02 +01:00
|
|
|
<row>
|
|
|
|
<entry><structfield>queryid</structfield></entry>
|
|
|
|
<entry><type>bigint</type></entry>
|
|
|
|
<entry></entry>
|
Keep pg_stat_statements' query texts in a file, not in shared memory.
This change allows us to eliminate the previous limit on stored query
length, and it makes the shared-memory hash table very much smaller,
allowing more statements to be tracked. (The default value of
pg_stat_statements.max is therefore increased from 1000 to 5000.)
In typical scenarios, the hash table can be large enough to hold all the
statements commonly issued by an application, so that there is little
"churn" in the set of tracked statements, and thus little need to do I/O
to the file.
To further reduce the need for I/O to the query-texts file, add a way
to retrieve all the columns of the pg_stat_statements view except for
the query text column. This is probably not of much interest for human
use but it could be exploited by programs, which will prefer using the
queryid anyway.
Ordinarily, we'd need to bump the extension version number for the latter
change. But since we already advanced pg_stat_statements' version number
from 1.1 to 1.2 in the 9.4 development cycle, it seems all right to just
redefine what 1.2 means.
Peter Geoghegan, reviewed by Pavel Stehule
2014-01-27 21:37:54 +01:00
|
|
|
<entry>Internal hash code, computed from the statement's parse tree</entry>
|
2013-12-07 18:06:02 +01:00
|
|
|
</row>
|
|
|
|
|
|
|
|
<row>
|
2009-01-04 23:19:59 +01:00
|
|
|
<entry><structfield>query</structfield></entry>
|
|
|
|
<entry><type>text</type></entry>
|
|
|
|
<entry></entry>
|
Keep pg_stat_statements' query texts in a file, not in shared memory.
This change allows us to eliminate the previous limit on stored query
length, and it makes the shared-memory hash table very much smaller,
allowing more statements to be tracked. (The default value of
pg_stat_statements.max is therefore increased from 1000 to 5000.)
In typical scenarios, the hash table can be large enough to hold all the
statements commonly issued by an application, so that there is little
"churn" in the set of tracked statements, and thus little need to do I/O
to the file.
To further reduce the need for I/O to the query-texts file, add a way
to retrieve all the columns of the pg_stat_statements view except for
the query text column. This is probably not of much interest for human
use but it could be exploited by programs, which will prefer using the
queryid anyway.
Ordinarily, we'd need to bump the extension version number for the latter
change. But since we already advanced pg_stat_statements' version number
from 1.1 to 1.2 in the 9.4 development cycle, it seems all right to just
redefine what 1.2 means.
Peter Geoghegan, reviewed by Pavel Stehule
2014-01-27 21:37:54 +01:00
|
|
|
<entry>Text of a representative statement</entry>
|
2009-01-04 23:19:59 +01:00
|
|
|
</row>
|
|
|
|
|
|
|
|
<row>
|
|
|
|
<entry><structfield>calls</structfield></entry>
|
|
|
|
<entry><type>bigint</type></entry>
|
|
|
|
<entry></entry>
|
|
|
|
<entry>Number of times executed</entry>
|
|
|
|
</row>
|
|
|
|
|
|
|
|
<row>
|
|
|
|
<entry><structfield>total_time</structfield></entry>
|
|
|
|
<entry><type>double precision</type></entry>
|
|
|
|
<entry></entry>
|
2012-04-28 22:03:57 +02:00
|
|
|
<entry>Total time spent in the statement, in milliseconds</entry>
|
2009-01-04 23:19:59 +01:00
|
|
|
</row>
|
|
|
|
|
2015-03-27 20:43:22 +01:00
|
|
|
<row>
|
|
|
|
<entry><structfield>min_time</structfield></entry>
|
|
|
|
<entry><type>double precision</type></entry>
|
|
|
|
<entry></entry>
|
|
|
|
<entry>Minimum time spent in the statement, in milliseconds</entry>
|
|
|
|
</row>
|
|
|
|
|
|
|
|
<row>
|
|
|
|
<entry><structfield>max_time</structfield></entry>
|
|
|
|
<entry><type>double precision</type></entry>
|
|
|
|
<entry></entry>
|
|
|
|
<entry>Maximum time spent in the statement, in milliseconds</entry>
|
|
|
|
</row>
|
|
|
|
|
|
|
|
<row>
|
|
|
|
<entry><structfield>mean_time</structfield></entry>
|
|
|
|
<entry><type>double precision</type></entry>
|
|
|
|
<entry></entry>
|
|
|
|
<entry>Mean time spent in the statement, in milliseconds</entry>
|
|
|
|
</row>
|
|
|
|
|
|
|
|
<row>
|
|
|
|
<entry><structfield>stddev_time</structfield></entry>
|
|
|
|
<entry><type>double precision</type></entry>
|
|
|
|
<entry></entry>
|
|
|
|
<entry>Population standard deviation of time spent in the statement, in milliseconds</entry>
|
|
|
|
</row>
|
|
|
|
|
2009-01-04 23:19:59 +01:00
|
|
|
<row>
|
|
|
|
<entry><structfield>rows</structfield></entry>
|
|
|
|
<entry><type>bigint</type></entry>
|
|
|
|
<entry></entry>
|
|
|
|
<entry>Total number of rows retrieved or affected by the statement</entry>
|
|
|
|
</row>
|
|
|
|
|
2010-01-08 01:38:20 +01:00
|
|
|
<row>
|
|
|
|
<entry><structfield>shared_blks_hit</structfield></entry>
|
|
|
|
<entry><type>bigint</type></entry>
|
|
|
|
<entry></entry>
|
2012-04-28 22:03:57 +02:00
|
|
|
<entry>Total number of shared block cache hits by the statement</entry>
|
2010-01-08 01:38:20 +01:00
|
|
|
</row>
|
|
|
|
|
|
|
|
<row>
|
|
|
|
<entry><structfield>shared_blks_read</structfield></entry>
|
|
|
|
<entry><type>bigint</type></entry>
|
|
|
|
<entry></entry>
|
2012-04-28 22:03:57 +02:00
|
|
|
<entry>Total number of shared blocks read by the statement</entry>
|
2010-01-08 01:38:20 +01:00
|
|
|
</row>
|
|
|
|
|
2012-02-23 02:33:05 +01:00
|
|
|
<row>
|
|
|
|
<entry><structfield>shared_blks_dirtied</structfield></entry>
|
|
|
|
<entry><type>bigint</type></entry>
|
|
|
|
<entry></entry>
|
|
|
|
<entry>Total number of shared blocks dirtied by the statement</entry>
|
|
|
|
</row>
|
|
|
|
|
2010-01-08 01:38:20 +01:00
|
|
|
<row>
|
|
|
|
<entry><structfield>shared_blks_written</structfield></entry>
|
|
|
|
<entry><type>bigint</type></entry>
|
|
|
|
<entry></entry>
|
2012-04-28 22:03:57 +02:00
|
|
|
<entry>Total number of shared blocks written by the statement</entry>
|
2010-01-08 01:38:20 +01:00
|
|
|
</row>
|
|
|
|
|
|
|
|
<row>
|
|
|
|
<entry><structfield>local_blks_hit</structfield></entry>
|
|
|
|
<entry><type>bigint</type></entry>
|
|
|
|
<entry></entry>
|
2012-04-28 22:03:57 +02:00
|
|
|
<entry>Total number of local block cache hits by the statement</entry>
|
2010-01-08 01:38:20 +01:00
|
|
|
</row>
|
|
|
|
|
|
|
|
<row>
|
|
|
|
<entry><structfield>local_blks_read</structfield></entry>
|
|
|
|
<entry><type>bigint</type></entry>
|
|
|
|
<entry></entry>
|
2012-04-28 22:03:57 +02:00
|
|
|
<entry>Total number of local blocks read by the statement</entry>
|
2010-01-08 01:38:20 +01:00
|
|
|
</row>
|
|
|
|
|
2012-02-23 02:33:05 +01:00
|
|
|
<row>
|
|
|
|
<entry><structfield>local_blks_dirtied</structfield></entry>
|
|
|
|
<entry><type>bigint</type></entry>
|
|
|
|
<entry></entry>
|
|
|
|
<entry>Total number of local blocks dirtied by the statement</entry>
|
|
|
|
</row>
|
|
|
|
|
2010-01-08 01:38:20 +01:00
|
|
|
<row>
|
|
|
|
<entry><structfield>local_blks_written</structfield></entry>
|
|
|
|
<entry><type>bigint</type></entry>
|
|
|
|
<entry></entry>
|
2012-04-28 22:03:57 +02:00
|
|
|
<entry>Total number of local blocks written by the statement</entry>
|
2010-01-08 01:38:20 +01:00
|
|
|
</row>
|
|
|
|
|
|
|
|
<row>
|
|
|
|
<entry><structfield>temp_blks_read</structfield></entry>
|
|
|
|
<entry><type>bigint</type></entry>
|
|
|
|
<entry></entry>
|
2012-04-28 22:03:57 +02:00
|
|
|
<entry>Total number of temp blocks read by the statement</entry>
|
2010-01-08 01:38:20 +01:00
|
|
|
</row>
|
|
|
|
|
|
|
|
<row>
|
|
|
|
<entry><structfield>temp_blks_written</structfield></entry>
|
|
|
|
<entry><type>bigint</type></entry>
|
|
|
|
<entry></entry>
|
2012-04-28 22:03:57 +02:00
|
|
|
<entry>Total number of temp blocks written by the statement</entry>
|
2010-01-08 01:38:20 +01:00
|
|
|
</row>
|
|
|
|
|
2012-03-27 21:17:22 +02:00
|
|
|
<row>
|
2012-04-30 00:13:33 +02:00
|
|
|
<entry><structfield>blk_read_time</structfield></entry>
|
2012-03-27 21:17:22 +02:00
|
|
|
<entry><type>double precision</type></entry>
|
|
|
|
<entry></entry>
|
|
|
|
<entry>
|
2012-04-28 22:03:57 +02:00
|
|
|
Total time the statement spent reading blocks, in milliseconds
|
2012-04-29 22:23:54 +02:00
|
|
|
(if <xref linkend="guc-track-io-timing"> is enabled, otherwise zero)
|
2012-03-27 21:17:22 +02:00
|
|
|
</entry>
|
|
|
|
</row>
|
|
|
|
|
|
|
|
<row>
|
2012-04-30 00:13:33 +02:00
|
|
|
<entry><structfield>blk_write_time</structfield></entry>
|
2012-03-27 21:17:22 +02:00
|
|
|
<entry><type>double precision</type></entry>
|
|
|
|
<entry></entry>
|
|
|
|
<entry>
|
2012-04-28 22:03:57 +02:00
|
|
|
Total time the statement spent writing blocks, in milliseconds
|
2012-04-29 22:23:54 +02:00
|
|
|
(if <xref linkend="guc-track-io-timing"> is enabled, otherwise zero)
|
2012-03-27 21:17:22 +02:00
|
|
|
</entry>
|
|
|
|
</row>
|
|
|
|
|
2009-01-04 23:19:59 +01:00
|
|
|
</tbody>
|
|
|
|
</tgroup>
|
|
|
|
</table>
|
|
|
|
|
|
|
|
<para>
|
2013-12-07 18:06:02 +01:00
|
|
|
For security reasons, non-superusers are not allowed to see the SQL
|
2015-05-09 20:11:31 +02:00
|
|
|
text or <structfield>queryid</structfield> of queries executed by other users.
|
|
|
|
They can see the statistics, however, if the view has been installed in their
|
2013-12-07 18:06:02 +01:00
|
|
|
database.
|
2009-01-04 23:19:59 +01:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2012-03-29 03:00:31 +02:00
|
|
|
Plannable queries (that is, <command>SELECT</>, <command>INSERT</>,
|
|
|
|
<command>UPDATE</>, and <command>DELETE</>) are combined into a single
|
|
|
|
<structname>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. Utility commands (that is, all other commands)
|
|
|
|
are compared strictly on the basis of their textual query strings, however.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
When a constant's value has been ignored for purposes of matching the
|
|
|
|
query to other queries, the constant is replaced by <literal>?</literal>
|
|
|
|
in the <structname>pg_stat_statements</> display. The rest of the query
|
2013-12-07 18:06:02 +01:00
|
|
|
text is that of the first query that had the particular
|
|
|
|
<structfield>queryid</> hash value associated with the
|
|
|
|
<structname>pg_stat_statements</> entry.
|
2012-03-29 03:00:31 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
In some cases, queries with visibly different texts might get merged into a
|
|
|
|
single <structname>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.)
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2013-12-07 18:06:02 +01:00
|
|
|
Since the <structfield>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 <varname>search_path</> settings.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2015-10-05 18:44:12 +02:00
|
|
|
Consumers of <structname>pg_stat_statements</> may wish to use
|
Keep pg_stat_statements' query texts in a file, not in shared memory.
This change allows us to eliminate the previous limit on stored query
length, and it makes the shared-memory hash table very much smaller,
allowing more statements to be tracked. (The default value of
pg_stat_statements.max is therefore increased from 1000 to 5000.)
In typical scenarios, the hash table can be large enough to hold all the
statements commonly issued by an application, so that there is little
"churn" in the set of tracked statements, and thus little need to do I/O
to the file.
To further reduce the need for I/O to the query-texts file, add a way
to retrieve all the columns of the pg_stat_statements view except for
the query text column. This is probably not of much interest for human
use but it could be exploited by programs, which will prefer using the
queryid anyway.
Ordinarily, we'd need to bump the extension version number for the latter
change. But since we already advanced pg_stat_statements' version number
from 1.1 to 1.2 in the 9.4 development cycle, it seems all right to just
redefine what 1.2 means.
Peter Geoghegan, reviewed by Pavel Stehule
2014-01-27 21:37:54 +01:00
|
|
|
<structfield>queryid</> (perhaps in combination with
|
2013-12-07 18:06:02 +01:00
|
|
|
<structfield>dbid</> and <structfield>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 <structfield>queryid</> hash
|
|
|
|
value. Since the identifier is derived from the
|
|
|
|
post-parse-analysis tree, its value is a function of, among other
|
Keep pg_stat_statements' query texts in a file, not in shared memory.
This change allows us to eliminate the previous limit on stored query
length, and it makes the shared-memory hash table very much smaller,
allowing more statements to be tracked. (The default value of
pg_stat_statements.max is therefore increased from 1000 to 5000.)
In typical scenarios, the hash table can be large enough to hold all the
statements commonly issued by an application, so that there is little
"churn" in the set of tracked statements, and thus little need to do I/O
to the file.
To further reduce the need for I/O to the query-texts file, add a way
to retrieve all the columns of the pg_stat_statements view except for
the query text column. This is probably not of much interest for human
use but it could be exploited by programs, which will prefer using the
queryid anyway.
Ordinarily, we'd need to bump the extension version number for the latter
change. But since we already advanced pg_stat_statements' version number
from 1.1 to 1.2 in the 9.4 development cycle, it seems all right to just
redefine what 1.2 means.
Peter Geoghegan, reviewed by Pavel Stehule
2014-01-27 21:37:54 +01:00
|
|
|
things, the internal object identifiers appearing in this representation.
|
|
|
|
This has some counterintuitive implications. For example,
|
2015-10-05 18:44:12 +02:00
|
|
|
<filename>pg_stat_statements</> will consider two apparently-identical
|
Keep pg_stat_statements' query texts in a file, not in shared memory.
This change allows us to eliminate the previous limit on stored query
length, and it makes the shared-memory hash table very much smaller,
allowing more statements to be tracked. (The default value of
pg_stat_statements.max is therefore increased from 1000 to 5000.)
In typical scenarios, the hash table can be large enough to hold all the
statements commonly issued by an application, so that there is little
"churn" in the set of tracked statements, and thus little need to do I/O
to the file.
To further reduce the need for I/O to the query-texts file, add a way
to retrieve all the columns of the pg_stat_statements view except for
the query text column. This is probably not of much interest for human
use but it could be exploited by programs, which will prefer using the
queryid anyway.
Ordinarily, we'd need to bump the extension version number for the latter
change. But since we already advanced pg_stat_statements' version number
from 1.1 to 1.2 in the 9.4 development cycle, it seems all right to just
redefine what 1.2 means.
Peter Geoghegan, reviewed by Pavel Stehule
2014-01-27 21:37:54 +01:00
|
|
|
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
|
2013-12-07 18:06:02 +01:00
|
|
|
machine architecture and other facets of the platform.
|
|
|
|
Furthermore, it is not safe to assume that <structfield>queryid</>
|
|
|
|
will be stable across major versions of <productname>PostgreSQL</>.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
Keep pg_stat_statements' query texts in a file, not in shared memory.
This change allows us to eliminate the previous limit on stored query
length, and it makes the shared-memory hash table very much smaller,
allowing more statements to be tracked. (The default value of
pg_stat_statements.max is therefore increased from 1000 to 5000.)
In typical scenarios, the hash table can be large enough to hold all the
statements commonly issued by an application, so that there is little
"churn" in the set of tracked statements, and thus little need to do I/O
to the file.
To further reduce the need for I/O to the query-texts file, add a way
to retrieve all the columns of the pg_stat_statements view except for
the query text column. This is probably not of much interest for human
use but it could be exploited by programs, which will prefer using the
queryid anyway.
Ordinarily, we'd need to bump the extension version number for the latter
change. But since we already advanced pg_stat_statements' version number
from 1.1 to 1.2 in the 9.4 development cycle, it seems all right to just
redefine what 1.2 means.
Peter Geoghegan, reviewed by Pavel Stehule
2014-01-27 21:37:54 +01:00
|
|
|
As a rule of thumb, <structfield>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
|
2013-12-07 23:08:17 +01:00
|
|
|
to have identical <structfield>queryid</> values for the same query.
|
Keep pg_stat_statements' query texts in a file, not in shared memory.
This change allows us to eliminate the previous limit on stored query
length, and it makes the shared-memory hash table very much smaller,
allowing more statements to be tracked. (The default value of
pg_stat_statements.max is therefore increased from 1000 to 5000.)
In typical scenarios, the hash table can be large enough to hold all the
statements commonly issued by an application, so that there is little
"churn" in the set of tracked statements, and thus little need to do I/O
to the file.
To further reduce the need for I/O to the query-texts file, add a way
to retrieve all the columns of the pg_stat_statements view except for
the query text column. This is probably not of much interest for human
use but it could be exploited by programs, which will prefer using the
queryid anyway.
Ordinarily, we'd need to bump the extension version number for the latter
change. But since we already advanced pg_stat_statements' version number
from 1.1 to 1.2 in the 9.4 development cycle, it seems all right to just
redefine what 1.2 means.
Peter Geoghegan, reviewed by Pavel Stehule
2014-01-27 21:37:54 +01:00
|
|
|
However, logical replication schemes do not promise to keep replicas
|
|
|
|
identical in all relevant details, so <structfield>queryid</> will
|
|
|
|
not be a useful identifier for accumulating costs across a set of logical
|
|
|
|
replicas. If in doubt, direct testing is recommended.
|
2009-01-04 23:19:59 +01:00
|
|
|
</para>
|
2015-10-05 18:44:12 +02:00
|
|
|
|
|
|
|
<para>
|
|
|
|
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, <filename>pg_stat_statements</> may
|
|
|
|
choose to discard the query texts, whereupon all existing entries in
|
|
|
|
the <structname>pg_stat_statements</> view will show
|
|
|
|
null <structfield>query</> fields, though the statistics associated with
|
|
|
|
each <structfield>queryid</> are preserved. If this happens, consider
|
|
|
|
reducing <varname>pg_stat_statements.max</varname> to prevent
|
|
|
|
recurrences.
|
|
|
|
</para>
|
2009-01-04 23:19:59 +01:00
|
|
|
</sect2>
|
|
|
|
|
|
|
|
<sect2>
|
|
|
|
<title>Functions</title>
|
|
|
|
|
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
|
|
|
<term>
|
|
|
|
<function>pg_stat_statements_reset() returns void</function>
|
2014-05-07 03:28:58 +02:00
|
|
|
<indexterm>
|
|
|
|
<primary>pg_stat_statements_reset</primary>
|
|
|
|
</indexterm>
|
2009-01-04 23:19:59 +01:00
|
|
|
</term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
<function>pg_stat_statements_reset</function> discards all statistics
|
|
|
|
gathered so far by <filename>pg_stat_statements</>.
|
|
|
|
By default, this function can only be executed by superusers.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
Keep pg_stat_statements' query texts in a file, not in shared memory.
This change allows us to eliminate the previous limit on stored query
length, and it makes the shared-memory hash table very much smaller,
allowing more statements to be tracked. (The default value of
pg_stat_statements.max is therefore increased from 1000 to 5000.)
In typical scenarios, the hash table can be large enough to hold all the
statements commonly issued by an application, so that there is little
"churn" in the set of tracked statements, and thus little need to do I/O
to the file.
To further reduce the need for I/O to the query-texts file, add a way
to retrieve all the columns of the pg_stat_statements view except for
the query text column. This is probably not of much interest for human
use but it could be exploited by programs, which will prefer using the
queryid anyway.
Ordinarily, we'd need to bump the extension version number for the latter
change. But since we already advanced pg_stat_statements' version number
from 1.1 to 1.2 in the 9.4 development cycle, it seems all right to just
redefine what 1.2 means.
Peter Geoghegan, reviewed by Pavel Stehule
2014-01-27 21:37:54 +01:00
|
|
|
<varlistentry>
|
|
|
|
<term>
|
|
|
|
<function>pg_stat_statements(showtext boolean) returns setof record</function>
|
2014-05-07 03:28:58 +02:00
|
|
|
<indexterm>
|
|
|
|
<primary>pg_stat_statements</primary>
|
|
|
|
<secondary>function</secondary>
|
|
|
|
</indexterm>
|
Keep pg_stat_statements' query texts in a file, not in shared memory.
This change allows us to eliminate the previous limit on stored query
length, and it makes the shared-memory hash table very much smaller,
allowing more statements to be tracked. (The default value of
pg_stat_statements.max is therefore increased from 1000 to 5000.)
In typical scenarios, the hash table can be large enough to hold all the
statements commonly issued by an application, so that there is little
"churn" in the set of tracked statements, and thus little need to do I/O
to the file.
To further reduce the need for I/O to the query-texts file, add a way
to retrieve all the columns of the pg_stat_statements view except for
the query text column. This is probably not of much interest for human
use but it could be exploited by programs, which will prefer using the
queryid anyway.
Ordinarily, we'd need to bump the extension version number for the latter
change. But since we already advanced pg_stat_statements' version number
from 1.1 to 1.2 in the 9.4 development cycle, it seems all right to just
redefine what 1.2 means.
Peter Geoghegan, reviewed by Pavel Stehule
2014-01-27 21:37:54 +01:00
|
|
|
</term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The <structname>pg_stat_statements</structname> view is defined in
|
|
|
|
terms of a function also named <function>pg_stat_statements</>.
|
|
|
|
It is possible for clients to call
|
|
|
|
the <function>pg_stat_statements</function> function directly, and by
|
|
|
|
specifying <literal>showtext := false</literal> have query text be
|
|
|
|
omitted (that is, the <literal>OUT</literal> argument that corresponds
|
|
|
|
to the view's <structfield>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 <filename>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 <structname>pg_stat_statements</structname> data.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2009-01-04 23:19:59 +01:00
|
|
|
</variablelist>
|
|
|
|
</sect2>
|
|
|
|
|
|
|
|
<sect2>
|
2011-01-29 19:00:18 +01:00
|
|
|
<title>Configuration Parameters</title>
|
2009-01-04 23:19:59 +01:00
|
|
|
|
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
|
|
|
<term>
|
|
|
|
<varname>pg_stat_statements.max</varname> (<type>integer</type>)
|
|
|
|
</term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
<varname>pg_stat_statements.max</varname> is the maximum number of
|
|
|
|
statements tracked by the module (i.e., the maximum number of rows
|
|
|
|
in the <structname>pg_stat_statements</> view). If more distinct
|
|
|
|
statements than that are observed, information about the least-executed
|
|
|
|
statements is discarded.
|
Keep pg_stat_statements' query texts in a file, not in shared memory.
This change allows us to eliminate the previous limit on stored query
length, and it makes the shared-memory hash table very much smaller,
allowing more statements to be tracked. (The default value of
pg_stat_statements.max is therefore increased from 1000 to 5000.)
In typical scenarios, the hash table can be large enough to hold all the
statements commonly issued by an application, so that there is little
"churn" in the set of tracked statements, and thus little need to do I/O
to the file.
To further reduce the need for I/O to the query-texts file, add a way
to retrieve all the columns of the pg_stat_statements view except for
the query text column. This is probably not of much interest for human
use but it could be exploited by programs, which will prefer using the
queryid anyway.
Ordinarily, we'd need to bump the extension version number for the latter
change. But since we already advanced pg_stat_statements' version number
from 1.1 to 1.2 in the 9.4 development cycle, it seems all right to just
redefine what 1.2 means.
Peter Geoghegan, reviewed by Pavel Stehule
2014-01-27 21:37:54 +01:00
|
|
|
The default value is 5000.
|
2009-01-04 23:19:59 +01:00
|
|
|
This parameter can only be set at server start.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term>
|
|
|
|
<varname>pg_stat_statements.track</varname> (<type>enum</type>)
|
|
|
|
</term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
<varname>pg_stat_statements.track</varname> controls which statements
|
|
|
|
are counted by the module.
|
|
|
|
Specify <literal>top</> to track top-level statements (those issued
|
|
|
|
directly by clients), <literal>all</> to also track nested statements
|
|
|
|
(such as statements invoked within functions), or <literal>none</> to
|
2012-04-28 22:03:57 +02:00
|
|
|
disable statement statistics collection.
|
2009-01-04 23:19:59 +01:00
|
|
|
The default value is <literal>top</>.
|
|
|
|
Only superusers can change this setting.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
2009-12-15 21:04:49 +01:00
|
|
|
<varlistentry>
|
|
|
|
<term>
|
|
|
|
<varname>pg_stat_statements.track_utility</varname> (<type>boolean</type>)
|
|
|
|
</term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
<varname>pg_stat_statements.track_utility</varname> controls whether
|
|
|
|
utility commands are tracked by the module. Utility commands are
|
|
|
|
all those other than <command>SELECT</>, <command>INSERT</>,
|
|
|
|
<command>UPDATE</> and <command>DELETE</>.
|
|
|
|
The default value is <literal>on</>.
|
|
|
|
Only superusers can change this setting.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
2009-01-04 23:19:59 +01:00
|
|
|
<varlistentry>
|
|
|
|
<term>
|
|
|
|
<varname>pg_stat_statements.save</varname> (<type>boolean</type>)
|
|
|
|
</term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
<varname>pg_stat_statements.save</varname> specifies whether to
|
|
|
|
save statement statistics across server shutdowns.
|
|
|
|
If it is <literal>off</> then statistics are not saved at
|
|
|
|
shutdown nor reloaded at server start.
|
|
|
|
The default value is <literal>on</>.
|
|
|
|
This parameter can only be set in the <filename>postgresql.conf</>
|
|
|
|
file or on the server command line.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
|
|
|
|
|
|
|
<para>
|
Keep pg_stat_statements' query texts in a file, not in shared memory.
This change allows us to eliminate the previous limit on stored query
length, and it makes the shared-memory hash table very much smaller,
allowing more statements to be tracked. (The default value of
pg_stat_statements.max is therefore increased from 1000 to 5000.)
In typical scenarios, the hash table can be large enough to hold all the
statements commonly issued by an application, so that there is little
"churn" in the set of tracked statements, and thus little need to do I/O
to the file.
To further reduce the need for I/O to the query-texts file, add a way
to retrieve all the columns of the pg_stat_statements view except for
the query text column. This is probably not of much interest for human
use but it could be exploited by programs, which will prefer using the
queryid anyway.
Ordinarily, we'd need to bump the extension version number for the latter
change. But since we already advanced pg_stat_statements' version number
from 1.1 to 1.2 in the 9.4 development cycle, it seems all right to just
redefine what 1.2 means.
Peter Geoghegan, reviewed by Pavel Stehule
2014-01-27 21:37:54 +01:00
|
|
|
The module requires additional shared memory proportional to
|
|
|
|
<varname>pg_stat_statements.max</varname>. Note that this
|
2009-01-04 23:19:59 +01:00
|
|
|
memory is consumed whenever the module is loaded, even if
|
|
|
|
<varname>pg_stat_statements.track</> is set to <literal>none</>.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2011-10-04 18:36:18 +02:00
|
|
|
These parameters must be set in <filename>postgresql.conf</>.
|
|
|
|
Typical usage might be:
|
2009-01-04 23:19:59 +01:00
|
|
|
|
2010-07-29 21:34:41 +02:00
|
|
|
<programlisting>
|
2009-01-04 23:19:59 +01:00
|
|
|
# postgresql.conf
|
|
|
|
shared_preload_libraries = 'pg_stat_statements'
|
|
|
|
|
|
|
|
pg_stat_statements.max = 10000
|
|
|
|
pg_stat_statements.track = all
|
2010-07-29 21:34:41 +02:00
|
|
|
</programlisting>
|
|
|
|
</para>
|
2009-01-04 23:19:59 +01:00
|
|
|
</sect2>
|
|
|
|
|
|
|
|
<sect2>
|
2011-01-29 19:00:18 +01:00
|
|
|
<title>Sample Output</title>
|
2009-01-04 23:19:59 +01:00
|
|
|
|
2010-07-29 21:34:41 +02:00
|
|
|
<screen>
|
2010-01-08 01:38:20 +01:00
|
|
|
bench=# SELECT pg_stat_statements_reset();
|
2009-01-04 23:19:59 +01:00
|
|
|
|
2010-01-08 01:38:20 +01:00
|
|
|
$ pgbench -i bench
|
2012-03-29 03:00:31 +02:00
|
|
|
$ pgbench -c10 -t300 bench
|
2009-01-04 23:19:59 +01:00
|
|
|
|
2010-01-08 01:38:20 +01:00
|
|
|
bench=# \x
|
|
|
|
bench=# 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 ]---------------------------------------------------------------------
|
2012-03-29 03:00:31 +02:00
|
|
|
query | UPDATE pgbench_branches SET bbalance = bbalance + ? WHERE bid = ?;
|
2010-01-08 01:38:20 +01:00
|
|
|
calls | 3000
|
2012-04-28 22:03:57 +02:00
|
|
|
total_time | 9609.00100000002
|
2010-01-08 01:38:20 +01:00
|
|
|
rows | 2836
|
|
|
|
hit_percent | 99.9778970000200936
|
|
|
|
-[ RECORD 2 ]---------------------------------------------------------------------
|
2012-03-29 03:00:31 +02:00
|
|
|
query | UPDATE pgbench_tellers SET tbalance = tbalance + ? WHERE tid = ?;
|
2010-01-08 01:38:20 +01:00
|
|
|
calls | 3000
|
2012-04-28 22:03:57 +02:00
|
|
|
total_time | 8015.156
|
2010-01-08 01:38:20 +01:00
|
|
|
rows | 2990
|
|
|
|
hit_percent | 99.9731126579631345
|
|
|
|
-[ RECORD 3 ]---------------------------------------------------------------------
|
|
|
|
query | copy pgbench_accounts from stdin
|
|
|
|
calls | 1
|
2012-04-28 22:03:57 +02:00
|
|
|
total_time | 310.624
|
2010-01-08 01:38:20 +01:00
|
|
|
rows | 100000
|
|
|
|
hit_percent | 0.30395136778115501520
|
|
|
|
-[ RECORD 4 ]---------------------------------------------------------------------
|
2012-03-29 03:00:31 +02:00
|
|
|
query | UPDATE pgbench_accounts SET abalance = abalance + ? WHERE aid = ?;
|
2010-01-08 01:38:20 +01:00
|
|
|
calls | 3000
|
2012-04-28 22:03:57 +02:00
|
|
|
total_time | 271.741999999997
|
2010-01-08 01:38:20 +01:00
|
|
|
rows | 3000
|
|
|
|
hit_percent | 93.7968855088209426
|
|
|
|
-[ RECORD 5 ]---------------------------------------------------------------------
|
|
|
|
query | alter table pgbench_accounts add primary key (aid)
|
|
|
|
calls | 1
|
2012-04-28 22:03:57 +02:00
|
|
|
total_time | 81.42
|
2010-01-08 01:38:20 +01:00
|
|
|
rows | 0
|
|
|
|
hit_percent | 34.4947735191637631
|
2010-07-29 21:34:41 +02:00
|
|
|
</screen>
|
2009-01-04 23:19:59 +01:00
|
|
|
</sect2>
|
|
|
|
|
|
|
|
<sect2>
|
2012-04-13 22:42:24 +02:00
|
|
|
<title>Authors</title>
|
2009-01-04 23:19:59 +01:00
|
|
|
|
|
|
|
<para>
|
2012-04-13 22:42:24 +02:00
|
|
|
Takahiro Itagaki <email>itagaki.takahiro@oss.ntt.co.jp</email>.
|
|
|
|
Query normalization added by Peter Geoghegan <email>peter@2ndquadrant.com</email>.
|
2009-01-04 23:19:59 +01:00
|
|
|
</para>
|
|
|
|
</sect2>
|
|
|
|
|
|
|
|
</sect1>
|