Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Why does PostgreSQL need VACUUM at all, and what is the difference between VACUUM, VACUUM FULL and autovacuum?

PostgreSQL needs VACUUM because MVCC never overwrites rows: UPDATE and DELETE leave dead row versions behind, and VACUUM marks their space reusable once no snapshot can see them, and also freezes old rows against transaction ID wraparound. Plain VACUUM runs alongside normal reads and writes and usually does not shrink the file, VACUUM FULL rewrites the table into a new file under an exclusive lock, and autovacuum runs VACUUM and ANALYZE automatically when thresholds are crossed.

Background to this question

Engineers coming from other databases are often surprised that PostgreSQL tables keep growing even when the number of rows stays the same. The explanation is MVCC, and the cure is vacuum. Knowing the difference between the three kinds of vacuum prevents two common mistakes: ignoring bloat until queries slow down, and running VACUUM FULL in production and locking a table for an hour. In this answer, we will see why dead rows exist, what plain VACUUM does with them, why VACUUM FULL is so different, how autovacuum decides when to run, and how freezing fits in.

1Why Dead Rows Exist

PostgreSQL uses MVCC, multi-version concurrency control, so readers never block writers. Instead of changing a row in place, an UPDATE writes a new version of the row and marks the old one with the id of the transaction that replaced it. A DELETE only marks the row. The old versions stay in the table because a transaction that started earlier may still need to see them.

Once no running transaction can see an old version any more, it is a dead tuple: it takes space in the table and its index entries still point at it, but nobody will ever read it. Something has to remove it, and that is VACUUM's job.

SQL
CREATE TABLE t (id int PRIMARY KEY, v int);
INSERT INTO t SELECT g, 0 FROM generate_series(1, 100000) g;
UPDATE t SET v = 1; -- 100000 new versions; the old 100000 are now dead
SELECT n_live_tup, n_dead_tup FROM pg_stat_user_tables WHERE relname = 't';
-- 100000 | 100000 (the statistics may lag by a moment)
SELECT pg_size_pretty(pg_relation_size('t')); -- about 7 MB, double the 3.5 MB before

Notice that the table doubled in size while still holding 100,000 rows.

2What Plain VACUUM Does

Plain VACUUM scans the table, finds dead tuples that no snapshot can see, removes their index entries, and marks their space as free in the free space map, so future inserts and updates reuse it. It also updates the visibility map, which records pages where every row is visible to everyone; index-only scans and later vacuums use it to skip pages.

It takes only a SHARE UPDATE EXCLUSIVE lock, which allows normal SELECT, INSERT, UPDATE and DELETE to continue. But there is a catch: it generally does not return space to the operating system. The file keeps its size, with free space inside it to be reused. The only exception is empty pages at the very end of the table, which it can truncate.

Java
VACUUM (VERBOSE) t;
-- INFO: "t": removed 100000 dead row versions in 443 pages
-- (the exact wording varies by version)
SELECT pg_size_pretty(pg_relation_size('t')); -- still about 7 MB, but half of it is now reusable
UPDATE t SET v = 2; -- reuses the freed space; the file barely grows

3What VACUUM FULL Does Differently

VACUUM FULL copies all live rows into a brand new file, rebuilds every index, and then drops the old file. The table really shrinks, back to about 3.5 MB in our example.

The price is high. It takes an ACCESS EXCLUSIVE lock, which blocks every query on the table, even plain SELECTs, for the whole rewrite. On a 200 GB table that can be an hour of outage for every feature that touches it. It also needs free disk space for the new copy of the table and its indexes while the old ones still exist. So VACUUM FULL is a maintenance window tool, not a routine one.

Three kinds of vacuum
AspectVACUUMVACUUM FULLautovacuum
Who runs itus, manuallyus, manuallythe server, automatically
LockSHARE UPDATE EXCLUSIVEACCESS EXCLUSIVEsame as VACUUM, yields to lock waiters
Blocks reads and writesnoyes, for the whole runno
Shrinks the fileonly trailing empty pagesyes, rewrites itonly trailing empty pages
Extra disk needednoa full copy of table and indexesno
Also runs ANALYZEonly with ANALYZE optionnoyes, on its own thresholds

4How Autovacuum Decides When to Run

