Sub topic 9
What Triggers a Checkpoint
Assume you know basic SQL, transactions, PostgreSQL pages, and configuration parameters. A checkpoint is a coordinated event in which PostgreSQL flushes dirty data pages to durable storage and records a new recovery starting point. Checkpoints limit how much WAL crash recovery must replay.
PostgreSQL normally starts a checkpoint for one of two reasons:
- The time since the previous checkpoint reaches
checkpoint_timeout. - PostgreSQL estimates that WAL usage has reached
max_wal_size.
The first condition is time-based. The second is volume-based. Whichever condition happens first requests the checkpoint.
Time-based checkpoints
checkpoint_timeout sets the maximum time between automatic checkpoints. Its default is commonly five minutes, but always check the value on the server you operate.
SHOW checkpoint_timeout;
SHOW max_wal_size;A busy system can generate enough WAL to trigger a checkpoint before the timeout. A quiet system can reach the timeout while generating little WAL. Therefore, checkpoint_timeout is an upper scheduling interval, not a promise that every checkpoint will happen at exactly that interval.
WAL-volume checkpoints
max_wal_size is a target for the amount of WAL that can accumulate between checkpoints. When PostgreSQL approaches this limit, it requests a checkpoint so older WAL can become recyclable or removable.
This limit is not a strict cap. WAL usage can temporarily exceed it because PostgreSQL must finish active operations and because the checkpoint process takes time. Replication slots, archiving delays, and other retention requirements can also keep old WAL files even after a checkpoint.
The following diagram shows the normal decision:
Explicit and operational requests
A client with sufficient privilege can request a checkpoint directly:
CHECKPOINT;This is useful for controlled experiments or before certain maintenance tasks, but frequent manual checkpoints can create unnecessary write bursts. A manual checkpoint does not permanently change checkpoint_timeout or max_wal_size.
Server shutdown also involves checkpoint-related work. A clean shutdown flushes necessary state so the next startup has less recovery work. An immediate or crash-like shutdown cannot perform that normal cleanup, so PostgreSQL uses WAL during the next startup to recover.
Distinguishing timed and requested checkpoints
PostgreSQL records whether checkpoints were caused by the timer or by another request, such as reaching the WAL target or executing CHECKPOINT. The statistics view differs by PostgreSQL version.
-- PostgreSQL 17 and later
SELECT num_timed, num_requested, write_time, sync_time
FROM pg_stat_checkpointer;
-- PostgreSQL 16 and earlier
SELECT checkpoints_timed, checkpoints_req,
checkpoint_write_time, checkpoint_sync_time
FROM pg_stat_bgwriter;A high requested-to-timed ratio often means the server reaches its WAL target before the timer. That pattern can indicate heavy write traffic, a small max_wal_size, or unexpectedly large WAL records. It does not automatically mean the setting is wrong. Checkpoint duration, storage capacity, recovery objectives, and replication requirements also matter.
Exercise
Which event triggers an automatic checkpoint first: reaching checkpointtimeout or reaching maxwal_size?
Show answer
Either event can trigger it. PostgreSQL requests a checkpoint when the first condition is reached.