Difficulty: Advanced
What is Write-Ahead Logging (WAL) and how does it guarantee durability after a crash?
Write-Ahead Logging means the database records every change in an append-only log on disk, and forces that log record to stable storage before the transaction is reported as committed and before the modified data page is written. If the server crashes, the data files may be stale or half-written, but replaying the log from the last checkpoint brings them back to a consistent, committed state. That is how the D in ACID is delivered without writing every data page on every commit.
A modified data page may not be flushed to disk until the log records describing that change are already durable. In practice:
COMMIT, the WAL is flushed with fsync (or equivalent) up to the commit record's LSN.Each page stores the LSN of the last change applied to it, so recovery knows which records still need replaying.
When PostgreSQL restarts after a crash it reads the last checkpoint location from pg_control and replays (redoes) every WAL record from that point forward. Records for transactions that never committed are replayed too, but MVCC makes their row versions invisible, so PostgreSQL needs no separate undo phase. Engines like InnoDB and the ARIES family do redo first and then undo (roll back) uncommitted changes using undo logs.
Partial page writes are handled separately: PostgreSQL's full_page_writes logs a full image of a page the first time it changes after a checkpoint, and InnoDB uses a doublewrite buffer for the same purpose.
DELETE.| Setting (PostgreSQL) | Effect |
|---|---|
synchronous_commit = on (default) | Commit waits for local WAL flush; no committed data lost |
synchronous_commit = off | Commit returns before flush; a crash can lose the last few hundred milliseconds of commits, but never corrupts data |
fsync = off | Never safe in production; a crash can corrupt the cluster |
wal_level | replica (default) or logical for logical decoding |
A common mistake is assuming the disk is durable when fsync returns. Consumer SSDs or virtual disks with volatile write caches can lie, which is why production databases run on storage with power-loss protection.
-- Current WAL write position
SELECT pg_current_wal_lsn();
-- How much WAL an operation generates
SELECT pg_current_wal_lsn() AS before \gset
UPDATE orders SET status = 'SHIPPED' WHERE created_at < now() - interval '7 days';
SELECT pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), :'before')) AS wal_generated;
-- Durability-related settings
SHOW wal_level; -- replica by default
SHOW synchronous_commit; -- on by default
SHOW full_page_writes; -- on by default
-- Relax durability for one low-value transaction only
BEGIN;
SET LOCAL synchronous_commit = off;
INSERT INTO page_views (user_id, path) VALUES (42, '/jobs');
COMMIT;
pg_wal_lsn_diff measures how many bytes of WAL a statement produced. SET LOCAL synchronous_commit = off trades a tiny window of possible loss for faster commits on data you can afford to lose, without risking corruption.
Write-Ahead Log, Durability, Crash Recovery, fsync, Log Sequence Number