// SQL snippet

Check WAL volume generated in PostgreSQL

How much WAL is the instance writing, and how much disk is the WAL directory holding? Two queries, two answers.

Main statement
PostgreSQL
Também cobre
SQL Server

The task

Every change PostgreSQL makes is written to WAL first, so WAL volume drives checkpoint frequency, replication traffic, archive storage and point-in-time-recovery restore time. pg_stat_wal (PostgreSQL 14 and later) counts WAL generated since its last reset. pg_ls_waldir() lists the files currently in pg_wal. The first measures the write rate; the second is the disk the directory holds, which grows when something prevents old segments from being removed.

The snippet

Written for PostgreSQL. Versions for SQL Server are under Variations.

PostgreSQL
SELECT wal_records, wal_fpi,
       pg_size_pretty(wal_bytes) AS wal_generated,
       wal_bytes, wal_buffers_full,
       stats_reset
FROM pg_stat_wal;

Como funciona

wal_bytes is the WAL generated since stats_reset; sample it twice and divide by the interval for a rate in bytes per second. wal_fpi counts full-page images, written for the first change to each page after a checkpoint; a large share of the total points at checkpoints that are too frequent (see the checkpoint-warnings recipe). wal_buffers_full counts times WAL had to be written out because wal_buffers was full; a steadily rising value on a busy server suggests raising wal_buffers. pg_ls_waldir() needs superuser or the pg_monitor role. In normal operation pg_wal stays at or somewhat under max_wal_size; much more than that means segments are being retained, the case the first FAQ covers.

Variations

Disk currently held by the WAL directory

SELECT pg_size_pretty(SUM(size)) AS pg_wal_directory_size
FROM pg_ls_waldir();

SQL Server: transaction log usage

SQL Server
SELECT RTRIM(instance_name) AS database_name,
       CAST(cntr_value / 1024.0 AS DECIMAL(12,1)) AS log_used_mb
FROM sys.dm_os_performance_counters
WHERE object_name LIKE '%:Databases%'
  AND counter_name = 'Log File(s) Used Size (KB)'
  AND instance_name NOT IN ('_Total', 'mssqlsystemresource')
ORDER BY cntr_value DESC;

Questions that come up

pg_wal holds 50 GB but pg_stat_wal says little was written. Why?

pg_stat_wal counts what was written; the directory holds whatever has not been removed yet. The usual causes: a replication slot whose consumer is gone or far behind (check active and restart_lsn in pg_replication_slots; max_slot_wal_keep_size caps how much a slot can hold), archiving that keeps failing (failed_count and last_failed_wal in pg_stat_archiver), or a large wal_keep_size. Once the slot advances or is dropped, or archiving succeeds, the next checkpoint removes or recycles the old segments.

How does WAL volume relate to replication lag?

Directly: a standby must receive and replay every byte the primary writes. A burst that doubles the WAL rate needs twice the network bandwidth and replay speed while it lasts, and a sustained rate above what the standby can replay means lag grows without limit. Full-page images inflate the volume, which is one more reason to keep checkpoints spaced out.

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.