// SQL snippet

Check for checkpoint pressure in PostgreSQL

A requested checkpoint is one that WAL volume forced before the schedule. This query shows how many of yours were forced.

Main statement
PostgreSQL
Также охватывает
MySQL

The task

A checkpoint writes every dirty page in shared_buffers to disk so older WAL can be recycled. PostgreSQL starts one when checkpoint_timeout (5 minutes by default) has passed since the last one, which counts as timed, or earlier when the WAL written since the last one approaches max_wal_size (1 GB by default), which counts as requested, as does a manual CHECKPOINT. Requested checkpoints that happen often mean max_wal_size is too small for the write load: checkpoints run back to back, and every page modified after each one writes a full-page image into WAL.

The snippet

Written for PostgreSQL. Versions for MySQL are under Variations.

PostgreSQL
SELECT num_timed, num_requested,
       ROUND(100.0 * num_requested / NULLIF(num_timed + num_requested, 0), 1) AS requested_pct,
       ROUND(write_time::numeric / 1000) AS write_seconds_total,
       ROUND(sync_time::numeric / 1000) AS sync_seconds_total,
       buffers_written,
       stats_reset
FROM pg_stat_checkpointer;

Как это работает

pg_stat_checkpointer exists from PostgreSQL 17; on older versions the same counters are columns of pg_stat_bgwriter (the variant). num_timed counts checkpoints due to checkpoint_timeout, including ones skipped because nothing had changed, and num_requested counts those forced by WAL volume or a CHECKPOINT command. A requested_pct that stays low means the schedule is in charge. A high one means raise max_wal_size until most checkpoints are timed, then confirm with log_checkpoints = on (on by default since PostgreSQL 15), which logs each checkpoint with its cause and duration. write_time and sync_time are in milliseconds. checkpoint_completion_target (0.9 by default) spreads each checkpoint's writes over most of the interval, which is why write time is large and harmless while sync time should stay small.

Variations

PostgreSQL 16 and earlier: pg_stat_bgwriter

PostgreSQL
SELECT checkpoints_timed, checkpoints_req,
       ROUND(100.0 * checkpoints_req / NULLIF(checkpoints_timed + checkpoints_req, 0), 1) AS requested_pct,
       ROUND(checkpoint_write_time::numeric / 1000) AS write_seconds_total,
       ROUND(checkpoint_sync_time::numeric / 1000) AS sync_seconds_total,
       buffers_checkpoint, buffers_clean, buffers_backend
FROM pg_stat_bgwriter;

MySQL 8.0.30 and later: redo checkpoint age and dirty pages

MySQL
SELECT
  (SELECT VARIABLE_VALUE FROM performance_schema.global_status
    WHERE VARIABLE_NAME = 'Innodb_redo_log_current_lsn')
  - (SELECT VARIABLE_VALUE FROM performance_schema.global_status
      WHERE VARIABLE_NAME = 'Innodb_redo_log_checkpoint_lsn') AS checkpoint_age_bytes,
  (SELECT VARIABLE_VALUE FROM performance_schema.global_variables
    WHERE VARIABLE_NAME = 'innodb_redo_log_capacity') AS redo_capacity_bytes,
  (SELECT VARIABLE_VALUE FROM performance_schema.global_status
    WHERE VARIABLE_NAME = 'Innodb_buffer_pool_pages_dirty') AS dirty_pages,
  (SELECT VARIABLE_VALUE FROM performance_schema.global_status
    WHERE VARIABLE_NAME = 'Innodb_log_waits') AS log_waits;

Questions that come up

What does the checkpoint warning in the log mean?

The log line LOG: checkpoints are occurring too frequently (N seconds apart), with a hint to consider increasing max_wal_size, is written when a checkpoint forced by WAL volume starts less than checkpoint_warning (30 seconds by default) after the previous one. The fix is raising max_wal_size; it can be changed with a reload, no restart. Size it so a normal busy period fits between timed checkpoints, and check that the warning stops.

Is frequent checkpointing really harmful?

Yes, in two ways. A page updated many times between two checkpoints is written to disk once per checkpoint, so more checkpoints mean more data-file writes for the same work. And with full_page_writes on (the default), the first change to each page after a checkpoint writes the whole 8 KB page into WAL, so frequent checkpoints inflate WAL volume, archive size and replication traffic. The cost of spacing them further apart is longer crash recovery, since more WAL has to be replayed.

Keep reading

// snippets are better with a schema brain

Run these with autocomplete that knows your tables

SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.