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
- A transaction changes a row → the change is written to WAL (sequential, fast) and flushed on commit.
- Data pages are updated in memory and written to disk later at checkpoints.
- 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 failingarchive_commandstops 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."
More on PostgreSQL
How do you set up PostgreSQL and log in with psql?BeginnerHow do you create a database, tables, users and query data in PostgreSQL?BeginnerHow do you back up and restore a PostgreSQL database?BeginnerWhat is PostgreSQL and why is it so popular?BeginnerHow is PostgreSQL organised: cluster, database, schema and table?BeginnerHow do you connect to PostgreSQL from the command line?Beginner