← Back to PostgreSQL, page 4

postgresql-0070 · Hard senior

After deleting most of a table, why does its size not shrink, and when is VACUUM FULL or pg_repack appropriate?

DELETE marks rows dead; plain VACUUM makes their space reusable for future inserts but normally does not return it to the operating system, except for empty pages at the end of the table. VACUUM FULL rewrites the table compactly but holds an ACCESS EXCLUSIVE lock for the whole rewrite, blocking all access. On busy systems, pg_repack rebuilds online with only brief locks.

Full answer

Sample schema and data:

sql
CREATE TABLE audit (id int PRIMARY KEY, payload text);
INSERT INTO audit SELECT g, repeat('p', 200) FROM generate_series(1, 20000) g;
sql
SELECT pg_size_pretty(pg_relation_size('audit')) AS before_delete;

PostgreSQL returns:

text
 before_delete
---------------
 4712 kB
(1 row)
sql
DELETE FROM audit WHERE id % 10 <> 0;

Result on PostgreSQL 17:

text
DELETE 18000
sql
VACUUM audit;

Result:

text
VACUUM
sql
SELECT pg_size_pretty(pg_relation_size('audit')) AS after_vacuum, count(*) AS rows_left FROM audit;

Output:

text
 after_vacuum | rows_left
--------------+-----------
 4712 kB      | 2000
(1 row)
sql
VACUUM FULL audit;

PostgreSQL returns:

text
VACUUM
sql
SELECT pg_size_pretty(pg_relation_size('audit')) AS after_vacuum_full;

Result on PostgreSQL 17:

text
 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.

sql
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