Sub topic 12
Observing WAL and Checkpoints
This chapter assumes basic SQL and psql, familiarity with transactions, and a basic understanding of PostgreSQL pages and configuration parameters. The examples require permission to read the relevant statistics views. Some checkpoint functions require a superuser or a role with suitable privileges.
Start with the current WAL position
A log sequence number (LSN) identifies a position in PostgreSQL's write-ahead log (WAL). LSNs increase as PostgreSQL writes WAL records. The current LSN tells you where new WAL is being appended.
SELECT pg_current_wal_lsn() AS current_lsn,
pg_walfile_name(pg_current_wal_lsn()) AS wal_file;The result includes the WAL segment file containing the current position. PostgreSQL normally stores these files in the pg_wal directory. The file name alone does not tell you how much WAL a workload generates, so compare two LSNs.
Measure WAL generated by a statement
In psql, \gset saves a query result as a variable. This example records an LSN, performs an insert, and measures the difference.
CREATE TABLE wal_demo (id int PRIMARY KEY, payload text);
SELECT pg_current_wal_lsn() AS before_lsn \gset
INSERT INTO wal_demo
SELECT g, repeat('x', 100)
FROM generate_series(1, 10000) AS g;
SELECT pg_size_pretty(
pg_wal_lsn_diff(pg_current_wal_lsn(), :'before_lsn')
) AS wal_generated;The result is approximate for the operation because other activity on the server can generate WAL between the two readings. Run the test on an otherwise quiet server when comparing changes.
Inspect checkpoint state
A checkpoint writes dirty data pages and records a consistent recovery point. The control-file function exposes the latest checkpoint and its redo LSN.
SELECT checkpoint_lsn,
redo_lsn,
redo_wal_file,
timeline_id,
checkpoint_time
FROM pg_control_checkpoint();During crash recovery, PostgreSQL starts replaying WAL from the redo LSN. The distance between the redo point and the end of WAL gives a rough indication of how much work recovery may need to perform.
Check checkpoint activity and WAL volume
The statistics views show whether checkpoints happen because of the timer or because PostgreSQL reaches the WAL limit.
| Observation | What it can indicate |
|---|---|
| Requested checkpoints are frequent | `max_wal_size` may be too small or WAL generation is high |
| Timed checkpoints dominate | The timeout is usually controlling checkpoint frequency |
| High `wal_fpi` after checkpoints | Full-page images are adding WAL after each checkpoint |
On PostgreSQL 17 and later, query pg_stat_checkpointer:
SELECT num_timed,
num_requested,
write_time,
sync_time,
buffers_written
FROM pg_stat_checkpointer;On PostgreSQL 16 and earlier, use the corresponding columns in pg_stat_bgwriter:
SELECT checkpoints_timed,
checkpoints_req,
checkpoint_write_time,
checkpoint_sync_time,
buffers_checkpoint
FROM pg_stat_bgwriter;For WAL totals, PostgreSQL 14 and later provide pg_stat_wal:
SELECT wal_records,
wal_fpi,
pg_size_pretty(wal_bytes) AS wal_bytes,
stats_reset
FROM pg_stat_wal;wal_fpi counts full-page images. PostgreSQL may write a complete page image after a checkpoint so recovery can repair a torn page safely.
> callout note > Statistics are cumulative since the last reset. Record the current values, wait through a representative workload, and compare the difference instead of interpreting one snapshot in isolation.
A practical observation loop
- Record the current WAL LSN and checkpoint statistics.
- Run a representative workload for a known period.
- Record the same values again.
- Compare WAL bytes, full-page images, checkpoint counts, write time, and sync time.
- Investigate requested checkpoints before changing settings.
Exercise
Which observation most directly suggests that WAL generation is reaching the configured limit before the checkpoint timer expires?
Show answer
A rising count of requested checkpoints compared with timed checkpoints suggests that PostgreSQL is reaching the WAL threshold first.