Sub topic 10
Spreading Checkpoint Writes Over Time
This chapter assumes basic SQL and psql, familiarity with transactions and ACID properties, and a basic understanding of PostgreSQL data pages and configuration parameters.
A checkpoint writes dirty data pages from shared buffers to durable storage. If PostgreSQL writes all these pages in a short burst, disk latency can rise sharply. Queries and commits may compete with checkpoint I/O. PostgreSQL reduces this burst by spreading checkpoint writes across time.
What `checkpoint_completion_target` Controls
checkpoint_completion_target is the fraction of the expected checkpoint interval during which PostgreSQL tries to finish writing checkpoint data. Its usual default is 0.9.
For example, if PostgreSQL expects the next checkpoint in 10 minutes and the target is 0.9, it aims to spread writes over about 9 minutes instead of completing them immediately.
The target applies to writing dirty buffers. It does not delay the checkpoint forever. PostgreSQL still must finish the work before the checkpoint ends, and the actual duration can be shorter when there are few dirty pages.
| Setting | Effect |
|---|---|
| `0.5` | Writes checkpoint pages over roughly half the expected interval |
| `0.9` | Spreads writes over most of the expected interval |
| `1.0` | Uses nearly the full available interval |
A higher value usually produces smoother I/O. However, it leaves less time to finish the checkpoint if the system becomes busy or the next checkpoint is requested early.
Why Spreading Helps
Without spreading, a checkpoint can create a pattern like this:
- A checkpoint starts.
- PostgreSQL writes many dirty pages quickly.
- Storage bandwidth and I/O queues become saturated.
- User queries experience higher latency.
- The checkpoint finishes, and the burst ends.
With a high completion target, PostgreSQL writes smaller batches at regular intervals. This can reduce latency spikes and make storage utilization more predictable.
Common Trade-Offs
A high target is not automatically better. If the server generates WAL faster than checkpoints can write dirty pages, checkpoints may overlap with new pressure from later activity. A checkpoint that cannot keep up can contribute to frequent requested checkpoints and increased recovery work.
A low target may finish checkpoint writes quickly, but the resulting I/O burst can harm foreground workloads. A high target can smooth latency, but it requires storage that can sustain the average write rate.
The setting also cannot compensate for insufficient storage capacity or an undersized max_wal_size. Inspect behavior before changing it.
Inspecting and Changing the Setting
Check the current value and related limits:
SHOW checkpoint_timeout;
SHOW max_wal_size;
SHOW checkpoint_completion_target;You can change the target for the current session while testing:
SET checkpoint_completion_target = 0.9;To change it for the server, use configuration management appropriate to your environment. PostgreSQL normally applies this parameter after a configuration reload.
Choosing a Starting Point
Keep the default or another high value when checkpoint bursts cause latency spikes and storage has enough sustained write capacity. Investigate workload volume and storage performance when requested checkpoints are frequent. Do not use a low target merely to make each checkpoint finish sooner.
Exercise
A server has severe I/O spikes whenever a checkpoint begins, but its storage can sustain the required average write rate. Which direction should you try for checkpointcompletiontarget, and what should you measure afterward?
Show answer
Try a higher value, such as 0.9, then measure checkpoint write time, requested checkpoints, storage latency, and application response time.