We should not have to run VACUUM by hand. The autovacuum launcher wakes up regularly (every autovacuum_naptime, 1 minute by default) and starts worker processes for tables that need attention. A table needs vacuuming when its dead tuples exceed autovacuum_vacuum_threshold + autovacuum_vacuum_scale_factor x number of rows. With the defaults, 50 + 0.2 x rows, a table of 1 million rows is vacuumed after about 200,050 dead tuples. A similar rule with its own settings triggers ANALYZE, which refreshes the statistics the planner uses. Since PostgreSQL 13, a threshold on inserts also triggers vacuum on append-only tables.

These settings can be changed per table, which matters because the defaults suit medium tables badly at the extremes: for a 1-billion-row table, 20 percent is 200 million dead rows before anything happens.

SQL
ALTER TABLE big_events SET (autovacuum_vacuum_scale_factor = 0.01); -- vacuum after 1% dead rows

5Freezing: The Other Reason VACUUM Exists

VACUUM has a second job. Every row stores the 32-bit id of the transaction that created it, and these ids wrap around after about 4 billion transactions. To keep old rows from suddenly looking like they come from the future, vacuum freezes them, marking them as visible to everyone so their transaction id no longer matters. If freezing falls too far behind, autovacuum runs an aggressive anti-wraparound vacuum, and as a last resort PostgreSQL stops assigning new transaction ids. So even a table that never has dead rows must be vacuumed from time to time.

6Follow-up questions

What tool would you use to remove bloat without a long exclusive lock?

pg_repack. It builds a new copy of the table and its indexes in the background while recording ongoing changes with a trigger, then applies those changes and swaps the files, holding an exclusive lock only briefly at the start and end. It needs a primary key or a unique index and extra disk space, like VACUUM FULL. pg_squeeze is a similar extension. For indexes alone, REINDEX CONCURRENTLY rebuilds them without blocking writes.

Why not just run VACUUM FULL every night?

Because every run blocks the table completely, and the space it returns is usually needed again within hours as rows are updated. A table with steady churn reaches a stable size when plain vacuum keeps up; shrinking it every night only adds outages.

Does VACUUM help query performance or only disk space?

Both. Fewer dead tuples mean fewer pages to scan, the visibility map lets index-only scans avoid reading the table, and ANALYZE keeps the planner's estimates accurate.

7Conclusion

In this answer, we saw that MVCC leaves dead row versions behind after every UPDATE and DELETE, and VACUUM makes their space reusable once no snapshot can see them. Plain VACUUM runs alongside normal work, updates the visibility and free space maps, and rarely shrinks the file. VACUUM FULL rewrites the table into a new file, takes an ACCESS EXCLUSIVE lock and needs extra disk, so it is an outage for that table. Autovacuum runs vacuum and analyze automatically based on thresholds and scale factors that can be tuned per table, and vacuum also freezes old rows to prevent transaction ID wraparound.

What to say in the interviewAbout 90 seconds aloud

Open with

PostgreSQL needs vacuum because of MVCC. An UPDATE writes a new row version and a DELETE only marks the row, so the old versions stay in the table for transactions that may still see them.

Then, if they want more

Once no snapshot can see them, they are dead tuples, and VACUUM removes them and their index entries and records the space in the free space map so new rows can reuse it. It also updates the visibility map, which helps index-only scans, and it freezes old rows to protect against transaction ID wraparound.

Plain VACUUM takes a light lock, so reads and writes continue, but it usually does not shrink the file, except for truncating empty pages at the end.

VACUUM FULL is different: it rewrites the whole table and its indexes into new files, so the table really shrinks, but it holds an ACCESS EXCLUSIVE lock for the whole time and needs disk space for a full copy, so it is effectively an outage for that table.

Autovacuum is the background system that runs vacuum and analyze automatically when dead tuples pass a threshold plus a scale factor of the table size, 50 plus 20 percent by default, and I would tune those per table for very large or very busy tables. For bloat without a long lock, I would use pg_repack.

About this answer

Last updated
12 November 2026
Versions it names
PostgreSQL 13
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
PostgreSQL documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Why does PostgreSQL need VACUUM at all, and what is the difference between VACUUM...": https://scrivio-interview-library.netlify.app/databases/postgresql/2c454b3509/