Sub topic 7
Full-Page Writes and Torn Pages
This chapter assumes you know what a WAL record and an LSN are, that a checkpoint sets a redo point, and that PostgreSQL stores table and index data in 8 KB pages. It explains why WAL sometimes contains a complete copy of a page, why that raises WAL volume right after a checkpoint, and how to measure and tune the effect.
The problem: torn pages
PostgreSQL pages are 8 KB by default. Most filesystems and storage devices do not guarantee that an 8 KB write is atomic. The operating system may split it into smaller blocks, commonly 4 KB, or the device may write it in sectors. If the machine loses power or the kernel crashes in the middle of such a write, the page on disk can be a mix of old and new content. This is a torn page (also called a partial write).
A torn page is a problem because ordinary WAL records do not describe a whole page. A typical record says something like "insert this tuple at offset 5 of block 12". Replaying it assumes the page it applies to is a valid, consistent page. If the page is half old and half new, replay can produce garbage, and the damage may not be detected until much later.
The write-ahead rule does not fix this by itself. It guarantees that the log reaches disk before the data page, but it says nothing about the data page being intact after a crash in the middle of its write.
The solution: full-page images
When full_page_writes is on (the default), PostgreSQL does this:
- After a checkpoint starts, the first time a data page is modified, the WAL record for that change includes a complete copy of the page. This copy is a full-page image (FPI), also called a full-page write or backup block.
- Later changes to the same page, until the next checkpoint, are logged as ordinary small records.
During crash recovery, when replay meets a record carrying a full-page image, it overwrites the page on disk with that image. It does not matter whether the on-disk page was torn, because it is replaced entirely. Subsequent small records then apply to a known-good page.
Why "the first change after a checkpoint"? Recovery starts at the redo point of the last checkpoint. Any page modified after that point has an FPI in the WAL that recovery will read, so every page that could have been torn is covered. A page written to disk before the checkpoint was flushed as part of the checkpoint, so it is not at risk from writes that happened earlier.
Why WAL volume spikes after a checkpoint
Because the first touch of each page after a checkpoint writes about 8 KB instead of tens of bytes, WAL generation is highest right after a checkpoint and then falls as more pages have already been imaged. This has two practical consequences:
- A workload that touches many distinct pages, such as random updates across a large table, produces far more WAL than one that touches the same few pages repeatedly.
- Frequent checkpoints reset the "already imaged" state more often, so they increase total WAL volume. Fewer checkpoints means fewer FPIs for the same workload.
PostgreSQL can compress full-page images. The wal_compression setting (default off) compresses FPIs in WAL. Depending on the version, it accepts pglz, lz4 or zstd as methods when the server is built with support for them. It trades CPU for less WAL and is often worth testing on write-heavy systems.
Hint-bit changes can also trigger FPIs. If wal_log_hints is on, or the cluster was initialized with data checksums, the first hint-bit update to a page after a checkpoint can write an FPI even though no row changed. Data checksums are the usual reason this applies to a cluster.
Measuring full-page images
pg_stat_wal (PostgreSQL 14 and later) counts WAL records, full-page images and bytes since the last statistics reset.
SELECT wal_records, wal_fpi, pg_size_pretty(wal_bytes) AS wal_bytes, stats_reset
FROM pg_stat_wal;The ratio wal_fpi / wal_records shows how much of your WAL traffic comes from page images. A high ratio, together with frequent checkpoints, suggests that raising max_wal_size and checkpoint_timeout would reduce WAL volume.
You can see the effect directly by measuring WAL around a checkpoint. Run this in psql on a test server, using a table that already exists and has been flushed:
CREATE TABLE fpw_demo (id int PRIMARY KEY, payload text);
INSERT INTO fpw_demo SELECT g, repeat('x', 100) FROM generate_series(1, 10000) AS g;
CHECKPOINT;
SELECT pg_current_wal_lsn() AS lsn_a \gset
UPDATE fpw_demo SET payload = repeat('y', 100) WHERE id <= 5000;
SELECT pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), :'lsn_a')) AS first_update;
SELECT pg_current_wal_lsn() AS lsn_b \gset
UPDATE fpw_demo SET payload = repeat('z', 100) WHERE id <= 5000;
SELECT pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), :'lsn_b')) AS second_update;The first update touches pages for the first time since the checkpoint, so it includes an FPI for each page. The second update touches the same pages again, so it writes only small records and generates noticeably less WAL. Exact numbers depend on the page layout and on whether autovacuum or other activity runs at the same time. CHECKPOINT requires superuser or the pg_checkpoint role.
To look at the records themselves, use pg_waldump with --stats=record or by decoding records from an LSN. Records that carry an image show FPW in the output.
pg_waldump -p "$PGDATA/pg_wal" -s 0/1000028 -n 10Replace the start LSN with one from your own server.
The settings involved
SHOW full_page_writes;
SHOW wal_compression;
SHOW wal_log_hints;
SHOW checkpoint_timeout;
SHOW max_wal_size;| Setting | Default | Effect on FPIs |
|---|---|---|
| `full_page_writes` | `on` | Enables full-page images after each checkpoint. |
| `wal_compression` | `off` | Compresses FPIs, reducing WAL size at a CPU cost. |
| `wal_log_hints` | `off` | Logs hint-bit-only changes with FPIs. Implied by data checksums. |
| `checkpoint_timeout` | `5min` | Longer interval means fewer FPIs for the same workload. |
| `max_wal_size` | `1GB` | Larger value avoids early checkpoints triggered by WAL volume. |
full_page_writes can be changed with a configuration reload. wal_compression can be changed by a superuser in a session or with a reload.
Can you turn it off?
Setting full_page_writes = off removes the extra WAL, but it also removes the protection. After a crash, a torn page can go unnoticed and corrupt data, with the damage appearing as odd query results or errors later. Only consider turning it off if the storage stack guarantees atomic writes of the full page size. Examples that people sometimes rely on are certain copy-on-write filesystems configured appropriately, or hardware with atomic write support matched to the page size. This depends on the exact filesystem, block size and settings, so verify it for your environment instead of assuming it.
Common mistakes
- Treating the WAL spike after a checkpoint as a bug. It is expected behavior. Look at checkpoint frequency instead.
- Setting
checkpoint_timeoutvery low to shorten recovery. This increases FPI volume and I/O. Recovery time is better managed with a balanced setting, as covered in the tuning chapter. - Disabling
full_page_writesbecause "the disk is fast". Speed does not make a write atomic. - Reading
wal_fpiwithout a baseline. The counters are cumulative sincestats_reset. Compare two snapshots over a known period. - Forgetting data checksums. On clusters with checksums enabled, hint-bit updates also produce FPIs, which can surprise you on read-heavy workloads right after a checkpoint or after a standby is promoted.
Exercise
A server runs random UPDATEs across a 200 GB table, and pg_stat_wal shows that most WAL bytes come from full-page images. Checkpoints are happening every two minutes because max_wal_size is reached. Name two changes that would reduce WAL volume without giving up torn-page protection, and explain why each works.
Show answer
First, raise max_wal_size (and, if needed, checkpoint_timeout) so checkpoints occur less often. Each checkpoint resets which pages have been imaged, so fewer checkpoints means fewer first-touch full-page images for the same workload. Second, enable wal_compression, which compresses the full-page images in WAL at the cost of some CPU, shrinking the bytes written. Leave full_page_writes on, because turning it off removes protection against torn pages.
Exercise
Why does crash recovery not need a full-page image for a page that was last modified before the most recent checkpoint's redo point?
Show answer
Recovery replays WAL only from the redo point. A page modified before that point was flushed to disk by the checkpoint (or earlier), so its on-disk state is complete and no replayed record depends on a possibly torn copy of it. Any page modified after the redo point gets a full-page image on its first change, so recovery can overwrite a torn page with a good copy before applying later records.