Skip to content

pg_stat_log

Track cumulative PostgreSQL log message statistics by backend, database, user, level, and SQLSTATE.

pg_stat_log : Track cumulative PostgreSQL log message statistics by backend, database, user, level, and SQLSTATE.

Overview

ID Extension Package Version Category License Language
6040 pg_stat_log pg_stat_log 0.3 STAT PostgreSQL C
Attribute Has Binary Has Library Need Load Has DDL Relocatable Trusted
--sLd-r No Yes Yes Yes yes no
PostgreSQL 18+; shared preload and restart required. Upgrading 0.1/0.2 to 0.3 resets all cumulative statistics; SQL upgrade recreates the view/function, requiring dependent views and custom grants to be restored.

Packages

Type Repo Version PG Major Compatibility Package Pattern Dependencies
EXT PGDG 0.3 18 17 16 15 14 pg_stat_log -
RPM PGDG 0.3 18 17 16 15 14 pg_stat_log_$v -
DEB PGDG 0.2 18 17 16 15 14 postgresql-$v-stat-log -
Linux / PG PG18 PG17 PG16 PG15 PG14
el8.x86_64 PGDG 0.3 N/A N/A N/A N/A
el8.aarch64 PGDG 0.3 N/A N/A N/A N/A
el9.x86_64 PGDG 0.3 N/A N/A N/A N/A
el9.aarch64 PGDG 0.3 N/A N/A N/A N/A
el10.x86_64 PGDG 0.3 N/A N/A N/A N/A
el10.aarch64 PGDG 0.3 N/A N/A N/A N/A
d12.x86_64 PGDG 0.2 N/A N/A N/A N/A
d12.aarch64 PGDG 0.2 N/A N/A N/A N/A
d13.x86_64 PGDG 0.2 N/A N/A N/A N/A
d13.aarch64 PGDG 0.2 N/A N/A N/A N/A
u22.x86_64 PGDG 0.2 N/A N/A N/A N/A
u22.aarch64 PGDG 0.2 N/A N/A N/A N/A
u24.x86_64 PGDG 0.2 N/A N/A N/A N/A
u24.aarch64 PGDG 0.2 N/A N/A N/A N/A
u26.x86_64 PGDG 0.2 N/A N/A N/A N/A
u26.aarch64 PGDG 0.2 N/A N/A N/A N/A
Package Version OS ORG SIZE File URL
pg_stat_log_18 0.3 el8.x86_64 pgdg 29.0 KiB pg_stat_log_18-0.3-1PGDG.rhel8.10.x86_64.rpm
pg_stat_log_18 0.2 el8.x86_64 pgdg 24.0 KiB pg_stat_log_18-0.2-1PGDG.rhel8.10.x86_64.rpm
pg_stat_log_18 0.1 el8.x86_64 pgdg 23.2 KiB pg_stat_log_18-0.1-1PGDG.rhel8.10.x86_64.rpm
pg_stat_log_18 0.3 el8.aarch64 pgdg 28.7 KiB pg_stat_log_18-0.3-1PGDG.rhel8.10.aarch64.rpm
pg_stat_log_18 0.2 el8.aarch64 pgdg 23.9 KiB pg_stat_log_18-0.2-1PGDG.rhel8.10.aarch64.rpm
pg_stat_log_18 0.1 el8.aarch64 pgdg 23.1 KiB pg_stat_log_18-0.1-1PGDG.rhel8.10.aarch64.rpm
pg_stat_log_18 0.3 el9.x86_64 pgdg 29.6 KiB pg_stat_log_18-0.3-1PGDG.rhel9.8.x86_64.rpm
pg_stat_log_18 0.2 el9.x86_64 pgdg 24.3 KiB pg_stat_log_18-0.2-1PGDG.rhel9.8.x86_64.rpm
pg_stat_log_18 0.1 el9.x86_64 pgdg 23.4 KiB pg_stat_log_18-0.1-1PGDG.rhel9.8.x86_64.rpm
pg_stat_log_18 0.1 el9.x86_64 pgdg 23.5 KiB pg_stat_log_18-0.1-1PGDG.rhel9.7.x86_64.rpm
pg_stat_log_18 0.1 el9.x86_64 pgdg 23.6 KiB pg_stat_log_18-0.1-1PGDG.rhel9.6.x86_64.rpm
pg_stat_log_18 0.3 el9.aarch64 pgdg 29.4 KiB pg_stat_log_18-0.3-1PGDG.rhel9.8.aarch64.rpm
pg_stat_log_18 0.2 el9.aarch64 pgdg 24.1 KiB pg_stat_log_18-0.2-1PGDG.rhel9.8.aarch64.rpm
pg_stat_log_18 0.1 el9.aarch64 pgdg 23.4 KiB pg_stat_log_18-0.1-1PGDG.rhel9.8.aarch64.rpm
pg_stat_log_18 0.1 el9.aarch64 pgdg 23.4 KiB pg_stat_log_18-0.1-1PGDG.rhel9.7.aarch64.rpm
pg_stat_log_18 0.1 el9.aarch64 pgdg 23.5 KiB pg_stat_log_18-0.1-1PGDG.rhel9.6.aarch64.rpm
pg_stat_log_18 0.3 el10.x86_64 pgdg 29.6 KiB pg_stat_log_18-0.3-1PGDG.rhel10.2.x86_64.rpm
pg_stat_log_18 0.2 el10.x86_64 pgdg 24.4 KiB pg_stat_log_18-0.2-1PGDG.rhel10.2.x86_64.rpm
pg_stat_log_18 0.1 el10.x86_64 pgdg 23.5 KiB pg_stat_log_18-0.1-1PGDG.rhel10.2.x86_64.rpm
pg_stat_log_18 0.1 el10.x86_64 pgdg 23.5 KiB pg_stat_log_18-0.1-1PGDG.rhel10.1.x86_64.rpm
pg_stat_log_18 0.1 el10.x86_64 pgdg 23.9 KiB pg_stat_log_18-0.1-1PGDG.rhel10.0.x86_64.rpm
pg_stat_log_18 0.3 el10.aarch64 pgdg 29.7 KiB pg_stat_log_18-0.3-1PGDG.rhel10.2.aarch64.rpm
pg_stat_log_18 0.2 el10.aarch64 pgdg 24.4 KiB pg_stat_log_18-0.2-1PGDG.rhel10.2.aarch64.rpm
pg_stat_log_18 0.1 el10.aarch64 pgdg 23.7 KiB pg_stat_log_18-0.1-1PGDG.rhel10.2.aarch64.rpm
pg_stat_log_18 0.1 el10.aarch64 pgdg 23.7 KiB pg_stat_log_18-0.1-1PGDG.rhel10.1.aarch64.rpm
pg_stat_log_18 0.1 el10.aarch64 pgdg 23.7 KiB pg_stat_log_18-0.1-1PGDG.rhel10.0.aarch64.rpm
postgresql-18-stat-log 0.2 d12.x86_64 pgdg 42.7 KiB postgresql-18-stat-log_0.2-1.pgdg12+2_amd64.deb
postgresql-18-stat-log 0.2 d12.x86_64 pgdg 42.7 KiB postgresql-18-stat-log_0.2-1.pgdg12+1_amd64.deb
postgresql-18-stat-log 0.1 d12.x86_64 pgdg 42.3 KiB postgresql-18-stat-log_0.1-2.pgdg12+1_amd64.deb
postgresql-18-stat-log 0.2 d12.aarch64 pgdg 42.7 KiB postgresql-18-stat-log_0.2-1.pgdg12+2_arm64.deb
postgresql-18-stat-log 0.2 d12.aarch64 pgdg 42.7 KiB postgresql-18-stat-log_0.2-1.pgdg12+1_arm64.deb
postgresql-18-stat-log 0.1 d12.aarch64 pgdg 42.3 KiB postgresql-18-stat-log_0.1-2.pgdg12+1_arm64.deb
postgresql-18-stat-log 0.2 d13.x86_64 pgdg 42.8 KiB postgresql-18-stat-log_0.2-1.pgdg13+2_amd64.deb
postgresql-18-stat-log 0.2 d13.x86_64 pgdg 42.8 KiB postgresql-18-stat-log_0.2-1.pgdg13+1_amd64.deb
postgresql-18-stat-log 0.1 d13.x86_64 pgdg 42.3 KiB postgresql-18-stat-log_0.1-2.pgdg13+1_amd64.deb
postgresql-18-stat-log 0.2 d13.aarch64 pgdg 42.8 KiB postgresql-18-stat-log_0.2-1.pgdg13+2_arm64.deb
postgresql-18-stat-log 0.2 d13.aarch64 pgdg 42.9 KiB postgresql-18-stat-log_0.2-1.pgdg13+1_arm64.deb
postgresql-18-stat-log 0.1 d13.aarch64 pgdg 42.3 KiB postgresql-18-stat-log_0.1-2.pgdg13+1_arm64.deb
postgresql-18-stat-log 0.2 u22.x86_64 pgdg 43.1 KiB postgresql-18-stat-log_0.2-1.pgdg22.04+2_amd64.deb
postgresql-18-stat-log 0.2 u22.x86_64 pgdg 43.1 KiB postgresql-18-stat-log_0.2-1.pgdg22.04+1_amd64.deb
postgresql-18-stat-log 0.1 u22.x86_64 pgdg 42.7 KiB postgresql-18-stat-log_0.1-2.pgdg22.04+1_amd64.deb
postgresql-18-stat-log 0.2 u22.aarch64 pgdg 42.8 KiB postgresql-18-stat-log_0.2-1.pgdg22.04+2_arm64.deb
postgresql-18-stat-log 0.2 u22.aarch64 pgdg 42.8 KiB postgresql-18-stat-log_0.2-1.pgdg22.04+1_arm64.deb
postgresql-18-stat-log 0.1 u22.aarch64 pgdg 42.4 KiB postgresql-18-stat-log_0.1-2.pgdg22.04+1_arm64.deb
postgresql-18-stat-log 0.2 u24.x86_64 pgdg 42.9 KiB postgresql-18-stat-log_0.2-1.pgdg24.04+2_amd64.deb
postgresql-18-stat-log 0.2 u24.x86_64 pgdg 42.9 KiB postgresql-18-stat-log_0.2-1.pgdg24.04+1_amd64.deb
postgresql-18-stat-log 0.1 u24.x86_64 pgdg 42.5 KiB postgresql-18-stat-log_0.1-2.pgdg24.04+1_amd64.deb
postgresql-18-stat-log 0.2 u24.aarch64 pgdg 42.8 KiB postgresql-18-stat-log_0.2-1.pgdg24.04+2_arm64.deb
postgresql-18-stat-log 0.2 u24.aarch64 pgdg 42.8 KiB postgresql-18-stat-log_0.2-1.pgdg24.04+1_arm64.deb
postgresql-18-stat-log 0.1 u24.aarch64 pgdg 42.4 KiB postgresql-18-stat-log_0.1-2.pgdg24.04+1_arm64.deb
postgresql-18-stat-log 0.2 u26.x86_64 pgdg 42.8 KiB postgresql-18-stat-log_0.2-1.pgdg26.04+2_amd64.deb
postgresql-18-stat-log 0.2 u26.x86_64 pgdg 42.8 KiB postgresql-18-stat-log_0.2-1.pgdg26.04+1_amd64.deb
postgresql-18-stat-log 0.1 u26.x86_64 pgdg 42.3 KiB postgresql-18-stat-log_0.1-2.pgdg26.04+1_amd64.deb
postgresql-18-stat-log 0.2 u26.aarch64 pgdg 42.8 KiB postgresql-18-stat-log_0.2-1.pgdg26.04+2_arm64.deb
postgresql-18-stat-log 0.2 u26.aarch64 pgdg 42.8 KiB postgresql-18-stat-log_0.2-1.pgdg26.04+1_arm64.deb
postgresql-18-stat-log 0.1 u26.aarch64 pgdg 42.3 KiB postgresql-18-stat-log_0.1-2.pgdg26.04+1_arm64.deb

