Sub topic 9
What triggers a checkpoint
This chapter assumes you know what a checkpoint does (it flushes dirty pages and records a redo point) and what WAL segments are. It also assumes you can read postgresql.conf and query pg_settings. Here you will learn which events start a checkpoint, how to tell them apart, and which ones you can control.
Two kinds of trigger
Every checkpoint is performed by one process, the checkpointer. Something has to wake it up, and the cause falls into one of two groups:
- Scheduled checkpoints happen because
checkpoint_timeoutelapsed. Statistics views count these as timed. - Requested checkpoints happen because something asked for one: WAL volume, a SQL command, a backup, a shutdown, or a database-level operation. Statistics views count these as requested.
Most of the tuning work in the later chapters comes down to one aim: on a busy server, almost all checkpoints should be timed. Requested checkpoints caused by WAL volume are the ones to avoid.
Trigger 1: checkpoint_timeout
checkpoint_timeout sets the maximum time between automatic checkpoints. The default is 5 minutes. It accepts values from 30 seconds to 1 day. A reload is enough to change it.
A timed checkpoint is skipped when nothing has happened. If no WAL has been written since the previous checkpoint, the checkpointer does not write a new one. An idle server therefore does not produce a stream of identical checkpoint records.
A longer timeout means fewer checkpoints and less full-page-image overhead, because each page is logged in full only on its first change after a checkpoint. The cost is a longer crash recovery, since more WAL may need replaying. It also means more WAL has to be kept around.
Trigger 2: WAL volume and max_wal_size
max_wal_size (default 1GB) is the other main trigger. When enough WAL has been written since the last checkpoint, the server requests another one, even if checkpoint_timeout has not elapsed.
Two details are often misunderstood.
First, max_wal_size is a soft limit. The server does not stop writing WAL when it reaches it. The limit can be exceeded under heavy load, when archive_command fails, with a high wal_keep_size, or when a replication slot holds back old segments.
Second, the checkpoint is not triggered when pg_wal reaches max_wal_size. The server divides the limit by a factor so that a spread-out checkpoint can finish in time. The internal distance between checkpoints is approximately:
max_wal_size / (2 + checkpoint_completion_target)The "2" covers the WAL written during the previous checkpoint cycle and the one in progress. The term checkpoint_completion_target accounts for the time the current checkpoint spends spreading its writes. You can compute the figure for your server:
SELECT
(SELECT setting::numeric FROM pg_settings WHERE name = 'max_wal_size')
/ (2 + (SELECT setting::numeric FROM pg_settings
WHERE name = 'checkpoint_completion_target'))
AS approx_mb_between_wal_triggered_checkpoints;With the defaults (max_wal_size of 1024 MB and checkpoint_completion_target of 0.9), the result is about 353 MB. A server generating more than that between timeouts will have WAL-triggered checkpoints. At a rate of 2 MB/s the 353 MB threshold is reached in about three minutes, well before the 5-minute timeout.
The warning in the log
checkpoint_warning (default 30 seconds) makes the server log a message when checkpoints caused by WAL volume occur closer together than that interval. The message suggests increasing max_wal_size. If you see it often, the setting is too small for your write rate.
Trigger 3: the CHECKPOINT command
A superuser, or a role with the pg_checkpoint privilege (PostgreSQL 15 and later), can run CHECKPOINT. It is an immediate checkpoint: the checkpointer does not spread its writes, so it finishes as fast as the disks allow, and it can cause a burst of I/O. The command waits until the checkpoint is complete.
SELECT redo_lsn, checkpoint_time FROM pg_control_checkpoint();
CHECKPOINT;
SELECT redo_lsn, checkpoint_time FROM pg_control_checkpoint();The second query shows a newer redo_lsn and checkpoint_time. This is useful in tests, before taking a filesystem snapshot, and when you want a predictable starting point for recovery experiments. It is rarely a good idea on a busy production system.
Trigger 4: shutdown
A normal shutdown (smart or fast mode) ends with a shutdown checkpoint. It writes all dirty pages and records that the cluster was shut down cleanly. The next start then needs no WAL replay. An immediate shutdown (pg_ctl stop -m immediate) skips this checkpoint. It behaves like a crash, and the next start performs crash recovery.
A large shutdown checkpoint can delay a restart. If a lot of dirty data is waiting, the server may stay in the "shutting down" state for some time. Running CHECKPOINT manually just before a planned restart moves most of that work out of the shutdown window.
Trigger 5: backups
Starting a base backup (pg_basebackup, or pg_backup_start() in PostgreSQL 15 and later) forces a checkpoint. The backup needs a redo point that is not older than the moment the backup began. By default, pg_basebackup requests a spread checkpoint, which can take a while. You can ask for a fast one:
pg_basebackup -D /backups/base -c fast -PWith -c fast, the backup starts sooner and the checkpoint produces an I/O burst. With -c spread, the start waits for the paced checkpoint.
Trigger 6: database-level operations
Some commands force a checkpoint because they act on whole files rather than going through the buffer cache in the normal way:
DROP DATABASECREATE DATABASEusing the file-copy strategy, which is the default. The copy needs the source database's pages to be on disk first. In PostgreSQL 15 and later,STRATEGY = wal_logcopies through WAL instead and avoids the checkpoint.ALTER DATABASE ... SET TABLESPACEDROP TABLESPACE
These checkpoints are immediate. A system that creates and drops databases often (for example, one database per test run) will show many requested checkpoints that have nothing to do with max_wal_size.
Trigger 7: recovery
A standby does not write checkpoints. It performs restartpoints, which are the same kind of flush performed while replaying WAL, and they follow the checkpoint records that arrive from the primary. Promoting a standby, or finishing archive recovery, normally triggers an end-of-recovery checkpoint. This gives the new primary a clean starting point on its new timeline. You can see it in the log as checkpoint starting: end-of-recovery immediate wait.
Telling the causes apart
In the log
Set log_checkpoints = on. It is on by default in PostgreSQL 15 and later. Each checkpoint writes two messages. The first states the reason:
LOG: checkpoint starting: time
LOG: checkpoint starting: wal
LOG: checkpoint starting: immediate force wait
LOG: checkpoint starting: shutdown immediateRead the words as flags. time and wal are the two automatic causes. immediate means no spreading. force means the checkpoint was requested even if no WAL had been written. wait means a client is waiting for it to finish. The "checkpoint complete" message that follows reports the number of buffers written, the write and sync times, and the distance and estimate for WAL.
In statistics
In PostgreSQL 17 and later:
SELECT num_timed, num_requested, write_time, sync_time, buffers_written
FROM pg_stat_checkpointer;In PostgreSQL 16 and earlier:
SELECT checkpoints_timed, checkpoints_req,
checkpoint_write_time, checkpoint_sync_time, buffers_checkpoint
FROM pg_stat_bgwriter;num_requested (or checkpoints_req) lumps together every non-timed cause: WAL volume, manual commands, backups, and database operations. A high count does not by itself prove that max_wal_size is too small. To check, run CHECKPOINT and compare the counter before and after. Then look in the log for lines that say wal.
SELECT num_timed, num_requested FROM pg_stat_checkpointer;
CHECKPOINT;
SELECT num_timed, num_requested FROM pg_stat_checkpointer;Here num_requested increases by one, because a manual command counts as a requested checkpoint.
A practical diagnosis
Suppose num_requested is much larger than num_timed and the log is full of checkpoint starting: wal lines. You can follow these steps:
- Check
checkpoint_warningmessages in the log to confirm that WAL-triggered checkpoints are happening less than 30 seconds apart. - Estimate your WAL rate by taking
pg_current_wal_lsn()twice and passing both values topg_wal_lsn_diff. - Compare the rate with the computed distance from the formula above.
- Raise
max_wal_sizeso that, at peak write rate, the distance lasts at least as long ascheckpoint_timeout. - Reload the configuration and watch the counters again.
ALTER SYSTEM SET max_wal_size = '4GB';
SELECT pg_reload_conf();If pg_stat_checkpointer shows mostly timed checkpoints afterwards, the WAL trigger is no longer the limiting factor.
Summary table
| Trigger | Counted as | Spread over time? | Controlled by |
|---|---|---|---|
| `checkpoint_timeout` elapsed | timed | yes | `checkpoint_timeout` |
| WAL since last checkpoint | requested | yes | `max_wal_size` |
| `CHECKPOINT` command | requested | no | the caller |
| Shutdown | requested | no | shutdown mode |
| Base backup start | requested | optional (`-c fast`) | backup tool options |
| Database create/drop, tablespace drop | requested | no | the operation |
| End of recovery | requested | no | promotion or recovery end |
Exercise
A server has maxwalsize = 2GB and checkpointcompletiontarget = 0.9, and the log shows "checkpoint starting: wal" every 90 seconds. Roughly how much WAL is being written per second, and what would you change?
Show answer
The distance between WAL-triggered checkpoints is about 2048 MB / (2 + 0.9), which is roughly 706 MB. A checkpoint every 90 seconds means about 706 / 90, or about 7.8 MB of WAL per second. To make timed checkpoints the normal case with checkpointtimeout at 5 minutes (300 s), the distance needs to be at least 300 s x 7.8 MB/s, about 2.3 GB. Setting maxwal_size to 2.9 x 2.3 GB, about 6.8 GB (round up, for example to 8GB), achieves that. Check that disk space allows it, and then reload the configuration.
Exercise
After running CHECKPOINT on a PostgreSQL 17 server, which counter in pgstatcheckpointer increases, and why does that make numrequested alone a poor measure of whether maxwal_size is too small?
Show answer
numrequested increases. It counts every checkpoint that was not triggered by checkpointtimeout, including manual CHECKPOINT commands, backups, shutdowns, and database operations such as DROP DATABASE. To find out whether WAL volume is the cause, look at the "checkpoint starting: wal" lines in the log, or at checkpoint_warning messages.