stat_bgwriter collector exposes statistics from the background writer and checkpointer processes.
Status
Default: EnabledVersion-Specific Behavior
Metrics
Checkpoint Metrics (PostgreSQL less than 17)
pg_stat_bgwriter_checkpoints_timed_total
Type: CounterDescription: Number of scheduled checkpoints that have been performed
pg_stat_bgwriter_checkpoints_req_total
Type: CounterDescription: Number of requested checkpoints that have been performed
pg_stat_bgwriter_checkpoint_write_time_total
Type: CounterDescription: Total amount of time spent writing checkpoint files to disk, in milliseconds
pg_stat_bgwriter_checkpoint_sync_time_total
Type: CounterDescription: Total amount of time spent synchronizing checkpoint files to disk, in milliseconds
pg_stat_bgwriter_buffers_checkpoint_total
Type: CounterDescription: Number of buffers written during checkpoints
Background Writer Metrics (All Versions)
pg_stat_bgwriter_buffers_clean_total
Type: CounterDescription: Number of buffers written by the background writer
pg_stat_bgwriter_maxwritten_clean_total
Type: CounterDescription: Number of times the background writer stopped a cleaning scan because it had written too many buffers
pg_stat_bgwriter_buffers_alloc_total
Type: CounterDescription: Number of buffers allocated
Backend Metrics (PostgreSQL less than 17)
pg_stat_bgwriter_buffers_backend_total
Type: CounterDescription: Number of buffers written directly by a backend
pg_stat_bgwriter_buffers_backend_fsync_total
Type: CounterDescription: Number of times a backend had to execute its own fsync call
Statistics Reset
pg_stat_bgwriter_stats_reset_total
Type: CounterDescription: Time at which these statistics were last reset (Unix timestamp)
SQL Queries
PostgreSQL less than 17:PostgreSQL Versions
Supported: All versions Version changes:- PostgreSQL 17+: Checkpoint metrics moved to
pg_stat_checkpointer
Required Permissions
The monitoring user needs:- Access to
pg_stat_bgwriterview (granted toPUBLICby default)
Example Output
PostgreSQL less than 17:Use Cases
Monitor Checkpoint Frequency
Backend Write Activity
Background Writer Effectiveness
Tuning Recommendations
High checkpoints_req
If requested checkpoints are high:
- Increase
max_wal_size - Increase
checkpoint_timeout
High buffers_backend
If backends are writing directly:
- Increase
bgwriter_lru_maxpages - Decrease
bgwriter_delay - Increase
shared_buffers
High maxwritten_clean
If bgwriter is hitting limits:
- Increase
bgwriter_lru_maxpages
Migration to PostgreSQL 17
If upgrading to PostgreSQL 17, use thestat_checkpointer collector for checkpoint metrics.