Sub topic 14
Monitoring PostgreSQL Bloat and Building a Maintenance Routine
Assume you know basic SQL, transactions, indexes, and how to use psql. This chapter combines those ideas into a repeatable maintenance routine. The goal is not to remove every unused byte. The goal is to keep query performance, disk usage, and maintenance work within acceptable limits.
What to monitor
PostgreSQL exposes useful estimates in pg_stat_user_tables. The most important values are:
n_live_tup: estimated rows that are currently visible.n_dead_tup: estimated row versions that no longer need to be read.last_autovacuum: the last automatic vacuum time.last_autoanalyze: the last automatic statistics update.vacuum_countandautovacuum_count: how often manual and automatic vacuum have run.
A high dead-row percentage is a warning, not an automatic repair decision. A table that receives frequent updates may have many dead tuples between vacuum runs. A table that is rarely queried may tolerate more unused space.
SELECT schemaname,
relname,
n_live_tup,
n_dead_tup,
round(
100.0 * n_dead_tup /
nullif(n_live_tup + n_dead_tup, 0),
1
) AS dead_pct,
last_autovacuum,
last_autoanalyze
FROM pg_stat_user_tables
ORDER BY n_dead_tup DESC
LIMIT 20;pg_stat_user_tables contains estimates collected during normal operation. For an exact measurement, install and use pgstattuple. It reads the table, so schedule it carefully on large, busy relations.
CREATE EXTENSION IF NOT EXISTS pgstattuple;
SELECT table_len,
tuple_count,
dead_tuple_count,
round(dead_tuple_percent::numeric, 1) AS dead_pct,
free_space,
round(free_percent::numeric, 1) AS free_pct
FROM pgstattuple('public.orders');The result separates dead tuples from free space. Dead tuples may become reusable after VACUUM. Free space is already available for later inserts or updates. Neither value alone proves that a table needs rewriting.
Build a maintenance workflow
Use this sequence when a table appears bloated:
A useful routine has three levels:
- Daily or hourly: review dead-tuple estimates, table size, index size, and autovacuum activity for high-write tables.
- During an incident: run
VACUUM (VERBOSE, ANALYZE)and inspect its output. Check whether old transactions, prepared transactions, or replication slots prevent cleanup. - During a planned maintenance window: measure exact bloat with
pgstattuple. Rebuild only when the storage or performance benefit justifies the lock, I/O, and temporary disk space.
| Signal | First response | Escalate when |
|---|---|---|
| Dead tuples rising | Check autovacuum and run `VACUUM` if needed | Cleanup remains blocked |
| Table size rising | Compare live rows and free space | Space must return to the operating system |
| Index size rising | Inspect individual indexes | An index is much larger than its workload requires |
| Old `last_autovacuum` | Review table-specific settings | Autovacuum cannot keep up |
Choose the least disruptive repair
VACUUM normally makes dead-row space reusable inside the table and updates planner statistics when ANALYZE is included. It usually does not shrink the table file for the operating system.
VACUUM FULL rewrites the table and can return space to the operating system, but it takes a strong table lock. Use it only with an approved outage or maintenance window.
REINDEX INDEX CONCURRENTLY rebuilds one index while allowing normal table writes. pg_repack can rebuild a table and its indexes with shorter exclusive locks, but it needs extra disk space and installation privileges. If your PostgreSQL version provides REPACK, verify its locking behavior and syntax in that version's documentation before using it.
Exercise
A table has a high dead-tuple percentage, but its size is stable and autovacuum ran recently. Should you immediately run VACUUM FULL?
Show answer
No. First check whether the space is reusable and whether queries are affected. Run ordinary VACUUM if cleanup is needed. Reserve VACUUM FULL or online repacking for cases where the table must be rewritten and the operational cost is justified.