Background writer
The background writer
is a separate server process responsible for writing "dirty" (new or modified) buffers to the on-disk datafiles, to reduce load on regular backends.
The background writer
was added in PostgreSQL 8.0.
Until PostgreSQL 9.2 it was also responsible for handling checkpoints.
Configuration
Currently following configuration items control the background writer behaviour:
bgwriter_delay
(integer
)bgwriter_lru_maxpages
(integer
)bgwriter_lru_multiplier
(floating point)
bgwriter_flush_after
(integer
)
Setting bgwriter_lru_maxpages
to zero effectively disables the background writer.
For more details, see the background writer configuration documentation.
Statistics
The number of buffers written by the background writer itself is recorded in the "buffers_clean" column of the pg_stat_bgwriter view.
Source code
The main background writer process code: src/backend/postmaster/bgwriter.c
The actual "heavy lifting" is done in src/backend/storage/buffer/bufmgr.c, particularly function BgBufferSync()
.
Change history
Work-in-progress
- PostgreSQL 15
- will also run during crash recovery (commit 7ff23c6d)
- PostgreSQL 8.0
- added (initial commit cfeca621)
References
- PostgreSQL documentation: Background Writer
Useful links
- PostgreSQL Background Writer - July 2019 article by Postgres Tutorial