Sub topic 7
Full-Page Writes and Torn Pages
A prerequisite for this chapter is basic knowledge of PostgreSQL pages, WAL records, transactions, and checkpoints. You should also be comfortable running SQL in psql.
The torn-page problem
PostgreSQL stores table and index data in fixed-size pages. A page is usually 8 KiB, although the size is chosen when PostgreSQL is built. The operating system and storage device may write that page in smaller pieces.
Suppose PostgreSQL changes a page in memory and later writes it to disk. If the server loses power during the write, only part of the page might reach storage. The disk can then contain a mixture of the old page and the new page. This is a torn page.
A torn page may contain invalid or inconsistent data. Replaying ordinary WAL records might not repair it because the recovery process expects the page to begin from a valid earlier version.
How full-page writes protect recovery
PostgreSQL uses full-page writes to solve this problem. When full_page_writes is enabled, PostgreSQL puts a complete copy of a data page into WAL the first time that page is modified after a checkpoint.
The full copy is called a full-page image, or FPI. If a crash leaves a torn page on disk, crash recovery can first restore the complete page from the FPI. It can then replay later WAL records that describe changes made after that image.
flowchart TD
A[Checkpoint completes] --> B[Page is modified]
B --> C[WAL stores a full-page image]
C --> D[Later changes produce normal WAL records]
D --> E[Crash occurs during a data-page write]
E --> F[Recovery restores the full-page image]
F --> G[Recovery replays later WAL records]
G --> H[Consistent page]caption: How a full-page image repairs a torn page during recovery alt: A flowchart showing a checkpoint, a page modification, a full-page image in WAL, a crash, and recovery replay.
An FPI is protected by the write-ahead rule. PostgreSQL writes the WAL containing the image before it allows the corresponding data page to be flushed. The recovery process can therefore trust the WAL copy even when the data-page write was interrupted.
After the first modification to a page following a checkpoint, later modifications usually do not need another FPI until the next checkpoint. This limits the extra WAL volume.
Why checkpoints affect WAL volume
Checkpoints reset the point at which PostgreSQL considers pages protected by a new full-page image. Immediately after a checkpoint, the first change to many different pages can produce an FPI. Workloads that touch many pages may therefore generate a burst of WAL after each checkpoint.
The wal_fpi statistic counts full-page images since the statistics were reset. Compare it with total WAL records and bytes:
SHOW full_page_writes;
SELECT wal_records,
wal_fpi,
pg_size_pretty(wal_bytes) AS wal_bytes,
stats_reset
FROM pg_stat_wal;wal_fpi is a count of records containing full-page images. It is not a direct byte count, because page images and WAL records can have different sizes. Compression settings, page contents, and other WAL records affect the total.
Compression and trade-offs
Full-page images increase WAL volume, especially after checkpoints. PostgreSQL can compress them when wal_compression is enabled. Compression can reduce write and storage pressure when pages contain repeated or otherwise compressible data, but it uses CPU time.
The right choice depends on the bottleneck:
| Situation | Likely concern | Possible response |
|---|---|---|
| Many FPIs after checkpoints | Extra WAL volume | Measure `wal_fpi`, then consider WAL compression or less frequent checkpoints |
| High CPU usage | Compression cost | Test compression settings against the workload |
| Untrusted storage behavior | Torn-page risk | Keep `full_page_writes` enabled |
Do not treat a high FPI count as proof of a problem. It must be compared with workload volume, checkpoint activity, disk throughput, and recovery requirements.
Exercise
Why does PostgreSQL write a full-page image after a checkpoint instead of relying only on the ordinary WAL record for a page change?
Show answer
A crash can leave the data page torn. The full-page image gives recovery a complete valid page to restore before it replays the ordinary WAL record.