RefLib

Reference library for AI

Back

postgresql_16:4.13.2.2 Viewing Statistics

PostgreSQL 16 > Server Administration > Monitoring Database Activity > The Cumulative Statistics System > Viewing Statistics

Several predefined views, listed in (see monitoring-stats-dynamic-views-table), are available to show the current state of the system. There are also several other views, listed in (see 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 (see postgresql_16:4.13.2.25).

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 to shared memory just before going idle, but not more frequently than once per PGSTAT_MIN_INTERVAL milliseconds (1 second unless altered while building the server); so a query or transaction still in progress does not affect the displayed totals and the displayed information lags behind actual activity. However, current-query information collected by track_activities is always up-to-date.

Another important point is that when a server process is asked to display any of the accumulated statistics, accessed values are cached until the end of its current transaction in the default configuration. So the statistics will show static information as long as you continue the current transaction. Similarly, information about the current queries of all sessions is collected when any such information is first requested within a transaction, and the same information will be displayed throughout the transaction. This is a feature, not a bug, because it allows you to perform several queries on the statistics and correlate the results without worrying that the numbers are changing underneath you. When analyzing statistics interactively, or with expensive queries, the time delta between accesses to individual statistics can lead to significant skew in the cached statistics. To minimize skew, stats_fetch_consistency can be set to snapshot, at the price of increased memory usage for caching not-needed statistics data. Conversely, if it's known that statistics are only accessed once, caching accessed statistics is unnecessary and can be avoided by setting stats_fetch_consistency to none. You can invoke pg_stat_clear_snapshot() to discard the current transaction's statistics snapshot or cached values (if any). The next use of statistical information will (when in snapshot mode) cause a new snapshot to be built or (when in cache mode) accessed statistics to be cached.

A transaction can also see its own statistics (not yet flushed out to the shared memory statistics) in the views pg_stat_xact_all_tables, pg_stat_xact_sys_tables, pg_stat_xact_user_tables, and pg_stat_xact_user_functions. These numbers do not act as stated above; instead they update continuously throughout the transaction.

Some of the information in the dynamic statistics views shown in (see monitoring-stats-dynamic-views-table) is security restricted. Ordinary users can only see all the information about their own sessions (sessions belonging to a role that they are a member of). In rows about other sessions, many columns will be null. Note, however, that the existence of a session and its general properties such as its sessions user and database are visible to all users. Superusers and roles with privileges of built-in role pg_read_all_stats (see also (see postgresql_16:4.7.5)) can see all the information about all sessions.

| View Name | Description | | --- | --- | | pg_stat_activity | One row per server process, showing information related to the current activity of that process, such as state and current query. See pg_stat_activity (see postgresql_16:4.13.2.3) for details. | | pg_stat_replication | One row per WAL sender process, showing statistics about replication to that sender's connected standby server. See pg_stat_replication (see postgresql_16:4.13.2.4) for details. | | pg_stat_wal_receiver | Only one row, showing statistics about the WAL receiver from that receiver's connected server. See pg_stat_wal_receiver (see postgresql_16:4.13.2.6) for details. | | pg_stat_recovery_prefetch | Only one row, showing statistics about blocks prefetched during recovery. See pg_stat_recovery_prefetch (see postgresql_16:4.13.2.7) for details. | | pg_stat_subscription | At least one row per subscription, showing information about the subscription workers. See pg_stat_subscription (see postgresql_16:4.13.2.8) for details. | | pg_stat_ssl | One row per connection (regular and replication), showing information about SSL used on this connection. See pg_stat_ssl (see postgresql_16:4.13.2.10) for details. | | pg_stat_gssapi | One row per connection (regular and replication), showing information about GSSAPI authentication and encryption used on this connection. See pg_stat_gssapi (see postgresql_16:4.13.2.11) for details. | | pg_stat_progress_analyze | One row for each backend (including autovacuum worker processes) running ANALYZE, showing current progress. See (see postgresql_16:4.13.4.1). | | pg_stat_progress_create_index | One row for each backend running CREATE INDEX or REINDEX, showing current progress. See (see postgresql_16:4.13.4.4). | | pg_stat_progress_vacuum | One row for each backend (including autovacuum worker processes) running VACUUM, showing current progress. See (see postgresql_16:4.13.4.5). | | pg_stat_progress_cluster | One row for each backend running CLUSTER or VACUUM FULL, showing current progress. See (see postgresql_16:4.13.4.2). | | pg_stat_progress_basebackup | One row for each WAL sender process streaming a base backup, showing current progress. See (see postgresql_16:4.13.4.6). | | pg_stat_progress_copy | One row for each backend running COPY, showing current progress. See (see postgresql_16:4.13.4.3). |

| View Name | Description | | --- | --- | | pg_stat_archiver | One row only, showing statistics about the WAL archiver process's activity. See pg_stat_archiver (see postgresql_16:4.13.2.12) for details. | | pg_stat_bgwriter | One row only, showing statistics about the background writer process's activity. See pg_stat_bgwriter (see postgresql_16:4.13.2.14) for details. | | pg_stat_database | One row per database, showing database-wide statistics. See pg_stat_database (see postgresql_16:4.13.2.16) for details. | | pg_stat_database_conflicts | One row per database, showing database-wide statistics about query cancels due to conflict with recovery on standby servers. See pg_stat_database_conflicts (see postgresql_16:4.13.2.17) for details. | | pg_stat_io | One row for each combination of backend type, context, and target object containing cluster-wide I/O statistics. See pg_stat_io (see postgresql_16:4.13.2.13) for details. | | pg_stat_replication_slots | One row per replication slot, showing statistics about the replication slot's usage. See pg_stat_replication_slots (see postgresql_16:4.13.2.5) for details. | | pg_stat_slru | One row per SLRU, showing statistics of operations. See pg_stat_slru (see postgresql_16:4.13.2.24) for details. | | pg_stat_subscription_stats | One row per subscription, showing statistics about errors. See pg_stat_subscription_stats (see postgresql_16:4.13.2.9) for details. | | pg_stat_wal | One row only, showing statistics about WAL activity. See pg_stat_wal (see postgresql_16:4.13.2.15) for details. | | pg_stat_all_tables | One row for each table in the current database, showing statistics about accesses to that specific table. See pg_stat_all_tables (see postgresql_16:4.13.2.18) for details. | | pg_stat_sys_tables | Same as pg_stat_all_tables, except that only system tables are shown. | | pg_stat_user_tables | Same as pg_stat_all_tables, except that only user tables are shown. | | pg_stat_xact_all_tables | Similar to pg_stat_all_tables, but counts actions taken so far within the current transaction (which are *not* yet included in pg_stat_all_tables and related views). The columns for numbers of live and dead rows and vacuum and analyze actions are not present in this view. | | pg_stat_xact_sys_tables | Same as pg_stat_xact_all_tables, except that only system tables are shown. | | pg_stat_xact_user_tables | Same as pg_stat_xact_all_tables, except that only user tables are shown. | | pg_stat_all_indexes | One row for each index in the current database, showing statistics about accesses to that specific index. See pg_stat_all_indexes (see postgresql_16:4.13.2.19) for details. | | pg_stat_sys_indexes | Same as pg_stat_all_indexes, except that only indexes on system tables are shown. | | pg_stat_user_indexes | Same as pg_stat_all_indexes, except that only indexes on user tables are shown. | | pg_stat_user_functions | One row for each tracked function, showing statistics about executions of that function. See pg_stat_user_functions (see postgresql_16:4.13.2.23) for details. | | pg_stat_xact_user_functions | Similar to pg_stat_user_functions, but counts only calls during the current transaction (which are *not* yet included in pg_stat_user_functions). | | pg_statio_all_tables | One row for each table in the current database, showing statistics about I/O on that specific table. See pg_statio_all_tables (see postgresql_16:4.13.2.20) for details. | | pg_statio_sys_tables | Same as pg_statio_all_tables, except that only system tables are shown. | | pg_statio_user_tables | Same as pg_statio_all_tables, except that only user tables are shown. | | pg_statio_all_indexes | One row for each index in the current database, showing statistics about I/O on that specific index. See pg_statio_all_indexes (see postgresql_16:4.13.2.21) for details. | | pg_statio_sys_indexes | Same as pg_statio_all_indexes, except that only indexes on system tables are shown. | | pg_statio_user_indexes | Same as pg_statio_all_indexes, except that only indexes on user tables are shown. | | pg_statio_all_sequences | One row for each sequence in the current database, showing statistics about I/O on that specific sequence. See pg_statio_all_sequences (see postgresql_16:4.13.2.22) for details. | | pg_statio_sys_sequences | Same as pg_statio_all_sequences, except that only system sequences are shown. (Presently, no system sequences are defined, so this view is always empty.) | | pg_statio_user_sequences | Same as pg_statio_all_sequences, except that only user sequences are shown. |

The per-index statistics are particularly useful to determine which indexes are being used and how effective they are.

The pg_stat_io and pg_statio_ set of views are useful for determining the effectiveness of the buffer cache. They can be used to calculate a cache hit ratio. Note that while PostgreSQL's I/O statistics capture most instances in which the kernel was invoked in order to perform I/O, they do not differentiate between data which had to be fetched from disk and that which already resided in the kernel page cache. Users are advised to use the PostgreSQL statistics views in combination with operating system utilities for a more complete picture of their database's I/O performance.

Back