Sub topic 10
The background writer and who writes dirty pages
This chapter assumes that you can read basic EXPLAIN output, understand RAM and disk I/O, and edit PostgreSQL configuration. It also assumes that you know the difference between reloading a setting and restarting the server.
Dirty pages and shared buffers
A dirty page is a data page in PostgreSQL's shared buffer cache that has changed in memory but has not yet been written to the data files on disk. A transaction usually changes the shared copy first. PostgreSQL records the change in the write-ahead log (WAL) before the changed data page must become durable.
Several processes can write dirty pages:
| Writer | Main responsibility |
|---|---|
| Background writer | Proactively writes reusable dirty buffers |
| Checkpointer | Writes required dirty buffers during a checkpoint |
| Backend process | Writes buffers when it cannot find a clean buffer quickly enough |
The background writer tries to prevent ordinary database backends from pausing to write pages. It runs periodically, finds dirty buffers that are likely to be needed soon, and writes them. It does not write every dirty page.
Background writer versus checkpointer
The checkpointer creates a consistent recovery point. At a checkpoint, it writes dirty buffers so that recovery after a crash has less work to replay. Checkpoints can therefore produce a large amount of I/O.
The background writer handles ongoing buffer reuse between checkpoints. Its work is preventive. If it cannot keep up, backend processes take over some writes.
The most useful background-writer settings are:
bgwriter_delay: time between background-writer rounds.bgwriter_lru_maxpages: maximum buffers written in one round.-1removes this limit.bgwriter_lru_multiplier: multiplier applied to recent buffer demand when estimating how many buffers to write.bgwriter_flush_after: approximate amount of data written before requesting that the operating system flush it.
Inspect the current values and counters:
SELECT name, setting, unit, context
FROM pg_settings
WHERE name IN ('bgwriter_delay', 'bgwriter_lru_maxpages',
'bgwriter_lru_multiplier', 'bgwriter_flush_after');
SELECT checkpoints_timed,
checkpoints_req,
buffers_checkpoint,
buffers_clean,
maxwritten_clean,
buffers_backend,
buffers_backend_fsync,
buffers_alloc
FROM pg_stat_bgwriter;buffers_clean counts buffers written by the background writer. buffers_backend counts buffers written directly by backend processes. A high buffers_backend value can indicate that the background writer is not preparing enough clean buffers, although workload patterns and checkpoint pressure also matter. maxwritten_clean counts rounds in which the background writer stopped because it reached bgwriter_lru_maxpages.
On newer PostgreSQL releases, some checkpoint and I/O statistics may also appear in separate statistics views. Check the documentation for your server version before comparing view columns.
Tuning without guesswork
Do not increase the background writer limits simply because buffers_backend is nonzero. Some backend writes are normal. First observe the counters over a representative workload, then change one setting and measure again.
If checkpoints write heavily, inspect max_wal_size, checkpoint_timeout, and checkpoint_completion_target as well. The checkpointer spreads checkpoint writes across the interval; the background writer does not replace that function. A backend forced to perform synchronous file-system work can show up as query latency even when average throughput looks acceptable.
Exercise
Which counter suggests that backend processes are writing buffers because the background writer did not prepare enough clean buffers?
Show answer
buffers_backend shows direct backend writes. Compare it with buffers_clean and maxwritten_clean over the same workload before changing settings.