Sub topic 10
Spreading checkpoint writes over time
This chapter assumes you know what a checkpoint does and what triggers one. It covers how PostgreSQL paces the checkpoint's disk writes, which setting controls the pacing, and how to check from the logs whether the pacing works.
Why a checkpoint should not write everything at once
A checkpoint must flush every dirty shared buffer that existed when it started. On a busy server that can be gigabytes. Writing them as fast as possible produces a burst of I/O. Queries that need to read from disk, and backends that must write WAL, then compete with the burst, and latency spikes appear at regular intervals.
PostgreSQL avoids this by pacing the write phase. The checkpointer writes some buffers, checks whether it is ahead of schedule, and sleeps if it is. The goal is a steady, low-rate stream of writes across most of the checkpoint interval.
Spreading is safe because durability does not depend on the checkpoint finishing quickly. Every change is already in WAL. A slow checkpoint only means the previous checkpoint's redo point stays the recovery starting point for longer. Crash recovery still starts from the last completed checkpoint.
The setting: checkpoint_completion_target
checkpoint_completion_target is a fraction between 0 and 1. It states how much of the checkpoint interval the write phase should take.
- The default is
0.9in PostgreSQL 14 and later. Earlier versions defaulted to0.5. - With
checkpoint_timeout = 5minand a target of0.9, the checkpointer aims to finish writing buffers about 270 seconds after it started. - The remaining 10% is a margin for the fsync phase and for slipping behind schedule.
- The parameter is reloadable. No restart is needed.
| Target | Write window with a 300 s timeout | Effect |
|---|---|---|
| 0.5 | about 150 s | Faster bursts, a long idle gap before the next checkpoint |
| 0.7 | about 210 s | Middle ground |
| 0.9 | about 270 s | Smoothest I/O, small margin |
| 1.0 or close | about 300 s | Next checkpoint may start while this one is still writing |
This query computes the intended write window from your own settings:
SELECT current_setting('checkpoint_timeout') AS checkpoint_timeout,
current_setting('checkpoint_completion_target') AS completion_target,
round(
extract(epoch FROM current_setting('checkpoint_timeout')::interval)
* current_setting('checkpoint_completion_target')::numeric
) AS intended_write_seconds;How the checkpointer decides whether it is on schedule
The checkpointer sorts the dirty buffers and writes them in order. After each write it asks whether it is on schedule. If it is ahead, it sleeps for a short nap (about 100 ms) before continuing. If it is behind, it keeps writing without sleeping.
"On schedule" is judged against two clocks:
- Time. The fraction of buffers written, scaled by
checkpoint_completion_target, must not lag behind the fraction ofcheckpoint_timeoutthat has elapsed. - WAL volume. The fraction of buffers written must not lag behind the fraction of the WAL budget consumed since the checkpoint started.
The checkpointer must satisfy both, so whichever clock is running faster sets the pace. On a quiet server the time clock dominates and the checkpoint stretches over most of the interval. During heavy writing, WAL accumulates quickly, the WAL clock dominates, and the checkpoint speeds up so that it finishes before the next one is due.
flowchart TDThe Mermaid block above is replaced by the properly formatted diagram below.
Where the WAL clock comes from
Checkpoints triggered by WAL volume are scheduled using max_wal_size. The server derives the amount of WAL it allows between the starts of two checkpoints from this formula:
WAL between checkpoints ≈ max_wal_size / (2 + checkpoint_completion_target)With max_wal_size = 1GB and a target of 0.9, a WAL-triggered checkpoint begins after roughly 350 MB of WAL. The divisor reflects that WAL from the previous checkpoint interval and from the current one must both be kept while a checkpoint is still being spread out. A higher target therefore lowers the WAL distance between checkpoint starts for a given max_wal_size. Remember this when you raise the target without changing max_wal_size, because checkpoints may then become more frequent.
What is not spread
Pacing applies only to the write phase of a checkpoint that was not requested as immediate. These cases skip the delay:
- The
CHECKPOINTcommand, which runs with immediate behavior. - The checkpoint at clean shutdown.
- Checkpoints that the server requests as immediate, for example at the start of
CREATE DATABASEor when a base backup is started with the fast option. - A running paced checkpoint when someone requests an immediate one. The checkpointer stops sleeping.
The fsync phase is also not stretched by the completion target. After the buffers are handed to the operating system, the checkpointer must make the files durable, and the time this takes depends on how much the kernel has buffered. This is why the target stops short of 1.0.
Helping the kernel with checkpoint_flush_after
Writing slowly is not enough if the operating system collects all the writes in its page cache and flushes them in one burst at fsync time. checkpoint_flush_after makes the checkpointer ask the kernel to start writeback after a given amount of data has been written.
- The default is 256 kB on Linux and 0 (disabled) on other platforms.
- It is reloadable.
- Setting it to
0disables the hint. - Values larger than a few megabytes usually bring back the burst you were trying to avoid.
Reading the checkpoint log
Set log_checkpoints = on to get one line per checkpoint. A healthy, spread checkpoint on a 5-minute timeout looks like this:
LOG: checkpoint starting: time
LOG: checkpoint complete: wrote 4321 buffers (26.4%); 0 WAL file(s) added, 0 removed, 3 recycled; write=269.812 s, sync=0.021 s, total=269.9 s; sync files=12, longest=0.010 s, average=0.002 s; distance=48213 kB, estimate=48213 kBRead it as follows:
checkpoint starting: timeshows the reason. Thetimereason meanscheckpoint_timeoutfired.walmeansmax_wal_sizewas approached.immediateorforcemeans a request skipped the pacing.write=269.812 sis close to 270 s (300 s times 0.9). This is the target working as intended.sync=0.021 sis the fsync phase. A large value here points to kernel writeback burst, so look atcheckpoint_flush_afterand storage speed.distanceis the amount of WAL produced since the previous checkpoint's redo point.
If write is much shorter than the interval and the reason is wal, the checkpoint was running behind the WAL clock and sped up. That means max_wal_size is too small for your write rate.
Counting timed and requested checkpoints
The statistics views show whether pacing has room to work. The view name depends on the version.
-- PostgreSQL 17 and later
SELECT num_timed, num_requested, write_time, sync_time, buffers_written
FROM pg_stat_checkpointer;
-- PostgreSQL 16 and earlier
SELECT checkpoints_timed, checkpoints_req, checkpoint_write_time,
checkpoint_sync_time, buffers_checkpoint
FROM pg_stat_bgwriter;A healthy server shows mostly timed checkpoints. A large share of requested checkpoints means the WAL clock keeps overriding the time clock, and the writes are being compressed into shorter windows.
Changing the settings
The following statements apply the usual starting point and enable logging, with no restart:
ALTER SYSTEM SET log_checkpoints = on;
ALTER SYSTEM SET checkpoint_completion_target = 0.9;
ALTER SYSTEM SET max_wal_size = '4GB';
SELECT pg_reload_conf();Check the effect over several checkpoint cycles, not one.
Trade-offs and common mistakes
- Raising the target to 1.0. There is no margin for the fsync phase. A checkpoint can still be running when the next one is due, so the checkpointer falls behind and writes become continuous.
- Keeping 0.5 after an upgrade. A configuration file carried over from an older version may still set
0.5. The result is write bursts with idle gaps. CheckSHOW checkpoint_completion_target;. - Tuning the target while
max_wal_sizeis too small. If most checkpoints arewal-triggered, the time clock never gets to set the pace, and the target has little influence. Raisemax_wal_sizefirst. - Expecting spreading to reduce total I/O. It does not. It changes when the writes happen. Total volume depends on the number of dirty pages and the interval between checkpoints.
- Expecting a longer write phase to lengthen recovery. Recovery time depends on the distance from the last completed checkpoint's redo point to the end of WAL. The redo point is the start of the checkpoint, so a longer write phase does not move it.
- Ignoring a long sync phase. If
sync=regularly takes seconds, spreading the writes did not keep the kernel from queuing a large backlog. Checkcheckpoint_flush_afterand the storage's write throughput.
Exercise
A server has checkpointtimeout = 10min, checkpointcompletiontarget = 0.8 and maxwal_size = 2GB. (a) How long is the intended write phase of a timed checkpoint? (b) Roughly how much WAL accumulates between the starts of two WAL-triggered checkpoints? (c) The log shows "checkpoint starting: wal" with write=95 s on every checkpoint. What does this say about the settings?
Show answer
(a) 600 s × 0.8 = 480 s, or 8 minutes. (b) 2GB / (2 + 0.8) ≈ 0.71 GB, so about 730 MB. (c) The checkpoints are triggered by WAL volume, not by time, and they finish in 95 s instead of the 480 s window, so the WAL clock is forcing them to run fast. maxwalsize is too small for the write rate. Raising it (and watching disk space for pg_wal) lets the time clock set the pace and spreads the writes more evenly.
Exercise
Why is setting checkpointcompletiontarget to 1.0 a poor choice even though it gives the smoothest possible write phase?
Show answer
The target only covers the write phase. The fsync phase and the checkpoint record come after it, so a target of 1.0 leaves no time for them before the next checkpoint is due. The checkpoint can overlap with the next one, and the checkpointer then runs nearly continuously. A value such as 0.9 keeps a margin for fsync and for a checkpoint that falls behind schedule.