information about
server activity. Presently, accesses to tables and indexes in both
disk-block and individual-row terms are counted. The total number of rows
in each table, and information about vacuum and analyze actions for each
table are also counted. If enabled, calls to user-defined functions and
the total time spent in each one are counted as well.
</para>
<para>
<productname>PostgreSQL</productname> also supports reporting dynamic
information about exactly what is going on in the system right now, such as
the exact command currently being executed by other server processes, and
which other connections exist in the system. This facility is independent
of the cumulative statistics system.
</para>
<sect2 id="monitoring-stats-setup">
<title>Statistics Collection Configuration</title>
<para>
Since collection of statistics adds some overhead to query execution,
the system can be configured to collect or not collect information.
This is controlled by configuration parameters that are normally set in
<filename>postgresql.conf</filename>. (See <xref linkend="runtime-config"/> for
details about setting configuration parameters.)
</para>
<para>
The parameter <xref linkend="guc-track-activities"/> enables monitoring
of the current command being executed by any server process.
</para>
<para>
The parameter <xref linkend="guc-track-cost-delay-timing"/> enables
monitoring of cost-based vacuum delay.
</para>
<para>
The parameter <xref linkend="guc-track-counts"/> controls whether
cumulative statistics are collected about table and index accesses.
</para>
<para>
The parameter <xref linkend="guc-track-functions"/> enables tracking of
usage of user-defined functions.
</para>
<para>
The parameter <xref linkend="guc-track-io-timing"/> enables monitoring
of block read, write, extend, and fsync times.
</para>
<para>
The parameter <xref linkend="guc-track-wal-io-timing"/> enables monitoring
of WAL read, write and fsync times.
</para>
<para>
Normally these parameters are set in <filename>postgresql.conf</filename> so
that they apply to all server processes, but it is possible to turn
them on or off in individual sessions using the <xref
linkend="sql-set"/> command. (To prevent
ordinary users from hiding their activity from the administrator,
only superusers are allowed to change these parameters with
<command>SET</command>.)
</para>
<para>
Cumulative statistics are collected in shared memory. Every
<productname>PostgreSQL</productname> process collects statistics locally,
then updates the shared data at appropriate intervals. When a server,
including a physical replica, shuts down cleanly, a permanent copy of the
statistics data is stored in the <filename>pg_stat</filename> subdirectory,
so that statistics can be retained across server restarts. In contrast,
when starting from an unclean shutdown (e.g., after an immediate shutdown,
a server crash, starting from a base backup, and point-in-time recovery),
all statistics counters are reset.
</para>
</sect2>
<sect2 id="monitoring-stats-views">
<title>Viewing Statistics</title>
<para>
Several predefined views, listed in <xref
linkend="monitoring-stats-dynamic-views-table"/>, are available to show
the current state of the system. There are also several other
views, listed in <xref
linkend="monitoring-stats-views-table"/>, available to show the accumulated
statistics. Alternatively, one can
build custom views using the underlying cumulative statistics functions, as
discussed in <xref linkend="monitoring-stats-functions"/>.
</para>
<para>
When using the cumulative statistics views and functions to monitor
collected data, it is important to realize that the information does not
update instantaneously. Each individual server process flushes out
accumulated statistics