Sub topic 3
VACUUM FULL: reclaiming space with a table rewrite
This chapter assumes you know basic SQL, can use psql, understand transactions, and have read how VACUUM removes dead tuples. Dead tuples are old row versions that no transaction can see anymore.
VACUUM FULL: reclaiming space with a table rewrite
Plain VACUUM marks space inside a table as reusable, but it usually does not shrink the file on disk. VACUUM FULL writes a new copy of the table with only live rows, then swaps it in and returns the unused space to the operating system.
VACUUM FULL public.orders;The command takes an ACCESS EXCLUSIVE lock for the whole rewrite. Reads and writes on the table wait until it finishes. It also rebuilds every index on the table.
> Warning: Run VACUUM FULL only in a maintenance window. On a large table it can block the application for minutes or hours.
Rewriting with minimal locking: pg_repack and REPACK
pg_repack is an extension that rebuilds a table online. It takes only short exclusive locks at the start and end. The table needs a primary key or a unique index on NOT NULL columns.
pg_repack --dbname=mydb --table=public.ordersThe REPACK command does a similar rewrite in PostgreSQL versions that ship it. Its plain form takes a strong lock, like VACUUM FULL. Check your version's documentation before you rely on it.
Visibility map, freezing, and transaction ID wraparound
- The visibility map records which pages hold only visible rows, so VACUUM can skip them.
- Freezing marks old row versions as permanently visible. This keeps transaction IDs, which are 32-bit counters, from wrapping around.
- If a table is not vacuumed for too long, PostgreSQL forces an anti-wraparound vacuum and can stop accepting writes to protect data.
Index bloat and REINDEX CONCURRENTLY
Indexes bloat too. REINDEX CONCURRENTLY rebuilds an index without blocking writes:
REINDEX INDEX CONCURRENTLY public.orders_customer_id_idx;This command cannot run inside a transaction block. If it fails, it can leave an invalid index behind. Check indisvalid in pg_index and drop any invalid index.
Preventing bloat with fillfactor and HOT updates
fillfactor sets how full PostgreSQL packs each page when it writes rows. A value of 90 leaves 10 percent free space for updates:
ALTER TABLE public.orders SET (fillfactor = 90);A HOT (heap-only tuple) update stores the new row version on the same page. If no indexed column changes, indexes need no new entries. The free space from fillfactor makes HOT updates more likely.
Monitoring bloat and building a maintenance routine
Check bloat regularly with pg_stat_user_tables, and use pgstattuple for exact figures. Run a reclaim method only when the numbers show a problem.
Exercise
A 50 GB table is bloated, and the application cannot tolerate a lock that blocks writes. Which method should you use, and why?
Show answer
Use pg_repack, or REPACK if your version provides it with a low-lock option. Both rebuild the table online with only short exclusive locks, while VACUUM FULL blocks reads and writes for the whole rewrite.