← Back to SQL, page 24

sql-0467 · Hard senior

The database disk is filling up fast although data volume has not grown much. Where would you look?

Check the write-ahead log directory first: an inactive replication slot or failing WAL archiving makes PostgreSQL keep WAL indefinitely. Then look for table and index bloat from heavy updates, large temporary files from big sorts and hashes, and growing server logs. Find the largest relations with pg_total_relation_size and remove the cause rather than only adding disk.

Full answer

Checklist:

  • Replication slots: SELECT slot_name, active, pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)) FROM pg_replication_slots; An inactive slot from a decommissioned replica or CDC tool retains WAL forever. Drop it if it is no longer needed.
  • WAL archiving: if archive_command keeps failing, WAL segments pile up; check pg_stat_archiver.
  • Bloat: dead tuples in hot tables and indexes, blocked by long transactions.
  • Temporary files: queries spilling sorts and hashes; log_temp_files shows them.
  • Logs: verbose statement logging can produce gigabytes per day.
  • Largest objects: SELECT relname, pg_size_pretty(pg_total_relation_size(oid)) FROM pg_class ORDER BY pg_total_relation_size(oid) DESC LIMIT 10;

Set alerts on disk usage and on slot lag (max_slot_wal_keep_size caps retained WAL in PostgreSQL 13+). Running out of disk can stop the database entirely, so treat a fast-growing disk as urgent. Described here, not run.

Key points

  • Inactive replication slots retain WAL
  • Failing archiving accumulates WAL
  • Bloat and temp files consume space
  • Find the largest relations
  • Cap slot retention and alert early

Common mistakes

  • Deleting WAL files by hand
  • Only adding disk without finding the cause

Follow-ups

  • What does max_slot_wal_keep_size do?
  • How would you safely remove an old slot?

Tips

  • Start with WAL and slots