Skip to content

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

Key points

  • Updates and deletes leave dead tuples.
  • Autovacuum defaults are often too lazy for large tables.
  • VACUUM FULL locks the table; pg_repack does not.
  • A long transaction stops cleanup altogether.

What bloat is

An update writes a new row version and marks the old one dead. Normal VACUUM makes that space reusable inside the table but does not usually return it to the operating system. Bloat is the accumulated gap between the size of a table and what its live rows need.

Common causes

  • Heavy update traffic on tables with default autovacuum thresholds.
  • Long-running transactions or stale replication slots.
  • Autovacuum throttled by its cost limits.
  • Massive deletes without a follow-up.
  • Index bloat that develops separately from the table.

Diagnose it

Start with dead tuple counts and the last vacuum times.

bash
-- 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.

Frequently asked questions

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.

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.