The PostgreSQL administration field guideField notes · Runbooks · Free certification

Section 3 of 3 · 7 minutes

Find why old WAL is being retained

Your goal

What you will be able to do

Check replication slots and archiving before responding to WAL disk growth.

Why this matters at work

The practical reason

Deleting files from pg_wal can make the server unrecoverable and break replicas or backups.

Learn

The idea in plain English

PostgreSQL keeps WAL that is still required. A replication slot may retain it for a disconnected consumer. Failed archiving may also prevent safe recycling.

Find the owner and purpose of the slot or archive target. Fix the consumer or archive path, then confirm retained bytes fall. Never remove WAL files directly from the data directory.

Remember these points

  • A slot retains WAL to protect its consumer.
  • Archive failures can also stop safe WAL recycling.
  • Never delete files directly from pg_wal.

See it in SQL

Measure WAL retained by replication slots

Identify inactive or lagging slots that hold old WAL.

SELECT
  slot_name,
  slot_type,
  active,
  wal_status,
  pg_size_pretty(
    pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)
  ) AS retained_wal
FROM pg_replication_slots
ORDER BY pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn) DESC NULLS LAST;

`restart_lsn` is the oldest WAL position the slot may still need. A large gap on an inactive slot needs owner review.

Do not drop a slot until its consumer, recovery need, and resynchronisation plan are understood.

What you should see

Replication slots ordered by retained WAL, with active state and WAL status.

Useful words

Important terms

Replication slot
A server record that retains WAL or row changes until a named consumer receives them.

Quick check · Not graded

Check your understanding

What should you do first when pg_wal grows unexpectedly?

Choose one answer

Your progress is saved to your signed-in account.