Skip to content
pg_stat_log

pg_stat_log

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

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
6040
pg_stat_log
pg_stat_log
0.1
STAT
PostgreSQL
C
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--sLd-r
No
Yes
Yes
Yes
yes
no
Relationships
See Also
pg_stat_statements
pg_stat_monitor
pg_stat_plans

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXT
PGDG
0.1
18
17
16
15
14
pg_stat_log-
RPM
PGDG
0.1
18
17
16
15
14
pg_stat_log_$v-
DEB
PGDG
0.1
18
17
16
15
14
postgresql-$v-stat-log-
Linux / PGPG18PG17PG16PG15PG14
el8.x86_64
PGDG 0.1
MISS
MISS
MISS
MISS
el8.aarch64
PGDG 0.1
MISS
MISS
MISS
MISS
el9.x86_64
PGDG 0.1
MISS
MISS
MISS
MISS
el9.aarch64
PGDG 0.1
MISS
MISS
MISS
MISS
el10.x86_64
PGDG 0.1
MISS
MISS
MISS
MISS
el10.aarch64
PGDG 0.1
MISS
MISS
MISS
MISS
d12.x86_64
PGDG 0.1
MISS
MISS
MISS
MISS
d12.aarch64
PGDG 0.1
MISS
MISS
MISS
MISS
d13.x86_64
PGDG 0.1
MISS
MISS
MISS
MISS
d13.aarch64
PGDG 0.1
MISS
MISS
MISS
MISS
u22.x86_64
PGDG 0.1
MISS
MISS
MISS
MISS
u22.aarch64
PGDG 0.1
MISS
MISS
MISS
MISS
u24.x86_64
PGDG 0.1
MISS
MISS
MISS
MISS
u24.aarch64
PGDG 0.1
MISS
MISS
MISS
MISS
u26.x86_64
PGDG 0.1
MISS
MISS
MISS
MISS
u26.aarch64
PGDG 0.1
MISS
MISS
MISS
MISS
PackageVersionOSORGSIZEFile URL
pg_stat_log_180.1el8.x86_64pgdg23.2 KiBpg_stat_log_18-0.1-1PGDG.rhel8.10.x86_64.rpm
pg_stat_log_180.1el8.aarch64pgdg23.1 KiBpg_stat_log_18-0.1-1PGDG.rhel8.10.aarch64.rpm
pg_stat_log_180.1el9.x86_64pgdg23.4 KiBpg_stat_log_18-0.1-1PGDG.rhel9.8.x86_64.rpm
pg_stat_log_180.1el9.x86_64pgdg23.5 KiBpg_stat_log_18-0.1-1PGDG.rhel9.7.x86_64.rpm
pg_stat_log_180.1el9.x86_64pgdg23.6 KiBpg_stat_log_18-0.1-1PGDG.rhel9.6.x86_64.rpm
pg_stat_log_180.1el9.aarch64pgdg23.4 KiBpg_stat_log_18-0.1-1PGDG.rhel9.8.aarch64.rpm
pg_stat_log_180.1el9.aarch64pgdg23.4 KiBpg_stat_log_18-0.1-1PGDG.rhel9.7.aarch64.rpm
pg_stat_log_180.1el9.aarch64pgdg23.5 KiBpg_stat_log_18-0.1-1PGDG.rhel9.6.aarch64.rpm
pg_stat_log_180.1el10.x86_64pgdg23.5 KiBpg_stat_log_18-0.1-1PGDG.rhel10.2.x86_64.rpm
pg_stat_log_180.1el10.x86_64pgdg23.5 KiBpg_stat_log_18-0.1-1PGDG.rhel10.1.x86_64.rpm
pg_stat_log_180.1el10.x86_64pgdg23.9 KiBpg_stat_log_18-0.1-1PGDG.rhel10.0.x86_64.rpm
pg_stat_log_180.1el10.aarch64pgdg23.7 KiBpg_stat_log_18-0.1-1PGDG.rhel10.2.aarch64.rpm
pg_stat_log_180.1el10.aarch64pgdg23.7 KiBpg_stat_log_18-0.1-1PGDG.rhel10.1.aarch64.rpm
pg_stat_log_180.1el10.aarch64pgdg23.7 KiBpg_stat_log_18-0.1-1PGDG.rhel10.0.aarch64.rpm
postgresql-18-stat-log0.1d12.x86_64pgdg42.3 KiBpostgresql-18-stat-log_0.1-2.pgdg12+1_amd64.deb
postgresql-18-stat-log0.1d12.x86_64pgdg42.2 KiBpostgresql-18-stat-log_0.1-1.pgdg12+1_amd64.deb
postgresql-18-stat-log0.1d12.aarch64pgdg42.3 KiBpostgresql-18-stat-log_0.1-2.pgdg12+1_arm64.deb
postgresql-18-stat-log0.1d12.aarch64pgdg42.2 KiBpostgresql-18-stat-log_0.1-1.pgdg12+1_arm64.deb
postgresql-18-stat-log0.1d13.x86_64pgdg42.3 KiBpostgresql-18-stat-log_0.1-2.pgdg13+1_amd64.deb
postgresql-18-stat-log0.1d13.x86_64pgdg42.1 KiBpostgresql-18-stat-log_0.1-1.pgdg13+1_amd64.deb
postgresql-18-stat-log0.1d13.aarch64pgdg42.3 KiBpostgresql-18-stat-log_0.1-2.pgdg13+1_arm64.deb
postgresql-18-stat-log0.1d13.aarch64pgdg42.3 KiBpostgresql-18-stat-log_0.1-1.pgdg13+1_arm64.deb
postgresql-18-stat-log0.1u22.x86_64pgdg42.7 KiBpostgresql-18-stat-log_0.1-2.pgdg22.04+1_amd64.deb
postgresql-18-stat-log0.1u22.x86_64pgdg42.6 KiBpostgresql-18-stat-log_0.1-1.pgdg22.04+1_amd64.deb
postgresql-18-stat-log0.1u22.aarch64pgdg42.4 KiBpostgresql-18-stat-log_0.1-2.pgdg22.04+1_arm64.deb
postgresql-18-stat-log0.1u22.aarch64pgdg42.4 KiBpostgresql-18-stat-log_0.1-1.pgdg22.04+1_arm64.deb
postgresql-18-stat-log0.1u24.x86_64pgdg42.5 KiBpostgresql-18-stat-log_0.1-2.pgdg24.04+1_amd64.deb
postgresql-18-stat-log0.1u24.x86_64pgdg42.4 KiBpostgresql-18-stat-log_0.1-1.pgdg24.04+1_amd64.deb
postgresql-18-stat-log0.1u24.aarch64pgdg42.4 KiBpostgresql-18-stat-log_0.1-2.pgdg24.04+1_arm64.deb
postgresql-18-stat-log0.1u24.aarch64pgdg42.2 KiBpostgresql-18-stat-log_0.1-1.pgdg24.04+1_arm64.deb
postgresql-18-stat-log0.1u26.x86_64pgdg42.3 KiBpostgresql-18-stat-log_0.1-2.pgdg26.04+1_amd64.deb
postgresql-18-stat-log0.1u26.x86_64pgdg42.2 KiBpostgresql-18-stat-log_0.1-1.pgdg26.04+1_amd64.deb
postgresql-18-stat-log0.1u26.aarch64pgdg42.3 KiBpostgresql-18-stat-log_0.1-2.pgdg26.04+1_arm64.deb
postgresql-18-stat-log0.1u26.aarch64pgdg42.2 KiBpostgresql-18-stat-log_0.1-1.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: README, SQL objects, control file

