Troubleshooting · Databases
Postgres disk full or WAL growing? 7 causes, ranked
7 causes, most common first · last reviewed
TL;DR
When pg_wal grows without stopping, Postgres is being told to keep old WAL: usually by a replication slot whose consumer stopped, or by an archive_command that keeps failing. Check pg_replication_slots and pg_stat_archiver before anything else, and never delete files from pg_wal by hand.
On this page
Quick triage: the first five minutes
See which directory is growing: the WAL directory, the table data, temporary files or the logs.
Shelldf -h "$PGDATA" "$PGDATA/pg_wal" du -sh "$PGDATA"/pg_wal "$PGDATA"/base "$PGDATA"/base/pgsql_tmp "$PGDATA"/log 2>/dev/nullIf pg_wal is a separate volume, check it separately: it can fill while the data volume has room. List replication slots and how much WAL each one is holding back.
SQLSELECT slot_name, slot_type, active, wal_status, pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)) AS retained FROM pg_replication_slots ORDER BY pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn) DESC NULLS LAST;An inactive slot retaining gigabytes is the usual culprit. wal_status exists from PostgreSQL 13. Check whether WAL archiving is failing.
SQLSELECT archived_count, last_archived_wal, last_archived_time, failed_count, last_failed_wal, last_failed_time FROM pg_stat_archiver;A recent last_failed_time and a stale last_archived_time mean WAL cannot be recycled.
The causes, ranked
Most common first. The ranking is a judgement from how these failures usually turn out, not a measured statistic.
1. A replication slot is holding WAL for a consumer that stopped
What it looks like
- pg_wal grows steadily, by about the rate your database writes, and never shrinks after checkpoints.
- pg_replication_slots shows a slot with active = false, or a logical slot for a change-data-capture tool that is down or far behind.
- The growth started when a replica, a CDC connector or a migration tool was stopped or removed.
How to confirm it
A replication slot guarantees that WAL is kept until its consumer confirms it has received it. If the consumer disappears without the slot being dropped, Postgres keeps every WAL segment since the slot's restart_lsn. The retained size from the triage query grows with every write.
Fix it now
If the consumer is gone for good, drop the slot; WAL is released at the next checkpoint. If it still matters, restart the consumer and let it catch up, or drop and recreate it later with a fresh sync. Dropping a slot that a live replica or connector needs breaks that consumer.
SELECT pg_drop_replication_slot('old_cdc_slot');
CHECKPOINT;Prevent it
Set max_slot_wal_keep_size (PostgreSQL 13 and later) so a stuck slot is invalidated instead of filling the disk, and alert on WAL retained per slot. Drop slots as part of decommissioning a replica or connector.
2. WAL archiving is failing
What it looks like
- pg_stat_archiver.failed_count keeps rising; last_archived_time is old.
- The server log repeats messages that the archive command failed, with its exit code.
- Archiving worked until credentials, a bucket policy or a network path changed.
How to confirm it
With archive_mode on, a WAL segment can only be removed or recycled after archive_command succeeds for it. Run the configured archive_command by hand, as the postgres user, against the file named in last_failed_wal to see the real error.
SHOW archive_mode;
SHOW archive_command;Fix it now
Fix the destination: credentials, permissions, free space at the archive target or network access. Once archiving succeeds, Postgres works through the backlog and WAL shrinks over the following checkpoints. Turning archiving off stops the growth but also breaks point-in-time recovery, so do it only knowingly.
Prevent it
Alert on failed_count increasing and on the age of last_archived_time, and test restores from the archive regularly so a broken archive is noticed before you need it.
3. Dead rows pile up because vacuum cannot keep up
What it looks like
- Table and index sizes grow much faster than the number of live rows.
- n_dead_tup is high and last_autovacuum is old on the busiest tables.
- A long-running or idle-in-transaction session has been open for hours.
How to confirm it
Updates and deletes leave dead row versions that vacuum cleans up, but not while an older transaction might still need to see them. Look for the oldest open transactions and for tables with many dead rows.
SELECT relname, n_live_tup, n_dead_tup, last_autovacuum
FROM pg_stat_user_tables
ORDER BY n_dead_tup DESC LIMIT 10;SELECT pid, state, now() - xact_start AS xact_age, left(query, 60) AS query
FROM pg_stat_activity
WHERE xact_start IS NOT NULL
ORDER BY xact_start LIMIT 5;Fix it now
End the transaction that is holding cleanup back, then let autovacuum run or run VACUUM on the worst tables. Plain VACUUM makes space reusable inside the table but rarely returns it to the operating system; VACUUM FULL does, but takes an exclusive lock and needs room for a full copy. Tools such as pg_repack rebuild a table with a much shorter lock.
Prevent it
Set idle_in_transaction_session_timeout, tune autovacuum per table for high-churn tables, and alert on the age of the oldest transaction.
4. A burst of writes produced WAL faster than it can be cleared
What it looks like
- WAL jumps during a bulk load, a mass UPDATE or DELETE, an index rebuild or a data migration.
- Growth stops when the job ends, and space comes back after a few checkpoints.
- Replicas or the archiver fall behind at the same moment, which makes Postgres keep WAL longer.
How to confirm it
Measure the WAL write rate by sampling the current position twice. In PostgreSQL 14 and later, pg_stat_wal also reports total WAL written.
SELECT pg_current_wal_lsn();
-- wait 60 seconds, then:
SELECT pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), '0/3A2B1C00')) AS wal_per_minute;Fix it now
Pause or throttle the job, and run it in smaller batches with commits in between. Make sure replicas and archiving can keep up before resuming.
Prevent it
Schedule bulk changes off-peak, size the WAL volume for your largest planned job plus headroom, and remember that max_wal_size is a soft target that slots and archiving can override.
5. Large sorts and hashes spill to temporary files
What it looks like
- Disk usage spikes and drops with particular queries; base/pgsql_tmp is large while they run.
- Errors about no space left on device when writing to a pgsql_tmp file.
- temp_bytes in pg_stat_database climbs quickly.
How to confirm it
Find which database and which queries write temporary files. Logging every temporary file above a size shows the exact statements.
SELECT datname, temp_files, pg_size_pretty(temp_bytes) AS temp
FROM pg_stat_database ORDER BY temp_bytes DESC;
ALTER SYSTEM SET log_temp_files = '100MB';
SELECT pg_reload_conf();Fix it now
Cancel the runaway query, then fix it: an index so it sorts less, a smaller result, or more work_mem for that session only. Set temp_file_limit to cap how much one session can write.
Prevent it
Review query plans for reports and batch jobs before they run against production, and keep temp_file_limit set.
6. Server logs are filling the same disk
What it looks like
- The log directory is gigabytes in size, growing by the minute.
- log_statement is 'all' or log_min_duration_statement is 0, often left on after debugging.
- Errors repeat in a tight loop, each one logged.
How to confirm it
Check the size of the log directory and the logging settings.
SELECT sum(size) AS bytes FROM pg_ls_logdir();
SHOW log_statement;
SHOW log_min_duration_statement;Fix it now
Turn the verbose setting back down, reload the configuration, and remove or compress old log files. Unlike WAL, old log files are safe to delete.
Prevent it
Rotate and expire logs, ship them off the database host, and keep them on a different volume from data and WAL.
7. The data really grew, or the WAL volume was always too small
What it looks like
- No slot, archive or bloat problem; the largest tables are simply larger.
- WAL lives on a separate fixed-size volume that cannot be extended in place.
- Disk usage has trended up for months.
How to confirm it
List the largest tables including indexes and TOAST, and compare with the trend.
SELECT relname, pg_size_pretty(pg_total_relation_size(relid)) AS total
FROM pg_stat_user_tables
ORDER BY pg_total_relation_size(relid) DESC LIMIT 10;Fix it now
Add capacity, archive or delete data that is no longer needed, or partition large tables so old partitions can be dropped instantly.
Prevent it
Forecast disk use from the trend and alert on days of headroom left, not only percentage used.
See it happen in a real outage
90-second puzzles built from public postmortems. Diagnose it first; the company is revealed after you answer.
Related concepts
FAQ
Can I delete files in pg_wal to free space?
No. Postgres still needs them for crash recovery, replicas or archiving, and removing them by hand can make the database unrecoverable. Remove the reason they are kept: drop an abandoned slot or fix archiving, then let checkpoints recycle them.
Why is my pg_wal directory so large?
Something is holding WAL: a replication slot whose consumer is behind or gone, a failing archive_command, or a write burst larger than max_wal_size. pg_replication_slots and pg_stat_archiver show the first two.
What happens when Postgres runs out of disk space?
If it cannot write WAL it stops with a PANIC, and it may not restart until space is freed. If only the data volume is full, statements that need space fail. Freeing space safely is the first step either way.
Does VACUUM free disk space?
Plain VACUUM marks dead space reusable inside the table, so the table stops growing, but usually does not shrink the file. VACUUM FULL or a tool like pg_repack rewrites the table and returns space to the operating system.
Related troubleshooting guides
- DatabasesPostgres replica lag growing? 6 causes, ranked
- DatabasesDatabase connection pool exhausted? 7 causes, ranked
Reference documentation: PostgreSQL documentation: WAL configuration ↗ · PostgreSQL documentation: replication slots and max_slot_wal_keep_size ↗ · PostgreSQL documentation: continuous archiving ↗ · PostgreSQL documentation: routine vacuuming ↗ · Report a problem
Commands and settings are checked against the documentation linked above. Defaults change between versions; check the documentation for the version you run.