← Back to PostgreSQL, page 5

postgresql-0094 · Hard senior

Explain WAL, checkpoints and full-page writes, and what tuning max_wal_size changes.

Every change is first written to the write-ahead log, which is flushed at commit for durability; data pages are written later. A checkpoint flushes all dirty pages so older WAL can be recycled, and after each checkpoint the first change to a page writes the full page image into WAL to protect against torn pages. Larger max_wal_size and checkpoint_timeout mean fewer checkpoints and fewer full-page images, at the cost of longer crash recovery and more WAL disk.

Full answer

How the pieces fit:

  • Commit: the WAL records of a transaction are flushed to disk (unless synchronous_commit is off); the changed data pages stay in shared buffers.
  • Background writer and checkpointer write dirty pages over time. A checkpoint guarantees all changes up to its start are on disk, so recovery only replays WAL from the last checkpoint.
  • Full-page writes: the first modification of each page after a checkpoint logs the whole 8 kB page. Frequent checkpoints therefore inflate WAL volume, which also burdens replication and archiving.
  • Checkpoints are triggered by checkpoint_timeout or by WAL reaching max_wal_size; checkpoint_completion_target spreads the writes out.

Symptoms of checkpoints that are too frequent: log lines saying checkpoints are occurring too frequently, high WAL volume, I/O spikes, and replication lag after bulk loads. Typical tuning on a busy server: raise max_wal_size (several GB or more), set checkpoint_timeout to 10 to 15 minutes, and keep completion target at 0.9.

Trade-off: recovery after a crash replays more WAL, so recovery time grows with the interval. Measure with pg_stat_checkpointer (PostgreSQL 17) and WAL generation rates. This item is descriptive and nothing was run here.

Key points

  • WAL is written and flushed at commit
  • Checkpoints flush dirty pages
  • Full-page writes follow each checkpoint
  • Larger max_wal_size means fewer checkpoints
  • Longer intervals lengthen crash recovery

Common mistakes

  • Turning off fsync or full_page_writes for speed
  • Leaving max_wal_size at the default on write-heavy systems

Follow-ups

  • What does synchronous_commit = off risk?
  • How does wal_compression help?

Tips

  • Mention pg_stat_checkpointer