Sub topic 4
Measuring Bloat with Statistics and `pgstattuple`
Assume you know basic SQL, transactions, indexes, and how to use psql. This chapter focuses on measuring PostgreSQL table bloat. It does not cover the cleanup commands in detail.
Bloat is unused or obsolete space left inside a table or index. PostgreSQL uses multiversion concurrency control (MVCC), so an UPDATE usually creates a new row version instead of changing the old version in place. A DELETE also leaves the old row version until no transaction can see it. These old versions are called dead tuples.
flowchart TD
A[UPDATE or DELETE] --> B[Old row version remains]
B --> C[Dead tuple]
C --> D[VACUUM can mark space reusable]
D --> E[New rows may reuse the space]
C --> F[Growing table and slower scans]caption: How row changes create measurable dead space alt: An update or delete leaves an old row version, which becomes a dead tuple; vacuum can make its space reusable, while uncleaned dead tuples make the table grow.
Start with `pg_stat_user_tables`
The view pg_stat_user_tables contains approximate activity statistics for user tables. The columns n_live_tup and n_dead_tup estimate the number of live and dead tuples. The timestamp columns show when manual or automatic vacuum last ran.
SELECT 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_vacuum,
last_autovacuum
FROM pg_stat_user_tables
ORDER BY n_dead_tup DESC
LIMIT 10;The calculated dead_pct is a useful screening value, not an exact bloat percentage. These statistics are estimates and can be stale. A table can also have substantial free space even when it has few currently dead tuples. For example, earlier vacuum operations may have cleaned tuples but left reusable pages inside the table.
| Signal | Meaning | Limitation |
|---|---|---|
| `n_dead_tup` | Estimated dead row versions | Not exact |
| `dead_pct` | Estimated dead tuples as a share of observed tuples | Not the same as disk bloat |
| `last_autovacuum` | Last recorded autovacuum | A timestamp alone does not prove cleanup succeeded |
Get exact figures with `pgstattuple`
The pgstattuple extension scans a table and reports physical contents. It distinguishes live tuples, dead tuples, and free space. This is more accurate than the statistics view, but the scan reads the table and can be expensive on a large relation.
Install the extension once in the database, then inspect a table:
CREATE EXTENSION IF NOT EXISTS pgstattuple;
SELECT table_len,
tuple_count,
dead_tuple_count,
dead_tuple_percent,
free_space,
free_percent
FROM pgstattuple('public.orders');table_len is the table's physical size in bytes. dead_tuple_percent is the percentage of table bytes occupied by dead tuples. free_percent is space that is currently unused within the table. Neither value describes index bloat. Inspect important indexes separately with the extension's index functions or an index-specific maintenance check.
A large free_percent does not always require an immediate rewrite. VACUUM can often make that space available for future inserts. A rewrite such as VACUUM FULL is more appropriate when you must return space to the operating system and can accept a strong table lock.
Use both measurements
Use the statistics view for frequent, low-cost monitoring. Run pgstattuple when an individual table needs confirmation or when deciding whether a rewrite is worthwhile.
A practical workflow is:
- Find tables with rising
n_dead_tupor unusual physical growth. - Check
last_autovacuumand recent write activity. - Run
pgstattupleon selected tables. - Inspect indexes separately if queries show index growth or poor performance.
- Investigate cleanup blockers before choosing a rewrite.
Exercise
A table has dead_tuple_percent = 3 and free_percent = 35. Does this prove that VACUUM FULL is required?
Show answer
No. The table has substantial reusable free space, but a rewrite is not automatically required. Check whether normal VACUUM can reuse it, whether the table is still growing, and whether returning disk space to the operating system is necessary.