Sub topic 11
Dead tuples, VACUUM and autovacuum
This chapter assumes you know basic SQL and the transaction commands BEGIN, COMMIT, and ROLLBACK. You should also understand that PostgreSQL uses MVCC, meaning that an update creates a new row version instead of changing the old version in place.
How dead tuples appear
An old row version becomes a dead tuple when no future snapshot can see it. It remains physically present until PostgreSQL cleans it up.
An UPDATE creates a new tuple and marks the previous tuple as replaced. A DELETE marks the existing tuple as no longer visible. Neither operation immediately removes bytes from the table. Keeping old versions lets a transaction that started earlier finish reading a consistent view.
CREATE TABLE vacuum_demo (
id integer PRIMARY KEY,
value integer NOT NULL
);
INSERT INTO vacuum_demo
SELECT generate_series(1, 10000), 0;
UPDATE vacuum_demo
SET value = value + 1;
DELETE FROM vacuum_demo
WHERE id <= 1000;
SELECT relname, n_live_tup, n_dead_tup, last_autovacuum
FROM pg_stat_user_tables
WHERE relname = 'vacuum_demo';The n_dead_tup value is an estimate, not an exact count. Repeated updates and deletes increase it. Dead tuples can make table scans slower because PostgreSQL must inspect them, and they can cause table and index bloat.
What VACUUM does
VACUUM checks old tuple versions and marks their storage space as reusable after the tuples are no longer needed by any active snapshot. Ordinary vacuum usually does not return the table file to the operating system. It prepares space for later inserts and updates.
VACUUM (VERBOSE, ANALYZE) vacuum_demo;ANALYZE updates statistics used by the query planner. It is useful after a large data change, but it is a separate task from removing dead tuples.
VACUUM FULL rewrites the table and can return unused space to the operating system. It also takes a strong table lock and needs additional disk space during the rewrite. Use it deliberately, usually during a maintenance window.
How autovacuum decides to run
The autovacuum launcher starts workers when table activity passes configured thresholds. A common approximate rule is:
vacuum threshold + vacuum scale factor × estimated table rows
The default scale factor can delay vacuum on a very large, frequently changing table. Per-table settings can make cleanup more responsive.
ALTER TABLE vacuum_demo SET (
autovacuum_vacuum_scale_factor = 0.02,
autovacuum_vacuum_threshold = 500
);These settings affect future autovacuum decisions. They do not remove existing dead tuples immediately. You can run VACUUM yourself when prompt cleanup is needed.
Long transactions and the cleanup horizon
Vacuum cannot remove a tuple that an old transaction might still need. A session that begins a transaction and then remains idle can hold back this visibility horizon.
SELECT pid, state, xact_start, backend_xmin
FROM pg_stat_activity
WHERE backend_xmin IS NOT NULL
ORDER BY age(backend_xmin) DESC;Investigate old transactions before repeatedly increasing autovacuum activity. Closing abandoned transactions often allows the next vacuum to remove the accumulated dead tuples.
Exercise
Why might VACUUM fail to reduce n_dead_tup when a session has been idle in a transaction for a long time?
Show answer
The old transaction may still have a snapshot that could see those tuple versions, so vacuum must preserve them until the transaction ends.