Scrivio Interview library
DebuggingIntermediate6 min read · updated 12 November 2026

Write latency on a PostgreSQL 17 primary spikes every 5 minutes, and the log shows: 'LOG: checkpoints are occurring too frequently (24 seconds apart) HINT: Consider increasing the configuration parameter max_wal_size'. Explain what is going on and how you would tune it.

Heavy writes fill max_wal_size long before checkpoint_timeout expires, so PostgreSQL is forced to checkpoint every 24 seconds. Each checkpoint flushes dirty buffers and is followed by a burst of full page images in the WAL, and both raise I/O and write latency. We fix it by raising max_wal_size until checkpoints are mostly triggered by time, keeping checkpoint_completion_target around 0.9 to spread the writes, and sizing the pg_wal volume and recovery time for the larger setting, confirmed with pg_stat_checkpointer.

Why this question is asked

Periodic latency spikes are among the most confusing production problems, because nothing in the application changed and the database looks healthy between spikes. The log message in this question is a direct clue, and following it teaches how checkpoints, the WAL and disk I/O interact. Interviewers ask it to see whether we can connect a log line to the mechanism behind it and tune it without creating a new problem. In this answer, we will recap what a checkpoint does, see why ours run every 24 seconds, explain why each one hurts latency, tune the settings, and verify the result.

1A Quick Recap of Checkpoints

PostgreSQL makes commits durable by flushing the write-ahead log, not the data pages. Changed pages stay dirty in shared_buffers and are written later. A checkpoint is the process that eventually writes all of them: it flushes every page that was dirty when it started to the data files, then records a redo point, the WAL position from which crash recovery would have to start.

There are two triggers. A timed checkpoint happens every checkpoint_timeout, 5 minutes by default. A requested checkpoint happens earlier when the WAL written since the last checkpoint approaches max_wal_size, 1 GB by default, because PostgreSQL must not let pg_wal grow without bound. The warning in our log, controlled by checkpoint_warning (30 seconds by default), appears when requested checkpoints come closer together than that.

2Why Ours Run Every 24 Seconds

If the primary writes about 40 MB of WAL per second during busy periods, it fills 1 GB in roughly 25 seconds. Every 25 seconds the WAL limit is reached and a checkpoint is requested, long before the 5-minute timer. So almost every checkpoint is a requested one, which is exactly what the log line reports.

We can confirm this rather than guess. In PostgreSQL 17 and later, checkpoint statistics moved to their own view, pg_stat_checkpointer; on older versions the same columns are in pg_stat_bgwriter.

Java
-- PostgreSQL 17 and later
SELECT num_timed, num_requested, buffers_written, write_time, sync_time
FROM pg_stat_checkpointer;
-- num_timed 12, num_requested 4380 -> nearly all checkpoints forced by WAL volume
-- PostgreSQL 16 and earlier
SELECT checkpoints_timed, checkpoints_req, buffers_checkpoint, buffers_backend
FROM pg_stat_bgwriter;

Notice the ratio. A healthy busy server shows mostly timed checkpoints; ours shows hundreds of requested ones for every timed one.

3Why Each Checkpoint Hurts Latency

Two effects combine. First, the checkpoint itself writes dirty pages to disk. With frequent checkpoints, there is little time to spread that writing out, so the storage is busy with page writes exactly when backends need it to flush WAL for their commits. Commit latency, which waits on WAL fsync, goes up.

Second, right after each checkpoint, full page writes kick in. The first change to any page after a checkpoint writes the whole 8 KB page image into the WAL. Immediately after a checkpoint, almost every change is a first change, so WAL volume jumps several times over. That fills max_wal_size even faster, which brings the next checkpoint sooner, a self-reinforcing loop. More frequent checkpoints also mean the same hot pages are written to disk again and again instead of once per interval.

Step throughThe loop behind the spikes
  1. Heavy writes fill max_wal_size1 GB in about 25 seconds
  2. Requested checkpoint startsflushes dirty buffers, competes with WAL fsync
  3. First change to each page logs a full imageWAL volume jumps
  4. max_wal_size fills again even soonerthe next checkpoint arrives early
  5. Commit latency spikes at each checkpoint

5 steps in order. Play them, or step through them one at a time.

4Tuning: Make Checkpoints Time-Driven

The goal is that checkpoints are triggered by the timer, not by WAL volume. So we raise max_wal_size above the WAL a full interval produces. At 40 MB per second at peak, 5 minutes produces about 12 GB, so 16 GB leaves headroom. Many teams also raise checkpoint_timeout to 15 minutes, which reduces full page images further, with max_wal_size sized for that interval.

