VACUUM
PostgreSQL's UPDATE and DELETE do not remove a row in place. They only mark the previous version as ended. When every transaction that can see this previous version (a dead tuple) has finished, VACUUM reclaims that space so new rows in the same table can reuse it. The cleanup that other DBMSs do in the undo area, PostgreSQL does on the table itself with VACUUM.
Key Specifications
| Item | Value |
|---|---|
| Automatic run | autovacuum, on by default. autovacuum_max_workers defaults to 3 |
| Run threshold | Dead rows > autovacuum_vacuum_threshold (50) + autovacuum_vacuum_scale_factor (0.2) × row count |
| Statistics refresh threshold | Changed rows > autovacuum_analyze_threshold (50) + autovacuum_analyze_scale_factor (0.1) × row count |
| Insert-only tables | Since 13, it also runs based on the number of inserts (autovacuum_vacuum_insert_threshold) |
| Locking | A regular VACUUM runs alongside reads and writes (SHARE UPDATE EXCLUSIVE) |
VACUUM FULL | Rewrites the table to shrink the file. It takes an ACCESS EXCLUSIVE lock, which blocks even reads meanwhile |
| Transaction ID | 32 bits. Freezing is needed about every 2.1 billion transactions, before wraparound |
| Forced freeze threshold | autovacuum_freeze_max_age, 200 million by default |
How It Works
Figure. Until a dead row is reclaimed
flowchart LR
U["UPDATE: add a new version<br/>record xmax on the old version"] --> D["Old version = dead row"]
D --> Q{"Does any transaction that<br/>can see this version remain?"}
Q -- "Yes" --> K["Cannot reclaim<br/>the table grows"]
Q -- "No" --> V["VACUUM: remove index entries,<br/>mark the row space as free"]
V --> F["Record in the free space map (FSM)<br/>new rows reuse it"]
Diagram explanation: UPDATE adds a new version and leaves the number of the deleting transaction (xmax) on the old version. If a snapshot that can still see that version exists, the row stays in the Cannot reclaim state. After they are all gone, VACUUM first removes the index entries that point to that row and then frees the row space. The freed space is recorded in the free space map (FSM), and later inserts and updates reuse it. The file size does not shrink unless the end of the file is entirely free.
Long transactions. A row that died after the oldest active snapshot (the xmin horizon) cannot be reclaimed. A transaction open for hours, an idle in transaction session, an old replication slot, or a long query on a standby with hot_standby_feedback on blocks cleanup in every table, and the tables bloat.
Freeze. The transaction ID is 32 bits, so after it wraps around, old rows would look like they belong to the future. VACUUM prevents this by marking sufficiently old rows as "visible to all transactions" (freeze). When the age of a table's oldest ID (relfrozenxid) exceeds autovacuum_freeze_max_age, autovacuum runs by force even if it has been turned off, and if the limit gets close, the server refuses new transactions.
Visibility Map. When all rows in a page are visible to everyone, the page is marked, so the next VACUUM skips it and index-only scans do not read the heap.
Configuration and Inspection
-- Tables with many dead rows and their last cleanup time
SELECT relname, n_live_tup, n_dead_tup, last_vacuum, last_autovacuum
FROM pg_stat_user_tables ORDER BY n_dead_tup DESC LIMIT 10;
-- Old transactions that block cleanup
SELECT pid, state, xact_start, backend_xmin FROM pg_stat_activity
WHERE backend_xmin IS NOT NULL ORDER BY age(backend_xmin) DESC LIMIT 5;
-- Transaction ID age (a forced freeze happens near 200 million)
SELECT datname, age(datfrozenxid) FROM pg_database ORDER BY 2 DESC;
VACUUM (VERBOSE, ANALYZE) orders;
ALTER TABLE orders SET (autovacuum_vacuum_scale_factor = 0.02); -- run more often for big tables
Check an ongoing cleanup in pg_stat_progress_vacuum. To shrink a file without locking, use the pg_repack extension.
Comparison with Other DBMSs
| PostgreSQL | InnoDB | Oracle | SQL Server | |
|---|---|---|---|---|
| Location of old versions | Inside the table | Undo log | Undo tablespace | tempdb version store |
| Cleanup | VACUUM, autovacuum | Purge thread | Reused after the retention period | Background cleanup |
| Effect of long transactions | Table bloat | Undo growth (history list) | ORA-01555 | tempdb growth |
In the Articles
- 5.6 How DBMSs Differ and a One-Line Summary
- 7.7 Where Old Versions Are Kept
- 7.9 Cleaning Up Old Versions: Vacuum, Purge, and Long Transactions
NOTE The autovacuum defaults may differ by version.