pg_stat_log collects cumulative statistics about PostgreSQL log messages. It hooks into emit_log_hook and counts messages by backend type, database, user, severity, SQLSTATE, and SQLSTATE condition name.

Enable

pg_stat_log requires PostgreSQL 18 or newer and must be preloaded:

shared_preload_libraries = 'pg_stat_log'

Restart PostgreSQL, then create the extension:

CREATE EXTENSION pg_stat_log;

View Statistics

SELECT *
FROM pg_stat_log
ORDER BY count DESC;

The view exposes backend_type, database_oid, database_name, user_oid, user_name, elevel, sqlerrcode, sqlerrcode_name, and count.

Common Queries

SELECT elevel, sqlerrcode, sqlerrcode_name, sum(count) AS total
FROM pg_stat_log
GROUP BY elevel, sqlerrcode, sqlerrcode_name
ORDER BY total DESC
LIMIT 10;

SELECT backend_type, elevel, sqlerrcode_name, count
FROM pg_stat_log
WHERE backend_type <> 'client backend'
ORDER BY count DESC;

Reset And Capacity

SELECT pg_stat_log_reset();
SELECT * FROM pg_stat_log_info();

pg_stat_log_info() reports max_entries, num_entries, n_dropped, and stats_reset. Increase pg_stat_log.max_entries if n_dropped grows.

Configuration

Settings include pg_stat_log.enabled, pg_stat_log.min_error_level, and pg_stat_log.max_entries.

emit_log_hook only sees messages that reach the server log. log_min_messages therefore acts as a floor for what can be counted.

Last updated on