Sub topic 9
VACUUM FULL: reclaiming space with a table rewrite
Assume you know basic SQL, PostgreSQL transactions, indexes, and how to use psql. This chapter focuses on when VACUUM FULL is appropriate and what its table rewrite means for a live system.
Why regular VACUUM may not shrink a table
PostgreSQL uses multiversion concurrency control, or MVCC. An UPDATE creates a new row version, while a DELETE marks the old row version as removable. Regular VACUUM removes tuples that no transaction can see and makes their pages reusable by future writes.
Regular VACUUM usually does not return those pages to the operating system. The table file keeps its size even after many rows are deleted. This is useful when the table will grow again, but it does not help if the table permanently became much smaller.
VACUUM FULL takes a different approach. It rewrites the live rows into a new, compact table and rebuilds its indexes. PostgreSQL then replaces the old table file with the new one. The unused space becomes available to the operating system.
The cost: an exclusive lock and extra disk space
VACUUM FULL requires an ACCESS EXCLUSIVE lock on the table. It waits for conflicting activity and then blocks reads, writes, and other commands until the rewrite finishes. On a large table, that outage can last minutes or hours.
The rewrite also needs additional disk space while both the old and new files exist. Leave room for the new table and its indexes, plus temporary overhead. If the filesystem fills during the operation, the command can fail and the database may still have consumed substantial space.
Run it during a planned maintenance window:
VACUUM (FULL, VERBOSE, ANALYZE) public.orders;VERBOSE prints progress information. ANALYZE refreshes planner statistics after the rewrite. Without ANALYZE, run ANALYZE public.orders; separately if the table's distribution changed.
You can compare relation sizes before and after the operation:
SELECT pg_size_pretty(pg_table_size('public.orders')) AS table_size,
pg_size_pretty(pg_indexes_size('public.orders')) AS index_size,
pg_size_pretty(pg_total_relation_size('public.orders')) AS total_size;Choosing a maintenance method
| Method | Returns space to the operating system | Blocks table access | Typical use |
|---|---|---|---|
| `VACUUM` | Usually no | No, under normal conditions | Routine cleanup and reuse |
| `VACUUM FULL` | Yes | Yes, for the rewrite | Large one-time shrink during downtime |
| `pg_repack` | Yes | Only briefly, when configured correctly | Online table rewrite |
| `REINDEX CONCURRENTLY` | Applies to indexes | Does not block normal table writes | Repairing an oversized index |
Do not schedule VACUUM FULL as ordinary recurring maintenance. Autovacuum should handle normal dead-tuple cleanup. Use VACUUM FULL when reclaiming filesystem space is worth the lock and downtime.
For a production table that cannot tolerate a long exclusive lock, consider pg_repack. It requires the extension and command-line client, and the table needs a primary key or suitable unique index. PostgreSQL versions that provide REPACK may offer another rewrite option, but check that version's locking behavior before using it.
Exercise
A table is 500 GB, but after a purge it contains only 80 GB of live data. Regular VACUUM has completed, but the filesystem is still nearly full. What should you check before choosing VACUUM FULL?
Show answer
Check for enough free disk space to hold the rewritten table and rebuilt indexes, estimate the rewrite duration, and plan for an ACCESS EXCLUSIVE lock that blocks reads and writes. If the application cannot accept that outage, evaluate pg_repack or another online rewrite method.