Troubleshooting guide
PostgreSQL table bloat: how to find and remove it
Postgres keeps old row versions until vacuum reclaims them. If it cannot keep up, tables grow larger than their data and queries slow down.
3 min read10 sectionsUpdated
On this page
-- Tables with the most dead rows
SELECT relname, n_live_tup, n_dead_tup, last_autovacuum,
pg_size_pretty(pg_total_relation_size(relid)) AS total
FROM pg_stat_user_tables ORDER BY n_dead_tup DESC LIMIT 10;
-- Exact measurement (reads the table; use off-peak)
CREATE EXTENSION IF NOT EXISTS pgstattuple;
SELECT dead_tuple_percent, free_percent FROM pgstattuple('orders');Fixes
Make autovacuum more eager on busy tables:
ALTER TABLE orders SET (autovacuum_vacuum_scale_factor = 0.02);
Raise autovacuum_vacuum_cost_limit if vacuum cannot keep up. Find and end anything holding the transaction horizon. To reclaim disk, use pg_repack, which rebuilds online. VACUUM FULL takes an exclusive lock, so use it only during maintenance windows. Rebuild bloated indexes with REINDEX CONCURRENTLY.
Why the defaults fall short
By default autovacuum triggers when 20 percent of a table is dead. On a table of 500 million rows, that is 100 million dead rows before cleanup begins, by which time queries have already degraded. A small scale factor, or a fixed autovacuum_vacuum_threshold, keeps work small and frequent. Also consider design: a column that changes constantly can move into a narrower table, and a suitable fillfactor allows HOT updates that avoid index writes altogether. Partitioning lets you remove old data by dropping a partition, producing no dead rows at all.
Catch it automatically
Track dead tuple ratio and time since last autovacuum for your biggest tables, using PostgreSQL monitoring. Alert on transaction ID age as well: when that gets near its wraparound limit, Postgres will eventually refuse new writes.
Does VACUUM shrink the file?
Usually not. It makes space reusable. Only trailing empty pages are returned to the operating system.
Is VACUUM FULL safe?
It is safe for data, but it blocks reads and writes until complete and needs spare disk.
How much bloat is normal?
Twenty to thirty percent is common and not a concern on many tables.
Related reading
- PostgreSQL high CPU usage: how to find the cause: Diagnose PostgreSQL high CPU: use pg_stat_statements to rank queries by total time, spot missing indexes and stale statistics, and read EXPLAIN properly.
- PostgreSQL: deadlock detected: Understand PostgreSQL "ERROR: deadlock detected": how two transactions block each other, reading the log details.
- PostgreSQL replication lag: causes and how to fix it: Diagnose PostgreSQL replication lag: use pg_stat_replication to tell send, write and replay delays apart, and fix slow replicas, long queries and WAL pile-ups.
- PostgreSQL "too many clients already": how to fix it: Fix PostgreSQL FATAL: sorry, too many clients already. See who holds connections, stop leaks, add PgBouncer and size max_connections the right way.
- PostgreSQL connection refused: how to fix it: Fix PostgreSQL connection refused on port 5432: server not running, listen_addresses, pg_hba.conf, Docker hostnames and firewalls, with commands for every case.
- What is full-stack monitoring?: Full-stack monitoring explained: covering browser, network, application, database and infrastructure, linked so one alert leads to the cause.
Watch your own site and services with VitalRix
Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.