lesson depth
Mastery
not started · 0%

Write-Ahead Logging (WAL) & Crash Recovery

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

Freshness: current15 min readData Engineering and Databases

Key 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.

sql(9 lines)
1-- Query current WAL insertion LSN location
2SELECT pg_current_wal_lsn();
3
4-- Force a manual checkpoint to flush dirty buffer pages
5CHECKPOINT;
6
7-- Inspect WAL archiver status
8SELECT * 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.

Prerequisites & Related Concepts (2)

Private notes

0 words
Next