Checkpointing

Difficulty: Advanced

Question

What is a checkpoint in a database, why is it needed alongside WAL, and what are the trade-offs of checkpointing more or less often?

Answer

A checkpoint is the point at which the database writes all dirty (modified in memory) data pages to the data files and records that position in the WAL. After a checkpoint, crash recovery only needs to replay WAL generated since that point, and older WAL files can be recycled or archived. Checkpoints bound both recovery time and the disk space taken by the log.

Why WAL alone is not enough

With WAL, committed changes are safe in the log even if the data pages are still only in memory. Without checkpoints:

  • The log would grow forever, because no WAL could ever be deleted.
  • Recovery after a crash would have to replay the log from the very beginning, which could take hours.

A checkpoint says: "Everything up to this LSN is now safely in the data files." That makes everything before it unnecessary for crash recovery.

What happens during a checkpoint (PostgreSQL)

  1. The checkpointer process notes the current WAL position as the checkpoint's redo point.
  2. It writes every dirty buffer in shared buffers to the data files, spreading the writes over time.
  3. It calls fsync on the data files so the writes are durable.
  4. It writes a checkpoint record to the WAL and updates pg_control with its location.
  5. WAL segments older than the redo point become eligible for recycling, unless archiving or replication slots still need them.

What triggers one

TriggerPostgreSQL setting / default
Time elapsedcheckpoint_timeout, default 5 minutes
WAL volumemax_wal_size, default 1 GB
ManualCHECKPOINT command (superuser or pg_checkpoint role)
Shutdown, start of a base backupAutomatic

checkpoint_completion_target (default 0.9) spreads the writes across 90% of the interval so a checkpoint does not cause a sudden I/O spike. InnoDB uses fuzzy checkpointing, continuously flushing pages so the redo log never fills up, and only does a full "sharp" checkpoint at shutdown.

The trade-off

Checkpoint more oftenCheckpoint less often
Faster crash recoverySlower crash recovery
Less WAL kept on diskMore WAL on disk
More total writes: hot pages flushed repeatedlyHot pages flushed once for many changes
More full-page images in WAL after each checkpointFewer full-page images, less WAL volume

The full-page image point surprises people: with full_page_writes = on, the first change to each page after a checkpoint logs the whole 8 KB page, so very frequent checkpoints can actually increase WAL volume.

Common production issues

  • Log messages saying "checkpoints are occurring too frequently" mean max_wal_size is too small for the write load, so WAL-size triggered checkpoints fire before the timeout. Raising it usually fixes it.
  • Latency spikes every few minutes often come from checkpoint I/O bursts; check the completion target and storage throughput.
  • In PostgreSQL 17 and later, checkpoint statistics moved to the pg_stat_checkpointer view (earlier versions show them in pg_stat_bgwriter).
What a PostgreSQL checkpoint does. A checkpoint flushes dirty pages so recovery can start from a recent point instead of the beginning of the log.
  1. Record the redo point: Note the current WAL position where recovery would start.
  2. Flush dirty buffers: Write modified pages from memory to data files, spread over time.
  3. fsync data files: Make sure the page writes are on stable storage.
  4. Write checkpoint record: Log the checkpoint and update pg_control.
  5. Recycle old WAL: Segments before the redo point can be reused or archived.

Code examples

Checking checkpoint behaviour in PostgreSQL 17+

SHOW checkpoint_timeout;           -- 5min default
SHOW max_wal_size;                 -- 1GB default
SHOW checkpoint_completion_target; -- 0.9 default

-- Timed vs requested checkpoints (PG 17+)
SELECT num_timed, num_requested, buffers_written,
       write_time, sync_time
FROM pg_stat_checkpointer;

-- Lots of num_requested relative to num_timed means
-- max_wal_size is too small for the write rate.
ALTER SYSTEM SET max_wal_size = '4GB';
SELECT pg_reload_conf();

If most checkpoints are requested (WAL-size triggered) rather than timed, the server is checkpointing more often than intended; raising max_wal_size spaces them out without a restart.

Key points

  • A checkpoint flushes dirty pages and marks a point from which recovery can start.
  • It limits both crash recovery time and how much WAL must be kept.
  • PostgreSQL defaults: checkpoint_timeout 5min, max_wal_size 1GB, completion target 0.9.
  • Frequent checkpoints mean faster recovery but more I/O and more full-page images in WAL.
  • PostgreSQL 17 moved checkpoint stats to pg_stat_checkpointer.

Common follow-up questions

  • What are full-page writes and why are they needed after a checkpoint?
  • How do replication slots prevent WAL from being recycled after a checkpoint?
  • What is the difference between the background writer and the checkpointer?
  • How does InnoDB's fuzzy checkpointing differ from PostgreSQL's approach?

Interview tips

  • Connect checkpoints to WAL immediately: WAL gives durability, checkpoints keep recovery time and log size bounded.
  • Explain the trade-off both ways, including the less obvious full-page-write effect.
  • Mentioning the real log warning about checkpoints occurring too frequently shows hands-on experience.

Concepts covered

Checkpoint, Dirty Pages, WAL Replay, Recovery Time, max_wal_size

More Database Design questions