Skip to content
IT glossary
DatabasesUpdated December 3, 2026

VACUUM

PostgreSQL uses VACUUM to reclaim space from old row versions so the table can reuse it. Autovacuum is the background process that handles this maintenance automatically.

Also known as: autovacuum

Why deleting rows doesn't immediately free space

Suppose an order changes from pending to paid. PostgreSQL writes a new version of the row rather than immediately overwriting the old one. A transaction reading from an older snapshot may still need to see pending. The same principle applies to a deleted row: it may be gone from new queries but still visible to an older snapshot. This is part of multiversion concurrency control (MVCC).

Once no transaction can still see an obsolete version, VACUUM can reclaim the space it occupies. A table can then reuse that space instead of continually growing as rows are updated or deleted.

What VACUUM does besides cleanup

A regular VACUUM reclaims space in the table and cleans up index entries when needed. It also updates the visibility map, which lets an index-only scan skip checking table pages known to contain only rows visible to all transactions. Another job is freezing old row versions so that the database can safely reuse transaction IDs when their counter wraps around.

Autovacuum is the service that schedules this work, not another name for the SQL command. Its workers select tables based on factors such as the amount of data changed and the age of transaction IDs. They also run ANALYZE separately to refresh the statistics the query planner uses. Reclaiming space and gathering statistics are different tasks.

Example: after a bulk delete

If you've just purged old events and want to reclaim reusable space and refresh planner statistics without waiting for autovacuum, run:

VACUUM (ANALYZE) public.events;

Run this after the delete commits, outside an explicit transaction block. A manual run doesn't override snapshot rules: if another transaction can still see the deleted rows, VACUUM has to leave those versions in place.

Common interview questions

Why is the table file still the same size? Regular VACUUM normally makes space reusable inside the file rather than returning it to the operating system. VACUUM FULL rewrites the table into a smaller file, but needs extra disk space during the rewrite and takes an exclusive lock that blocks even reads. It's not a routine substitute for autovacuum.

Why might a table keep growing even though autovacuum runs? A long-lived snapshot can prevent cleanup, or a write-heavy workload can create obsolete versions faster than workers reclaim them. A useful starting point is pg_stat_user_tables: compare n_dead_tup (an estimate of dead row versions) with last_autovacuum. A recent run tells you maintenance happened, not how much space it was able to reclaim.