Overview

The write-ahead log (WAL) is a sequential, append-only record of every change, flushed to disk before the data files are touched. After a crash the database replays it to restore committed state, which is the Durability in acid. It also powers replication, point-in-time recovery, and CDC.

Rules

  • PostgreSQL ships WAL to standbys for streaming replication (read-replica). Logical replication and CDC tools decode it into row changes and need wal_level = logical; the default is replica.
  • Archive WAL plus a base backup to restore to any committed moment.
  • synchronous_commit defaults to on. off can lose the last few commits in a crash but never corrupts data; scope it with SET LOCAL to loss-tolerant batch loads.
  • WAL volume drives disk, backup, and replica sizing; heavy vacuum and many indexes add to it (write-amplification). A stalled replication slot or failing archive makes WAL pile up until the disk fills; cap slot retention with max_slot_wal_keep_size.
  • max_wal_size (default 1 GB) is a soft limit that triggers checkpoints. If the log warns that checkpoints come too often (checkpoint_warning, 30 s), raise it.

Enable SQLite WAL mode for concurrent readers and one writer

PRAGMA journal_mode=WAL; persists in the database file. Readers and the writer no longer block each other, but there is still one writer at a time.

  • All processes must share one host; network filesystems are unsupported.
  • Expect -wal and -shm files and checkpointing (default every 1000 pages). Long-running readers can starve checkpoints and let the WAL grow.
  • With PRAGMA synchronous=NORMAL, a power loss can drop recent commits but not corrupt the database.
  • Use SQLite 3.51.3 or later (or 3.44.6 or 3.50.7) to avoid a rare WAL-reset corruption bug.
SET LOCAL synchronous_commit = off;  -- inside a batch transaction where loss is acceptable