skip to content

torn pages and full-page writes

an 8 KB page, a 4 KB disk write, and why the first change after a checkpoint logs the whole page.

address
everything.wal.full_page_writes
part
part 16, WAL and recovery
chapter
chapter 4 of 11
reading time
8 min

a postgres page is 8 KB, and a disk or filesystem may write it in smaller pieces. crash halfway and the file holds a torn page: half new, half old. a WAL record that says "change slot 7" can't fix that. so the first time a page changes after a checkpoint, its WAL record carries a full-page image, and redo restores the whole page from it. that's why WAL volume spikes right after every checkpoint.

the picture

you know how a WAL record is tiny, a few dozen bytes for a changed row? that only works if the page it applies to is intact. "insert this tuple in slot 7" means nothing on a page whose header was written but whose tuples weren't.

think of correcting a book with a list of errata (the WAL): "page 112, line 7: replace 'teh' with 'the'". that works on a clean page 112. it doesn't work if the printer jammed halfway through page 112 and the bottom half is from a different edition. so the first erratum for any page after each reprint (checkpoint) comes with a photocopy of the whole page (the full-page image). if the page turns out mangled, paste in the photocopy, then apply the errata.

full_page_writes =
page in shared buffers
old4 KB
old4 KB
page in the file
old4 KB
old4 KB
WAL on disk
checkpoint

after a checkpoint, the page in memory and the page on disk are the same.

changed, not yet on diskbeing writtenrestored from the imagetorn
step 1/5
drawn by hand · the textbook case: an 8 KB postgres page on a device that writes 4 KB at a time
step through, then switch full_page_writes off and step again. the crash comes between the two halves of one page write. with the image in the WAL, redo doesn't care what the file holds; without it, the change lands on a page that isn't there anymore.

postgres writes pages in 8 KB blocks. the operating system's own page is usually 4 KB, and a disk writes 512-byte or 4 KB sectors atomically at best. a crash or power cut in the middle of one write() can leave any mix. postgres can't detect every torn page by itself (checksums catch most of them, after the fact), so it makes sure it never needs the page that was on disk.

the first change after a checkpoint

redo starts from the last checkpoint's redo pointer (crash recovery). so the page that matters is the one as it was at that checkpoint, and the record that has to carry the image is the first one to touch the page after it. later records for the same page don't: redo will have restored the page from the image by the time it gets to them.

obsCHECKPOINT

CHECKPOINT writes every dirty buffer out, fsyncs the files, then logs where redo would start.

flush line: durable from here backin wal_buffers, not on diskredo pointer, record headerjust appended, being replayeddirty page
step 1/5
real output, a postgres 18.6 server · checkpoint-fpi
step through: a checkpoint, two updates to the same page, another checkpoint, a third update. records with +FPI carry the page. the redo pointer is what decides who pays.

on a real server: the first update after the checkpoint wrote a 5181-byte record, 5116 of them the page image. the second update to the same page wrote 82 bytes. after the next checkpoint, the third update paid again: 5285 bytes. the rule postgres applies when it builds each record: if the page's LSN is not newer than the current redo pointer, this is the first change since the checkpoint began, so include the page.

the image isn't always 8 KB: postgres leaves out the hole between pd_lower and pd_upper (the page's free space), so a page that's 60% full logs about 60% of a page. you can measure it in the browser too. (the first SELECT sets lab's hint bits before the checkpoint, so the image below belongs to the UPDATE and not to a hint-bit record: more on that at the end.)

