VACUUM vs VACUUM FULL in PostgreSQL
A common misconception is:
"VACUUM removes dead tuples, so table size should shrink."
Wrong.
Regular VACUUM and VACUUM FULL have very different objectives.
Understanding the Problem
Suppose a table has grown to:
Live data:
Dead tuples:
After many UPDATEs and DELETEs.
Regular VACUUM
What it does
- Finds dead tuples.
- Marks their space as reusable.
- Updates visibility information.
- Helps index-only scans.
- Advances freeze information.
What it DOES NOT do
It does not return space to the operating system.
Example
Before:
After VACUUM:
File size remains:
But PostgreSQL can reuse that free space for future inserts/updates.
Analogy
Think of a hotel.
Before VACUUM:
VACUUM:
Hotel building still has:
The building doesn't shrink.
VACUUM FULL
What it does
Instead of reusing space inside the file:
Example
Before:
After:
Space is returned to:
immediately.
Analogy
Using the hotel example:
Before:
VACUUM FULL:
Now building size becomes:
Internal Architecture
Regular VACUUM
No table rewrite.
VACUUM FULL
Complete rewrite.
Locking Behavior
VACUUM
Lock:
Practical impact:
✅ SELECT works
✅ INSERT works
✅ UPDATE works
✅ DELETE works
Usually invisible to users.
VACUUM FULL
Lock:
Impact:
❌ SELECT blocked
❌ INSERT blocked
❌ UPDATE blocked
❌ DELETE blocked
Everything waits.
Example:
VACUUM FULL might run:
During which the table is unavailable.
Extra Disk Requirement
VACUUM
Needs very little additional disk.
VACUUM FULL
Needs enough space to hold:
Example:
May require:
temporarily.
Index Impact
VACUUM
Indexes stay as-is.
Some index bloat may remain.
VACUUM FULL
Indexes are rebuilt.
Example:
Result:
Performance Comparison
| Feature | VACUUM | VACUUM FULL |
|---|---|---|
| Removes dead tuples | Yes | Yes |
| Reclaims internal space | Yes | Yes |
| Returns disk to OS | No | Yes |
| Rewrites table | No | Yes |
| Rebuilds indexes | No | Yes |
| Requires exclusive lock | No | Yes |
| AutoVacuum uses it | Yes | No |
| Suitable for production use | Yes | Rarely |
Check Whether VACUUM Is Enough
Query:
If:
Use:
When VACUUM FULL Makes Sense
Case 1: Massive Delete
Deleted:
Table remains:
Live data:
VACUUM won't shrink it.
VACUUM FULL can reduce:
Case 2: One-Time Data Purge
Then run:
Case 3: After Bulk Migration Cleanup
Reclaim storage.
Why Production DBAs Prefer pg_repack
Instead of:
many DBAs use:
Benefits:
✅ Removes bloat
✅ Rebuilds indexes
✅ Minimal locking
✅ Works online
Very common on:
Interview Answer
VACUUM removes dead tuples and makes the space reusable inside the table file, but it does not reduce the file size on disk. It runs without blocking normal application activity and is what Autovacuum uses.
VACUUM FULL rewrites the entire table and indexes into a new compact copy, returning unused space to the operating system. However, it requires an ACCESS EXCLUSIVE lock, blocks all access to the table, and needs additional temporary disk space.
One-line summary:
VACUUM reuses free space inside the table; VACUUM FULL shrinks the table by rebuilding it.
No comments:
Post a Comment