Full answer
Sample schema and data:
CREATE TABLE audit (id int PRIMARY KEY, payload text);
INSERT INTO audit SELECT g, repeat('p', 200) FROM generate_series(1, 20000) g;SELECT pg_size_pretty(pg_relation_size('audit')) AS before_delete;PostgreSQL returns:
before_delete
---------------
4712 kB
(1 row)DELETE FROM audit WHERE id % 10 <> 0;Result on PostgreSQL 17:
DELETE 18000VACUUM audit;Result:
VACUUMSELECT pg_size_pretty(pg_relation_size('audit')) AS after_vacuum, count(*) AS rows_left FROM audit;Output:
after_vacuum | rows_left
--------------+-----------
4712 kB | 2000
(1 row)VACUUM FULL audit;PostgreSQL returns:
VACUUMSELECT pg_size_pretty(pg_relation_size('audit')) AS after_vacuum_full;Result on PostgreSQL 17:
after_vacuum_full
-------------------
472 kB
(1 row)Deleting 90% of the rows scattered across every page leaves the table at its original size after VACUUM, because each page still holds some live rows and the freed space stays inside the file for reuse. VACUUM FULL rewrites the 2,000 surviving rows into a file about a tenth of the size. Each VACUUM is sent alone because it cannot run inside a transaction block. In production, prefer to avoid the problem: partition tables so old data is dropped as whole partitions, delete in batches with autovacuum keeping up, and use pg_repack when a rewrite is unavoidable on a live table.
CREATE TABLE audit (id int PRIMARY KEY, payload text);
INSERT INTO audit SELECT g, repeat('p', 200) FROM generate_series(1, 20000) g;
SELECT pg_size_pretty(pg_relation_size('audit')) AS before_delete;
DELETE FROM audit WHERE id % 10 <> 0;
VACUUM audit;
SELECT pg_size_pretty(pg_relation_size('audit')) AS after_vacuum, count(*) AS rows_left FROM audit;
VACUUM FULL audit;
SELECT pg_size_pretty(pg_relation_size('audit')) AS after_vacuum_full;Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.
Key points
- DELETE leaves dead rows
- VACUUM makes space reusable, not smaller
- VACUUM FULL rewrites under an exclusive lock
- pg_repack rebuilds online
- Partitioning avoids mass deletes
Common mistakes
- Running VACUUM FULL on a busy production table
- Expecting disk space back after plain VACUUM
Follow-ups
- How does autovacuum decide when to run?
- What is the visibility map used for?
Tips
- Contrast the three sizes