RefLib

Reference library for AI

Back

postgresql_16:4.13.2.25 Statistics Functions

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

Other ways of looking at the statistics can be set up by writing queries that use the same underlying statistics access functions used by the standard views shown above. For details such as the functions' names, consult the definitions of the standard views. (For example, in psql you could issue \d+ pg_stat_activity.) The access functions for per-database statistics take a database OID as an argument to identify which database to report on. The per-table and per-index functions take a table or index OID. The functions for per-function statistics take a function OID. Note that only tables, indexes, and functions in the current database can be seen with these functions.

Additional functions related to the cumulative statistics system are listed in (see monitoring-stats-funcs-table).

| Function Description | | --- | | pg_backend_pid () integer Returns the process ID of the server process attached to the current session. | | pg_stat_get_activity ( integer ) setof record Returns a record of information about the backend with the specified process ID, or one record for each active backend in the system if NULL is specified. The fields returned are a subset of those in the pg_stat_activity view. | | pg_stat_get_snapshot_timestamp () timestamp with time zone Returns the timestamp of the current statistics snapshot, or NULL if no statistics snapshot has been taken. A snapshot is taken the first time cumulative statistics are accessed in a transaction if stats_fetch_consistency is set to snapshot | | pg_stat_get_xact_blocks_fetched ( oid ) bigint Returns the number of block read requests for table or index, in the current transaction. This number minus pg_stat_get_xact_blocks_hit gives the number of kernel read() calls; the number of actual physical reads is usually lower due to kernel-level buffering. | | pg_stat_get_xact_blocks_hit ( oid ) bigint Returns the number of block read requests for table or index, in the current transaction, found in cache (not triggering kernel read() calls). | | pg_stat_clear_snapshot () void Discards the current statistics snapshot or cached information. | | pg_stat_reset () void Resets all statistics counters for the current database to zero. This function is restricted to superusers by default, but other users can be granted EXECUTE to run the function. | | pg_stat_reset_shared ( text ) void Resets some cluster-wide statistics counters to zero, depending on the argument. The argument can be bgwriter to reset all the counters shown in the pg_stat_bgwriter view, archiver to reset all the counters shown in the pg_stat_archiver view, io to reset all the counters shown in the pg_stat_io view, wal to reset all the counters shown in the pg_stat_wal view or recovery_prefetch to reset all the counters shown in the pg_stat_recovery_prefetch view. This function is restricted to superusers by default, but other users can be granted EXECUTE to run the function. | | pg_stat_reset_single_table_counters ( oid ) void Resets statistics for a single table or index in the current database or shared across all databases in the cluster to zero. This function is restricted to superusers by default, but other users can be granted EXECUTE to run the function. | | pg_stat_reset_single_function_counters ( oid ) void Resets statistics for a single function in the current database to zero. This function is restricted to superusers by default, but other users can be granted EXECUTE to run the function. | | pg_stat_reset_slru ( text ) void Resets statistics to zero for a single SLRU cache, or for all SLRUs in the cluster. If the argument is NULL, all counters shown in the pg_stat_slru view for all SLRU caches are reset. The argument can be one of CommitTs, MultiXactMember, MultiXactOffset, Notify, Serial, Subtrans, or Xact to reset the counters for only that entry. If the argument is other (or indeed, any unrecognized name), then the counters for all other SLRU caches, such as extension-defined caches, are reset. This function is restricted to superusers by default, but other users can be granted EXECUTE to run the function. | | pg_stat_reset_replication_slot ( text ) void Resets statistics of the replication slot defined by the argument. If the argument is NULL, resets statistics for all the replication slots. This function is restricted to superusers by default, but other users can be granted EXECUTE to run the function. | | pg_stat_reset_subscription_stats ( oid ) void Resets statistics for a single subscription shown in the pg_stat_subscription_stats view to zero. If the argument is NULL, reset statistics for all subscriptions. This function is restricted to superusers by default, but other users can be granted EXECUTE to run the function. |

Warning

Using pg_stat_reset() also resets counters that autovacuum uses to determine when to trigger a vacuum or an analyze. Resetting these counters can cause autovacuum to not perform necessary work, which can cause problems such as table bloat or out-dated table statistics. A database-wide ANALYZE is recommended after the statistics have been reset.

pg_stat_get_activity, the underlying function of the pg_stat_activity view, returns a set of records containing all the available information about each backend process. Sometimes it may be more convenient to obtain just a subset of this information. In such cases, another set of per-backend statistics access functions can be used; these are shown in (see monitoring-stats-backend-funcs-table). These access functions use the session's backend ID number, which is a small positive integer that is distinct from the backend ID of any concurrent session, although a session's ID can be recycled as soon as it exits. The backend ID is used, among other things, to identify the session's temporary schema if it has one. The function pg_stat_get_backend_idset provides a convenient way to list all the active backends' ID numbers for invoking these functions. For example, to show the PIDs and current queries of all backends: SELECT pg_stat_get_backend_pid(backendid) AS pid, pg_stat_get_backend_activity(backendid) AS query FROM pg_stat_get_backend_idset() AS backendid;

| Function Description | | --- | | pg_stat_get_backend_activity ( integer ) text Returns the text of this backend's most recent query. | | pg_stat_get_backend_activity_start ( integer ) timestamp with time zone Returns the time when the backend's most recent query was started. | | pg_stat_get_backend_client_addr ( integer ) inet Returns the IP address of the client connected to this backend. | | pg_stat_get_backend_client_port ( integer ) integer Returns the TCP port number that the client is using for communication. | | pg_stat_get_backend_dbid ( integer ) oid Returns the OID of the database this backend is connected to. | | pg_stat_get_backend_idset () setof integer Returns the set of currently active backend ID numbers. | | pg_stat_get_backend_pid ( integer ) integer Returns the process ID of this backend. | | pg_stat_get_backend_start ( integer ) timestamp with time zone Returns the time when this process was started. | | pg_stat_get_backend_subxact ( integer ) record Returns a record of information about the subtransactions of the backend with the specified ID. The fields returned are subxact_count, which is the number of subtransactions in the backend's subtransaction cache, and subxact_overflow, which indicates whether the backend's subtransaction cache is overflowed or not. | | pg_stat_get_backend_userid ( integer ) oid Returns the OID of the user logged into this backend. | | pg_stat_get_backend_wait_event ( integer ) text Returns the wait event name if this backend is currently waiting, otherwise NULL. See (see wait-event-activity-table) through (see wait-event-timeout-table). | | pg_stat_get_backend_wait_event_type ( integer ) text Returns the wait event type name if this backend is currently waiting, otherwise NULL. See (see wait-event-table) for details. | | pg_stat_get_backend_xact_start ( integer ) timestamp with time zone Returns the time when the backend's current transaction was started. |

Back