Concept lesson

Write-Ahead Logging (WAL) & Crash Recovery

Write-Ahead Logging architecture, LSN sequence numbers, checkpointing, and point-in-time recovery.

lesson
Freshness: current15 min read
Mastery
not started · 0%

Learning outcomes

  • Trace WAL LSN record writes during transactions
  • Configure checkpoint intervals for high-write workloads

Mental model

Write-Ahead Logging (WAL) guarantees durability (ACID D) by requiring changes to be logged sequentially to non-volatile disk before physical data pages are updated in buffer memory.

Transaction Modification
Append LSN record to WAL Buffer
Flush WAL to Disk (fsync)
Update Shared Buffer Data Page
Background Checkpoint Flush to Heap
Conceptual teaching model synthesized from:PostgreSQL 16 Architecture, MVCC & Query Optimization Manual

Theory

Every database modification generates a WAL record identified by a 64-bit Log Sequence Number (LSN). During COMMIT, PostgreSQL flushes the WAL buffer to disk via fsync(). During crash recovery, the database engine locates the last valid checkpoint LSN and replays subsequent WAL records (REDO phase) to restore state consistency.

-- Query current WAL insertion LSN location
SELECT pg_current_wal_lsn();

-- Force a manual checkpoint to flush dirty buffer pages
CHECKPOINT;

-- Inspect WAL archiver status
SELECT * FROM pg_stat_archiver;

Alternatives and trade-offs

  • Synchronous WAL Flush (synchronous_commit = on): Guarantees zero data loss on power failure; limits commit rate to disk fsync latency.
  • Asynchronous WAL (synchronous_commit = off): Achieves 10x higher write throughput; risks losing up to wal_writer_delay milliseconds of recent transactions on OS crash.

Failure modes and misconceptions

  1. Unconstrained WAL Accumulation: Disabling max_wal_size limits or accumulating unconsumed replication slots causes WAL files to fill disk partitions, shutting down PostgreSQL.
  2. FSYNC Bypassing: Setting fsync = off for performance corrupts database page structures during abrupt power loss.
Reflect before revealing the guide

Decision scenario

Use synchronous_commit = off for non-critical metric ingestion pipelines to maximize transaction throughput, but maintain strict synchronous_commit = on for financial transactions.

Learning outcomes

  • Trace WAL record creation and LSN ordering across transaction commits.
  • Configure checkpoint parameters (max_wal_size, checkpoint_completion_target).
  • Diagnose disk exhaustion caused by stale replication slots.

Trade-offs

WAL guarantees ACID crash durability and point-in-time recovery, but introduces sequential disk write overhead during high-frequency transaction commits.

Evidence assessment

Theory and decision mastery

not-started · 0%
theory0%
decision0%
activityNot mapped
projectNot mapped
1. What 64-bit integer monotonically identifies the byte position of a record in the PostgreSQL WAL stream?
2. What happens during a PostgreSQL database CHECKPOINT?
3. What is the main trade-off of setting synchronous_commit = off?

Decision scenario

A PostgreSQL primary database server disk space is rapidly filling up due to un-cleared WAL files in the pg_wal directory.

What is the most likely cause of un-cleared WAL accumulation?

Primary sources