skip to content

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:

T1SELECT count(*), sum(balance) FROM accounts
+ 786 frames holding other relations, not drawn
page table: page → frame

read every row. four heap pages and the index's pages are in shared buffers, all clean: they match the file.

hitread from diskdirtypinnedempty frame
step 1/5
real output, a postgres 18.6 server · dirty-pages
step through. a frame with a corner is dirty: its page differs from the file. that's normal, not an error. the UPDATE dirties every page it touches, and the CHECKPOINT writes them all out and leaves the frames clean, still holding their pages.

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_writtenint8writesint8fsyncsint8
14714751
(1 row)
what the checkpoint wrote, from its own counters, after zeroing them. the pool had 147 dirty buffers just before it.

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:

sqlempty db
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;
output, from a real postgres 18.6 server
SELECT 6
nametextsettingtextunittext
bgwriter_delay200ms
bgwriter_lru_maxpages100null
bgwriter_lru_multiplier2null
checkpoint_completion_target0.9null
checkpoint_timeout300s
max_wal_size1024MB
(6 rows)

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_typetextpages_writtennumeric
client backend589
(1 row)
writes by who did them, from pg_stat_io, after the UPDATE. nobody else was running.

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:

sqldb=lab
SELECT backend_type, count(*) FROM pg_stat_activity GROUP BY backend_type ORDER BY backend_type;
output, from postgres 18.6
SELECT 1
backend_typetextcountint8
standalone backend1
(1 row)
sqlempty db
SELECT backend_type, count(*) FROM pg_stat_activity GROUP BY backend_type ORDER BY backend_type;
output, from a real postgres 18.6 server
SELECT 6
backend_typetextcountint8
background writer1
checkpointer1
client backend2
io worker3
logical replication launcher1
walwriter1
(6 rows)

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.)

predict the output
you UPDATE a row and COMMIT. COMMIT returns. what's been written to the table's file?
sql
BEGIN;
UPDATE accounts SET balance = balance + 1 WHERE id = 1;
COMMIT;   -- returns

the 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.

layerwhat puts a page therewhat makes it durable
shared buffersany read or write, via the buffer managernothing: it's memory
OS page cachepostgres's write(), and its readsfsync(), from the checkpointer
the diskthe 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.