What is WAL in PostgreSQL?

IntermediateConceptPostgreSQL
Answer

Short answer

The Write-Ahead Log (WAL) records every change before it's written to the data files. If the server crashes, Postgres replays the WAL to recover to a consistent state. WAL is also what powers streaming replication and point-in-time recovery (PITR).

How it works

  1. A transaction changes a row → the change is written to WAL (sequential, fast) and flushed on commit.
  2. Data pages are updated in memory and written to disk later at checkpoints.
  3. Crash → on restart, Postgres replays WAL from the last checkpoint.

Uses

Use How
Crash recovery Automatic replay on startup
Streaming replication Replicas receive and replay WAL in real time
PITR Base backup + archived WAL replayed up to a chosen time

DevOps issues

  • Disk filling with WAL (pg_wal/ growing): a stuck replication slot or failing archive_command stops old WAL being removed.
SELECT slot_name, active, pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)) AS retained
FROM pg_replication_slots;

Drop slots for replicas that no longer exist.

What to say in the interview

"WAL is the write-ahead log: changes hit WAL before data files, which gives crash recovery and powers streaming replication and PITR. A classic incident is pg_wal filling the disk because of an inactive replication slot or failing WAL archiving."

All Databases (MySQL, PostgreSQL, MongoDB) questions Roadmap: PostgreSQL basics

More on PostgreSQL