Source

Install

Make sure PGDG repo available:

pig repo add pgdg -u    # add pgdg repo and update cache

Install this extension with pig:

pig install pg_stat_log;		# install via package name, for the active PG version

pig install pg_stat_log -v 18;   # install for PG 18

Config this extension to shared_preload_libraries:

shared_preload_libraries = '$libdir/pg_stat_log';

Create this extension with:

CREATE EXTENSION pg_stat_log;

Usage

Sources:

pg_stat_log 0.3 collects PostgreSQL 18+ log-message counts by backend, database, role, severity and SQLSTATE, with optional grouping by query identifier. Add the library to the existing preload list and restart before creating the SQL extension. The default SQL grants allow pg_read_all_stats to inspect the counters.

Core Workflow

shared_preload_libraries = 'pg_stat_log'
CREATE EXTENSION pg_stat_log;
SELECT database_name, user_name, query_id, elevel, sqlerrcode_name, count
FROM pg_stat_log
ORDER BY count DESC;
SELECT * FROM pg_stat_log_info();

Operational Boundaries

The pg_stat_log view resolves names from pg_stat_log_data(). pg_stat_log_info() reports max_entries, num_entries, n_dropped and stats_reset; pg_stat_log_reset() clears counters and frees capacity, and is restricted by default. pg_stat_log.enabled and pg_stat_log.min_error_level control collection, with defaults true and warning. Only messages reaching the server log are counted: log_min_messages can filter messages before the hook runs, and caught exceptions are absent. Parallel-worker messages can count the same logical failure more than once.

pg_stat_log.compute_query_id defaults to off and requires restart to change; PostgreSQL’s compute_query_id must not be off. A missing query identifier appears as NULL. pg_stat_log.max_entries defaults to 1024, requires restart, and drops new combinations when full; inspect n_dropped. Changing this capacity resets all PostgreSQL cumulative statistics on the next start. Counters survive clean restarts, but drop telemetry does not, and crash recovery discards cumulative statistics.

Version 0.3 maps connection authentication, authorization and disconnection to PSL01, PSL02 and PSL03. Collection needs the corresponding server logging plus pg_stat_log.min_error_level = 'log' or lower; the role attribution is the login role.

Upstream releases 0.1 and 0.2 both install SQL version 0.1. Moving to 0.3 requires matching library files and a restart; the first start resets all PostgreSQL cumulative statistics. Then run ALTER EXTENSION pg_stat_log UPDATE TO '0.3' in each installed database. The update recreates the reporting view and function: drop and recreate dependent views and reapply custom grants; the default grants to pg_read_all_stats are restored.