pg_stat_log v0.1
First official release of pg_stat_log, a PostgreSQL extension that collects cumulative statistics about log messages using the Custom Cumulative Stats API introduced in PostgreSQL 18.
What it does
Hooks into emit_log_hook to count log messages grouped by (backend_type, database, user, error level, SQLSTATE code). Exposes the data through the pg_stat_log view and the pg_stat_log_data() function. Statistics persist across clean restarts and are discarded after crash recovery, following standard PostgreSQL cumulative stats semantics.
Custom stats kind ID 28, registered on the PostgreSQL wiki.
Requirements
- PostgreSQL 18 or later
- Must be loaded via
shared_preload_libraries
Features
pg_stat_logview with LEFT JOINs againstpg_databaseandpg_rolesfor human-readable namespg_stat_log_info()function exposing capacity metadata:max_entries,num_entries,n_dropped,stats_resetpg_stat_log_reset()to clear all counters and reclaim slots (superuser-only)
Configuration
| Parameter | Type | Default | Context | Description |
|---|---|---|---|---|
pg_stat_log.enabled
| bool | true
| SUSET | Enable/disable collection at runtime |
pg_stat_log.min_error_level
| enum | warning
| SUSET | Minimum severity to track |
pg_stat_log.max_entries
| int | 1024
| POSTMASTER | Max distinct combinations (requires restart) |
Privileges
| Function/View | Access |
|---|---|
pg_stat_log view
| pg_read_all_stats
|
pg_stat_log_data()
| pg_read_all_stats
|
pg_stat_log_info()
| pg_read_all_stats
|
pg_stat_log_reset()
| Superuser only |
Compatibility
- PostgreSQL 18 (primary target)
- PostgreSQL 19 (with
#if PG_VERSION_NUMcompat guards forserver_message_level_optionsandLWLockNewTrancheIdAPI changes)
Acknowledgements
Thanks to Nikolay Samokhvalov for thorough code reviews, adversarial testing, and feature contributions including pg_stat_log_info(), n_dropped counter, and slot reclamation on reset.