Overview

The write-ahead log records every change before the data page is modified, so a crash can be repaired by replaying the log from the last checkpoint. WAL is also the stream that physical replication, point-in-time recovery and logical decoding all consume (pg-replication-failover, pg-backup-pitr). It implements the durability in transactions-acid.

Core concepts

  • WAL is a sequence of 16 MB segment files in pg_wal/, addressed by an increasing LSN (log sequence number).
  • On COMMIT the commit record is flushed to disk (fsync) before the client is told success. Data pages stay dirty in shared_buffers and are written later.
  • A checkpoint flushes all dirty pages and records a redo point. Recovery starts from the last checkpoint’s redo point. Checkpoints happen every checkpoint_timeout (default 5 minutes) or when WAL reaches max_wal_size (default 1 GB), and are spread over checkpoint_completion_target (0.9) of the interval to smooth I/O.
  • Full-page writes (full_page_writes = on): the first change to a page after a checkpoint logs the whole 8 kB page, protecting against torn pages. This is why WAL volume spikes right after a checkpoint.
  • wal_level: minimal, replica (default), logical (adds the information logical decoding needs).
  • synchronous_commit: on (default) waits for the local flush; off returns before the flush, risking loss of the last few hundred milliseconds of commits (but not corruption). With synchronous replication it can also wait for standbys (pg-replication-failover).

Worked example

Measuring WAL volume on PostgreSQL 17:

SELECT pg_current_wal_lsn() AS a \gset
INSERT INTO b SELECT g + 200000, 'w' FROM generate_series(1, 10000) g;
SELECT pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), :'a')) AS wal_for_10k_rows;
-- 1274 kB

Inserting 10,000 small rows produced about 1.3 MB of WAL, roughly 130 bytes per row including index and page overhead. Defaults on the test server: wal_level = replica, max_wal_size = 1GB, checkpoint_timeout = 5min.

Useful views: pg_stat_wal (WAL volume, full-page images), pg_stat_checkpointer (PostgreSQL 17; in 16 and earlier the columns are in pg_stat_bgwriter): a high count of requested checkpoints compared with timed ones means max_wal_size is too small for the write load.

Pitfalls and production notes

  • WAL directory fills up when an archive command fails, a replication slot’s consumer is gone, or wal_keep_size is huge. Watch pg_replication_slots (restart_lsn, wal_status); set max_slot_wal_keep_size as a safety limit.
  • Very frequent checkpoints (small max_wal_size) mean more full-page images and more I/O. Typical write-heavy servers raise max_wal_size to several GB.
  • Never turn fsync off on data you care about.
  • Put WAL on fast, low-latency storage; commit latency is bounded by WAL flush time.
  • Bulk loads can use COPY and unlogged tables (CREATE UNLOGGED TABLE, truncated after a crash) when durability is not needed.

Practice questions

  1. Why can the database lose no committed data on a crash even though data files were not flushed?
  2. What is the trade-off of synchronous_commit = off?
  3. WAL growth is 200 GB overnight. List three causes.

References

pg-architecture · pg-backup-pitr · pg-replication-failover · transactions-acid