VACUUM finds row versions no snapshot can see anymore, removes the index entries that point at them, and marks their slots free for reuse. it also sets the visibility map and records free space. what it doesn't do is give space back: the file stays the size it was, except for empty pages at the very end. VACUUM FULL writes a brand new, compact file, and holds an ACCESS EXCLUSIVE lock on the table the whole time.
you know how nothing in postgres is ever overwritten? every UPDATE and DELETE leaves a dead version behind. something has to come round and collect them, or every table only ever grows, and every scan wades through more and more dead rows. that's VACUUM.
think of a car park. VACUUM is the attendant walking the levels, towing abandoned cars and painting their bays as free (the free space map), and putting a "nothing to do here" sticker on each clean level (the visibility map). the car park is still the same size afterwards. new cars fill the free bays. VACUUM FULL is building a new, smaller car park next door, driving every car across, and demolishing the old one, with the gates shut while it happens.
here's a six-page table on a real postgres 18.6 server: two rows in every three deleted, then VACUUM, then VACUUM FULL.
204 rows, 34 to a page, six pages, vacuumed once: every page is all-visible.
live tuplesdead tuplesfree spacepage header, VM bits (● all-visible, ■ all-frozen)
step 1/4
real output, a postgres 18.6 server · vacuum-sweep
step through. after the DELETE every page is still full, two thirds dead. VACUUM turns the dead tuples into free space and sets every page's all-visible bit (●): still six pages. VACUUM FULL writes the 68 live rows into two new pages, and leaves the visibility map empty.
one plain VACUUM of one table. hover a stage. the indexes are cleaned before the heap slots are freed, so no index entry ever points at a slot that's been reused.
look at page 0, before and after the VACUUM. after the DELETE it held 34 tuples, every one still there. after the VACUUM:
the page =
byte 0 headerline pointerst · block 0 · 8 KB · a dot is 16 Btuples, byte 8,192
real postgres 18.6 output · a native server · traces/vacuum-sweep
delete two rows in every three. every page still holds all its tuples, two thirds of them dead, and loses its all-visible bit. the index still has 204 entries.
switch moments and step through the slots. the deleted tuples' slots are unused afterwards, and the live tuples were slid together toward the end of the page, so the free space is one gap again.
11 live tuples and 22 unused slots, and free space went from 144 to 5484 bytes. the page has 33 line pointers now, not 34: slot 34 was the last one and unused, so it was cut off the array. unused slots in the middle can't be removed (that would change other tuples' ctids), so they wait for the next insert on this page.
the index lost its dead entries in the same VACUUM:
T1DELETEFROMtWHEREid%3<>0
after the DELETE: 204 entries, still one per tuple, dead or not.
step 1/2
real output, a postgres 18.6 server · vacuum-sweep
t_pkey, one leaf page, before and after. two thirds of the keys go: the ones whose heap tuple was dead.
VACUUM (VERBOSE) says all of it in its own words. this is the server log from that step:
INFO: vacuuming "postgres.public.t"
INFO: finished vacuuming "postgres.public.t": index scans: 1
the same table, built in a block. nothing reads it before the VACUUM, so the browser engine's eager pruning never gets a chance:
sqldb=lab · session vac
CREATE TABLE t (id int PRIMARY KEY, pad text NOT NULL);INSERT INTO t SELECT g, repeat('x', 200) FROM generate_series(1, 204) AS g ORDER BY g;DELETE FROM t WHERE id % 3 <> 0;SELECT pg_relation_size('t') AS heap_bytes, pg_relation_filenode('t') AS filenode, (SELECT sum(live_items) FROM bt_page_stats('t_pkey', 1)) AS index_entries, (SELECT count(*) FILTER (WHERE all_visible) FROM pg_visibility_map('t')) AS all_visible_pages;
output, from postgres 18.6
#1 · CREATE TABLE
#2 · INSERT 0 204
#3 · DELETE 136
#4 · SELECT 1
heap_bytesint8
filenodeoid
index_entriesint8
all_visible_pagesint8
49152
24576
204
0
(1 row)
sqldb=lab · session vac
continues session vac (1 earlier block run first)
VACUUM t;SELECT pg_relation_size('t') AS heap_bytes, pg_relation_filenode('t') AS filenode, (SELECT sum(live_items) FROM bt_page_stats('t_pkey', 1)) AS index_entries, (SELECT count(*) FILTER (WHERE all_visible) FROM pg_visibility_map('t')) AS all_visible_pages, (SELECT sum(avail) FROM pg_freespace('t')) AS free_bytes;
output, from postgres 18.6
#1 · VACUUM
#2 · SELECT 1
heap_bytesint8
filenodeoid
index_entriesint8
all_visible_pagesint8
free_bytesint8
49152
24576
68
6
32320
(1 row)
the same 49,152 bytes, the same file. a third of the index entries, every page all-visible, and the free space map now knows about the room on every page. new rows go into those bays, so the table won't grow until they're used up.
predict the output
after that VACUUM, you insert 100 new rows into t. how big is the file?
sql
INSERT INTO t SELECT g, repeat('y', 200) FROM generate_series(1001, 1100) AS g;SELECT pg_relation_size('t');
sqldb=lab · session vac
continues session vac (2 earlier blocks run first)
INSERT INTO t SELECT g, repeat('y', 200) FROM generate_series(1001, 1100) AS g;SELECT pg_relation_size('t') AS heap_bytes;
to actually shrink the file you have to move the rows, and moving rows changes their ctids, which every index points at. so VACUUM FULL doesn't move anything: it copies every live row into a new file, builds every index again from scratch, and switches the table over to the new files when it commits. the old files are deleted.
sqldb=lab · session vac
continues session vac (3 earlier blocks run first)
VACUUM FULL t;SELECT pg_relation_size('t') AS heap_bytes, pg_relation_filenode('t') AS filenode, (SELECT count(*) FILTER (WHERE all_visible) FROM pg_visibility_map('t')) AS all_visible_pages;
output, from postgres 18.6
#1 · VACUUM
#2 · SELECT 1
heap_bytesint8
filenodeoid
all_visible_pagesint8
40960
24585
0
(1 row)
a new file number (the relfilenode, see files and forks), and a smaller file. the price is the lock. while it copies, nobody else may read or write the table, so it takes ACCESS EXCLUSIVE, the strongest table lock. a plain VACUUM takes SHARE UPDATE EXCLUSIVE, which only conflicts with other vacuums and schema changes. captured on a real server:
T1
·
T2
snapshot 760:760:
T3
·
1
BEGIN;
SELECT count(*) FROM t→ 68
2
VACUUM t
3
obsSELECT pg_relation_filenode('t') AS filenode, pg_relation_size('t') AS bytes→ 1 row
4
VACUUM FULL twaits for T2: AccessExclusiveLock on t
5
waiting
SELECT count(*) FROM twaits for T1: AccessShareLock on t
6
woke:VACUUM FULL t
COMMIT
woke:SELECT count(*) FROM t→ 68
7
obsSELECT pg_relation_filenode('t') AS filenode, pg_relation_size('t') AS bytes→ 1 row
lock manager
T2AccessShareLockt
T2AccessShareLockt_pkey
T2ExclusiveLock5/4
T2 opens a transaction and reads t. it holds an ACCESS SHARE lock on the table until it ends.
step 1/7
real output, a postgres 18.6 server · vacuum-full-lock
step through. plain VACUUM runs straight past T2's open read. VACUUM FULL waits for it, and T3's plain SELECT queues behind VACUUM FULL. the locks panel shows who holds what, granted or waiting.
the file went from 49152 bytes under file number 16466 to 16384 bytes under 16475. on a big table the copy takes minutes or hours, and for all of it the table is closed, and so is everyone who queues behind the lock: the lock queue chapter is about exactly that.
bloat is space a table or index takes beyond what its live rows need: dead tuples not yet vacuumed, plus free space vacuum created that new rows haven't filled. some is normal (a table with fillfactor 90 is "bloated" by design). a lot means vacuum is falling behind, or something is stopping it. indexes bloat too, and VACUUM doesn't shrink them either: emptied index pages are recycled within the index, and REINDEX CONCURRENTLY rebuilds one without blocking writes.
in the browser, VACUUM only runs when you type it. on a real server autovacuum does it in the background, triggered by how many rows changed. and VACUUM has one more job, the one that can't be skipped: freezing old tuples before the transaction counter wraps around.
keylines
VACUUM: prune, clean the indexes, free the slots, update the free space and visibility maps.
the file doesn't shrink, except for empty pages at the end. the free space gets reused.
VACUUM runs next to reads and writes. VACUUM FULL locks everyone out.
VACUUM FULL copies live rows into a new file with a new relfilenode and rebuilds the indexes.
bloat is dead or unused space. the fix is usually vacuuming more, not VACUUM FULL.