PostgreSQL error: No space left on device
ERROR: could not extend file "base/16384/51829": No space left on device
HINT: Check free disk space.
The filesystem hosting the database has no room left. Writes start failing; if the WAL itself cannot be written, the server will PANIC and shut down. One rule before anything else: never manually delete files inside the data directory — especially pg_wal. That path leads from an outage to a restore.
What this error means
Disk-full on a PostgreSQL host has a short list of usual suspects, and the most instructive one is WAL retention: pg_wal is self-cleaning under normal operation, so when it grows unbounded, something is telling PostgreSQL to keep WAL around. The two classic reasons: a failing archive_command (WAL cannot be recycled until archived) and an inactive replication slot — a replica that was decommissioned without dropping its slot keeps WAL pinned forever.
Common causes
- Stale replication slot or broken WAL archiving pinning
pg_wal. - Plain data growth that outran the volume.
- Temporary files from huge sorts/hashes (a single bad query can write hundreds of GB of temp).
- Logs — verbose logging without rotation.
How to diagnose it
# Dove sono finiti i GB:
df -h
du -sh /var/lib/postgresql/*/main/* # base/ pg_wal/ log/ ...
-- Slot che trattengono WAL (guarda active=f e quanto trattengono):
SELECT slot_name, active,
pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)) AS retained_wal
FROM pg_replication_slots;
-- Query che scrivono troppi file temporanei (abilita log_temp_files per la storia):
SELECT datname, temp_files, pg_size_pretty(temp_bytes) FROM pg_stat_database;
How to fix it
- Free space outside the data directory first (old logs, packages, unrelated files) so the server can breathe, then fix the real cause.
- Stale slot →
SELECT pg_drop_replication_slot('old_replica');— WAL gets recycled within a few checkpoints. Broken archiving → fixarchive_commandand watch the backlog drain. - Cap the failure modes for the future:
max_slot_wal_keep_size(PostgreSQL 13+) bounds how much WAL a slot may retain;temp_file_limitbounds runaway query temp usage; log rotation for the logs. - Grow the volume when the cause is honest data growth — and add disk-space alerting with enough margin to act calmly next time.
