Sub topic 5
What VACUUM Does and Does Not Do
This chapter assumes basic SQL, PostgreSQL transactions, indexes, and use of psql or another PostgreSQL client. You should know that a transaction groups database changes and that an index helps PostgreSQL find table rows.
Why VACUUM exists
PostgreSQL uses multiversion concurrency control (MVCC). Instead of changing a row in place, an UPDATE usually writes a new row version and leaves the old version in the table. A DELETE marks a row version as deleted.
An old row version becomes a dead tuple when no transaction can ever need to see it again. Dead tuples occupy table pages and may also occupy index entries. If they remain, queries must read more pages, and the table's files can grow larger than the live data requires.
VACUUM finds dead tuples that are safe to remove and makes their space available for future writes.
What VACUUM does
A regular vacuum performs several related maintenance tasks:
- Removes dead tuple versions that are no longer visible to any active transaction.
- Makes freed space reusable inside the table. It usually does not shrink the table file on disk.
- Removes dead entries from indexes, when index cleanup is enabled and useful.
- Updates the visibility map. This records pages whose tuples are known to be visible to all transactions.
- Freezes old transaction IDs when needed, helping prevent transaction ID wraparound.
- Can update planner statistics when run with
ANALYZE.
Run it manually when investigating a busy table or testing maintenance behavior:
VACUUM (VERBOSE, ANALYZE) public.orders;VERBOSE prints details about scanned pages, removed tuples, index cleanup, and skipped pages. ANALYZE gathers column statistics for the query planner. These are separate jobs, even though one command can request both.
A regular VACUUM normally allows ordinary reads and writes to continue. It still uses locks briefly and consumes I/O and CPU, so schedule unusually large maintenance operations carefully.
What VACUUM does not do
Regular VACUUM is not a general file-shrinking operation. If a table grew to 500 GB and later lost most of its rows, vacuum can make internal pages reusable, but the operating system will usually still see a file near 500 GB.
It also cannot remove a dead tuple when an old snapshot might still need that tuple. A long-running transaction, prepared transaction, or replication-related retention can hold back cleanup.
Use this query to look for sessions with old snapshots:
SELECT pid,
state,
xact_start,
age(backend_xmin) AS xmin_age,
left(query, 60) AS query
FROM pg_stat_activity
WHERE backend_xmin IS NOT NULL
ORDER BY age(backend_xmin) DESC
LIMIT 5;An old transaction is not automatically safe to terminate. First determine whether it belongs to an application job, report, migration, or failed client.
Choosing the right maintenance action
| Goal | Typical action | Main effect |
|---|---|---|
| Make dead-row space reusable | `VACUUM` | Cleans internally with normal table access |
| Refresh planner statistics | `ANALYZE` or `VACUUM (ANALYZE)` | Updates estimates used by query plans |
| Return table space to the operating system | `VACUUM FULL` or online repacking | Rewrites the table, with different locking costs |
| Rebuild a bloated index | `REINDEX CONCURRENTLY` | Creates a new index while allowing table writes |
VACUUM FULL rewrites the table and can reduce its disk footprint, but it requires a strong lock for the rewrite. Prefer regular vacuum for routine maintenance, and reserve rewriting tools for measured space or performance problems.
Exercise
After deleting many rows, why might regular VACUUM reduce future growth without making the table file smaller?
Show answer
VACUUM marks space inside existing pages as reusable. It usually does not rewrite the table, so the operating-system file keeps most of its existing size.