VACUUM (Reclaim Table Space)
Last updated: 2026-11-04
VACUUM (Reclaim Table Space)
VACUUM reclaims the space held by dead tuples left behind by UPDATE and DELETE, and refreshes the table's visibility map so index scans get faster. Under PostgreSQL's MVCC, old row versions are not removed immediately; autovacuum cleans up in the background, and you can also run VACUUM by hand after a large delete or update.
| Kind | command |
|---|---|
| Example Language | SQL |
📝 Syntax
VACUUM [ ( <option> [, ...] ) ] [TABLE] <table> [, ...]
-- common options
-- FULL rewrite the table and return space to the OS
-- ANALYZE also update statistics
-- VERBOSE print per-table progress
-- FREEZE freeze sufficiently old tuples
⚙️ Parameters
| FULL | Rewrites the table and returns space to the OS; costly and locks |
|---|---|
| ANALYZE | Also refreshes statistics |
| VERBOSE | Prints per-table progress |
| FREEZE | Freezes sufficiently old tuples for long-term archiving |
Returns:No return value. After it finishes, the space held by dead tuples is marked reusable.
▶ Example
Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.
Output:
VACUUM
📌 Key Points
- VACUUM cannot run inside a transaction block and fails with VACUUM cannot run inside a transaction block.
- A plain VACUUM leaves the space in the table for reuse; VACUUM FULL rewrites the table and returns the space to the OS, but needs an ACCESS EXCLUSIVE lock and blocks reads and writes.
- VACUUM ANALYZE also refreshes statistics — the usual combination.
- VACUUM never removes rows newer than an open transaction snapshot, so running queries are not disturbed.
- autovacuum already does this in the background; running VACUUM by hand is mainly for right after a large change.
- VACUUM cannot be called from a stored procedure or function for the same reason.
💡 Related Cases
More case studies coming soon.
📚 Related Reference
❓ FAQ
QWhy does VACUUM report cannot run inside a transaction block?
AVACUUM has to run outside a transaction: it cannot sit inside a BEGIN/COMMIT block or inside a stored function. Commit it as a standalone statement.
QDoes VACUUM lock the table?
AA plain VACUUM takes only SHARE UPDATE EXCLUSIVE, which does not block normal reads and writes; VACUUM FULL rewrites the table and blocks all access.