writing dirty pages back
the background writer, the checkpointer, and the OS page cache sitting under all of it.
- address
- everything.storage.writing_pages_back
- part
- part 12, pages and the buffer pool
- chapter
- chapter 11 of 12
- reading time
- 10 min
a write in postgres changes the page in shared buffers, and appends to the WAL. that's all a COMMIT waits for. the table's files catch up later, written by one of three: the checkpointer, which writes every dirty page on a schedule; the background writer, which cleans pages the clock hand is about to reach; and, if neither got there first, the backend that needs the frame.
and every one of those writes lands in the operating system's page cache first. only an fsync makes it durable.
the picture
you know how a COMMIT comes back in a millisecond, but the table's file on disk hasn't changed at all? the change is real (it's in the WAL, flushed), it's just not in the table yet.
think of a busy kitchen. the orders go on the ticket rail the moment they're taken (the WAL). plates pile up dirty on the counter as the cooks work (dirty pages). a dishwasher keeps clearing the ones about to be needed (the background writer). every so often the whole counter gets cleared at once and the manager writes "everything clean as of 9:40" in the log (a checkpoint). and if a cook needs a plate and there isn't a clean one, they wash one themselves, mid-order (a backend write).
here's the counter, captured on a real server: a table read, then updated, then checkpointed:
read every row. four heap pages and the index's pages are in shared buffers, all clean: they match the file.
the UPDATE dirtied 8 heap pages (the 4 it read, plus 4 new ones for the new versions, since the old pages were full) and most of the index. nothing went to the table's file. the CHECKPOINT then wrote out every dirty buffer in the pool, these and the catalog pages the setup had dirtied, and counted them:
| buffers_writtenint8 | writesint8 | fsyncsint8 |
|---|---|---|
| 147 | 147 | 51 |
147 dirty buffers, 147 pages written: every one. and 51 fsyncs, one per file it wrote to. those fsyncs are the expensive part, and they're why the checkpoint is allowed to take its time.
the checkpointer
the checkpointer's job is to make sure that, every so often, everything the WAL describes up to some point is in the data files. after that, crash recovery never has to replay WAL older than that point, and that old WAL can be recycled. it starts a checkpoint when checkpoint_timeout passes or when max_wal_size worth of WAL has been written since the last one, whichever comes first, and it spreads the writes over most of the gap until the next one, so the disks see a trickle instead of a flood:
SELECT name, setting, unit FROM pg_settings
WHERE name IN ('checkpoint_timeout', 'max_wal_size', 'checkpoint_completion_target',
'bgwriter_delay', 'bgwriter_lru_maxpages', 'bgwriter_lru_multiplier')
ORDER BY name;| nametext | settingtext | unittext |
|---|---|---|
| bgwriter_delay | 200 | ms |
| bgwriter_lru_maxpages | 100 | null |
| bgwriter_lru_multiplier | 2 | null |
| checkpoint_completion_target | 0.9 | null |
| checkpoint_timeout | 300 | s |
| max_wal_size | 1024 | MB |
checked on a real postgres 18.6 server. the browser engine can't run this one.
every 5 minutes or every 1 GB of WAL, with the writes spread over 90% of the interval. what checkpoints mean for recovery is in checkpoints, and why the first change to a page after one logs the whole page is in full-page writes.
the background writer
between checkpoints, the clock hand keeps going round looking for victims, and a victim that's dirty has to be written before its frame can be reused. if the backend doing the sweep has to do that write, some unrelated SELECT just waited on a disk write. the background writer tries to get there first. every bgwriter_delay (200 ms) it looks at how many buffers backends have been grabbing lately, multiplies by bgwriter_lru_multiplier (2), and walks ahead of the clock hand writing out up to bgwriter_lru_maxpages (100) dirty buffers that are unpinned and at usage 0, the ones the hand will take next. it never moves the hand itself. it just makes sure that when the hand arrives, the frame is clean.
the captures in this book turn it off (bgwriter_lru_maxpages = 0), because when it runs depends on the clock and a capture has to come out the same twice. so here is what happens without it: a 2 MB pool, a table of 345 pages (bigger than the whole pool), and one UPDATE of every row:
| backend_typetext | pages_writtennumeric |
|---|---|
| client backend | 589 |
589 pages written, all by the client backend, in the middle of its own UPDATE. every frame it needed for the next page was holding a page it had dirtied a moment ago. on a real server with the background writer on, a good part of that would have been written by it instead, and in pg_stat_io the client backend writes would be a small number next to the background writer and checkpointer ones. if they aren't, the pool is too small for the write load, or the background writer is set too timid.
none of them in the browser
the browser engine is one process. there's no checkpointer, no background writer, no WAL writer: when you run CHECKPOINT there, your own session does the work. a real server is a family of processes:
SELECT backend_type, count(*) FROM pg_stat_activity GROUP BY backend_type ORDER BY backend_type;| backend_typetext | countint8 |
|---|---|
| standalone backend | 1 |
SELECT backend_type, count(*) FROM pg_stat_activity GROUP BY backend_type ORDER BY backend_type;| backend_typetext | countint8 |
|---|---|
| background writer | 1 |
| checkpointer | 1 |
| client backend | 2 |
| io worker | 3 |
| logical replication launcher | 1 |
| walwriter | 1 |
checked on a real postgres 18.6 server. the browser engine can't run this one.
one row in the browser: a standalone backend, which is postgres running as a single process. on a native postgres 18.6 there's the checkpointer, the background writer, the WAL writer, three io workers (those are asynchronous I/O), the logical replication launcher, and two client backends: this session and the checking tool's own connection. (that throwaway server runs with autovacuum off, or you'd see its launcher too.)
BEGIN;
UPDATE accounts SET balance = balance + 1 WHERE id = 1;
COMMIT; -- returnsthe OS page cache under all of it
postgres writes a page with an ordinary write(), and an ordinary write doesn't go to the disk. it goes into the kernel's page cache, and the kernel writes it out when it feels like it. that's why the checkpointer calls fsync() on every file it wrote to before it calls a checkpoint done: until then, a power cut could lose the writes. (a crash of postgres alone doesn't: the kernel still has them.)
reads go through the same cache. a page that isn't in shared buffers is often still in the OS page cache, and then a "read" costs a memory copy, about 1 µs on the machine why pages exist measured, instead of 70 to 80 µs from its SSD. that's double buffering: the same 8 KB can sit in shared buffers and in the OS cache at once. it wastes some RAM, and it's also why shared_buffers is usually set to a fraction of memory rather than most of it: the OS cache is the second tier.
| layer | what puts a page there | what makes it durable |
|---|---|---|
| shared buffers | any read or write, via the buffer manager | nothing: it's memory |
| OS page cache | postgres's write(), and its reads | fsync(), from the checkpointer |
| the disk | the kernel, eventually, or fsync() | it's there |
keylines
- a write dirties a page in shared buffers and appends WAL. COMMIT waits for the WAL, not the page.
- the checkpointer writes every dirty page on a schedule, then fsyncs each file.
- the background writer cleans buffers just ahead of the clock hand, so backends rarely have to.
- when nobody got there first, the backend writes the dirty page itself, mid-query.
- every write goes through the OS page cache. fsync is what makes it durable.
- the browser engine has none of these processes: your session does all of it.