sqldb=lab · session fpw
SELECT count(*) FROM accounts;
CHECKPOINT;
SELECT set_config('my.a', pg_current_wal_insert_lsn()::text, false) IS NOT NULL AS marked;
UPDATE accounts SET balance = balance + 1 WHERE id = 1;
SELECT record_type, record_length, block_fpi_length AS fpi_length
FROM pg_get_wal_block_info(current_setting('my.a')::pg_lsn, pg_current_wal_insert_lsn())
WHERE relfilenode = pg_relation_filenode('accounts') ORDER BY start_lsn;
output, from postgres 18.6
#1 · SELECT 1
countint8
3
(1 row)
#2 · CHECKPOINT
#3 · SELECT 1
markedbool
t
(1 row)
#4 · UPDATE 1
#5 · SELECT 1
record_typetextrecord_lengthint4fpi_lengthint4
HOT_UPDATE265200
(1 row)
sqldb=lab · session fpw
continues session fpw (1 earlier block run first)
SELECT set_config('my.a', pg_current_wal_insert_lsn()::text, false) IS NOT NULL AS marked;
UPDATE accounts SET balance = balance + 1 WHERE id = 2;
SELECT record_type, record_length, block_fpi_length AS fpi_length
FROM pg_get_wal_block_info(current_setting('my.a')::pg_lsn, pg_current_wal_insert_lsn())
WHERE relfilenode = pg_relation_filenode('accounts') ORDER BY start_lsn;
output, from postgres 18.6
#1 · SELECT 1
markedbool
t
(1 row)
#2 · UPDATE 1
#3 · SELECT 1
record_typetextrecord_lengthint4fpi_lengthint4
HOT_UPDATE700
(1 row)

lab's accounts page holds three short rows, so its image is small. a full page logs close to 8 KB per first touch.

predict the output
right after a CHECKPOINT, you UPDATE one row on each of 1,000 different pages. roughly how much WAL?
sql
CHECKPOINT;
UPDATE big SET flag = true WHERE id % 50 = 0;   -- one row per page, 1,000 pages

with and without

the same one-row UPDATE, right after a checkpoint, on a real server, with every option postgres 18.6 offers:

first change5,173 B
first, pglz1,093 B
first, lz41,247 B
first, zstd718 B
same page again76 B
first, fpw off76 B
real output, a postgres 18.6 server · fpi-sizes
WAL bytes of the heap record for one small UPDATE. "first change" is right after a CHECKPOINT; the page is about 60% full.

5173 bytes with the image, 76 without. full_page_writes = off makes the first change as cheap as any other (76 bytes) and leaves you exposed to exactly the torn page above. wal_compression compresses only the page images: pglz (since 9.5), lz4 and zstd (since 15). here zstd shrank the record to 718 bytes, lz4 to 1247, pglz to 1093. the cost is CPU on every image written and read. the browser's postgres is built without lz4 and zstd, so there only pglz works:

sqldb=lab · session fpw
continues session fpw (2 earlier blocks run first)
SET wal_compression = pglz;
CHECKPOINT;
SELECT set_config('my.a', pg_current_wal_insert_lsn()::text, false) IS NOT NULL AS marked;
UPDATE accounts SET balance = balance + 1 WHERE id = 3;
SELECT record_type, record_length, block_fpi_length AS fpi_length
FROM pg_get_wal_block_info(current_setting('my.a')::pg_lsn, pg_current_wal_insert_lsn())
WHERE relfilenode = pg_relation_filenode('accounts') ORDER BY start_lsn;
output, from postgres 18.6
#1 · SET
#2 · CHECKPOINT
#3 · SELECT 1
markedbool
t
(1 row)
#4 · UPDATE 1
#5 · SELECT 1
record_typetextrecord_lengthint4fpi_lengthint4
HOT_UPDATE235168
(1 row)

images for hint bits

a page can change without any row changing: setting hint bits on a read. normally that's not logged at all, because losing a hint bit is harmless. but with data checksums on (the default since postgres 18) or wal_log_hints, a torn hint-bit write would leave a page whose checksum doesn't match, so the first hint-bit change after a checkpoint logs the page as an XLOG FPI_FOR_HINT record. that's the read that writes WAL in commit log and hint bits, and the catalog records at the top of WAL records. later hint-bit changes to the same page, before the next checkpoint, log nothing.

keylines

  • an 8 KB page can be written half-way. a WAL record can't fix a torn page.
  • so the first change to a page after a checkpoint logs the whole page, minus its free-space hole.
  • redo restores the page from the image and doesn't read the file's copy at all.
  • every checkpoint makes the next touch of every page expensive. WAL spikes after checkpoints.
  • wal_compression shrinks the images: pglz, lz4, zstd. full_page_writes = off risks torn pages.
  • with checksums on, a hint-bit change after a checkpoint logs an FPI_FOR_HINT.