checkpoint_completion_target tells the checkpointer to spread its writes over that fraction of the interval. The default since PostgreSQL 14 is 0.9, which is what we want: writes are smoothed across 90 percent of the gap instead of arriving in a burst. Both max_wal_size and these settings can be changed with a reload, without restarting.

SQL
ALTER SYSTEM SET max_wal_size = '16GB';
ALTER SYSTEM SET checkpoint_timeout = '15min';
ALTER SYSTEM SET checkpoint_completion_target = 0.9;
ALTER SYSTEM SET wal_compression = 'lz4'; -- if the server was built with lz4; check your build
SELECT pg_reload_conf(); -- true

After the change, num_timed should grow and num_requested should almost stop growing, the warning should disappear from the log, and the latency graph should flatten.

5The Trade-Off: Recovery Time and Disk Space

A larger max_wal_size is not free. pg_wal may now hold up to 16 GB or more during busy periods, so the volume needs that space plus headroom for replication slots and archiving delays. And after a crash, recovery replays all WAL since the last redo point; with 15-minute checkpoints, recovery may take several minutes instead of seconds. We choose the values with the recovery time target in mind and test it, rather than raising the limits as far as possible.

Effect of the change
SettingBeforeAfter
max_wal_size1 GB16 GB
checkpoint intervalabout 24 s, requestedabout 15 min, timed
full page imagesafter every checkpoint, many times a minuteonce per page per 15 minutes
pg_wal disk useabout 1 GBup to about 16 GB, size the volume
crash recoverysecondspossibly minutes, test it

6Follow-up questions

What does it mean if backends write many buffers themselves?

It means that when a backend needed a free buffer, it found a dirty one and had to write it to disk itself before using it, which adds that write to the query's latency. It shows that neither the background writer nor checkpoints are cleaning buffers fast enough, or that shared_buffers is small for the working set. Before PostgreSQL 17 this was buffers_backend in pg_stat_bgwriter; in 16 and later, pg_stat_io shows writes by backend type. We would tune bgwriter_lru_maxpages and bgwriter_delay or revisit shared_buffers.

How would wal_compression change the picture?

It compresses full page images before they enter the WAL, using pglz or, if built in, lz4 or zstd. Full page images often dominate WAL volume after checkpoints, so compression can cut WAL by a large fraction, which fills max_wal_size more slowly and reduces I/O and replication traffic. The cost is some CPU on the primary, which is usually a good trade.

Should we lower shared_buffers to make checkpoints smaller?

Usually not. Fewer dirty pages per checkpoint would mean more reads from disk for normal queries. Spreading checkpoints and making them less frequent is the better lever.

7Conclusion

In this answer, we saw that the warning means checkpoints are forced by WAL volume: heavy writes fill max_wal_size long before checkpoint_timeout. Each checkpoint flushes dirty buffers, and after it full page images inflate WAL volume, which brings the next checkpoint even sooner and raises I/O and latency. We fix it by raising max_wal_size so checkpoints become timed, keeping checkpoint_completion_target near 0.9, perhaps adding wal_compression, and sizing pg_wal and recovery time for the new values, confirmed with pg_stat_checkpointer.

What to say in the interviewAbout 80 seconds aloud

Open with

The log tells me checkpoints are being requested because the WAL reaches max_wal_size every 24 seconds, far before checkpoint_timeout. So the write volume, maybe 40 MB of WAL per second, is driving checkpoints.

Then, if they want more

Each one flushes the dirty buffers to disk, competing with WAL fsyncs for I/O, and right after it full page writes log a whole page image for the first change to every page, which inflates WAL and brings the next checkpoint even sooner. That is the latency spike.

I would confirm it in pg_stat_checkpointer on PostgreSQL 17, or pg_stat_bgwriter on older versions, by comparing timed and requested checkpoints.

The fix is to raise max_wal_size so a full interval fits, for example 16 GB for a 5- or 15-minute checkpoint_timeout, and keep checkpoint_completion_target at 0.9 so writes are spread across the interval. wal_compression can reduce the full page image volume further.

The trade-off is that pg_wal will use more disk and crash recovery may take minutes rather than seconds, so I would size the volume and test recovery time against our target. Afterwards I expect requested checkpoints to stop growing and the latency graph to flatten.

About this answer

Last updated
12 November 2026
Versions it names
PostgreSQL 17, PostgreSQL 14, PostgreSQL 16
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
PostgreSQL documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Write latency on a PostgreSQL 17 primary spikes every 5 minutes, and the log shows...": https://scrivio-interview-library.netlify.app/databases/postgresql/a62990ef6d/