100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is the VACUUM Process in PostgreSQL?

Learn how PostgreSQL VACUUM reclaims dead tuple space, updates statistics, and prevents transaction ID wraparound.

mediumQ99 of 228 in Database Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

VACUUM is a PostgreSQL maintenance operation that reclaims storage occupied by dead tuples β€” rows left behind by UPDATE and DELETE under PostgreSQL's MVCC model β€” and updates statistics so the query planner and transaction-ID wraparound machinery keep working correctly.

Because PostgreSQL never overwrites a row in place, an UPDATE creates a brand new tuple version and marks the old one as dead, and a DELETE simply marks a tuple as dead without removing it. Those dead tuples still occupy pages on disk and are invisible to new transactions but not actually freed. Plain VACUUM scans each table, marks the space used by dead tuples as reusable inside the existing pages (without shrinking the file back to the OS), updates the free space map and visibility map, and advances the table's frozen transaction-ID horizon to prevent wraparound failures.

  • Reclaims space for reuse by future inserts and updates
  • Prevents unbounded table and index bloat
  • Updates the visibility map so index-only scans stay fast
  • Protects against transaction ID wraparound data loss

AI Mentor Explanation

Picture a scoreboard operator who, instead of erasing a wrong entry, just writes a fresh corrected line below it and marks the old line as void β€” the void line still takes up space on the board. VACUUM is the groundskeeper who periodically walks the board and frees up every void line so new entries can be written into that same space instead of the board needing to be rebuilt bigger. Skip that cleanup for too long and the board keeps growing even though most of it is void lines nobody reads.

Step-by-Step Explanation

  1. Step 1

    Statement marks tuples dead

    An UPDATE or DELETE leaves the old row version in place and marks it dead rather than removing it immediately, per MVCC.

  2. Step 2

    VACUUM scans the table

    VACUUM (or autovacuum) walks each page, identifying dead tuples that are no longer visible to any active transaction.

  3. Step 3

    Space is reclaimed in place

    Dead tuple space is marked reusable in the free space map; plain VACUUM does not shrink the file on disk, VACUUM FULL does.

  4. Step 4

    Visibility map and stats refresh

    The visibility map and planner statistics are updated, and the table’s transaction-ID horizon is advanced to guard against wraparound.

What Interviewer Expects

  • Understanding that PostgreSQL uses MVCC and never overwrites rows in place
  • Clear distinction between reclaiming space in place versus VACUUM FULL shrinking the file
  • Awareness that VACUUM also protects against transaction ID wraparound
  • Knowledge that autovacuum runs this process automatically based on thresholds

Common Mistakes

  • Believing VACUUM deletes rows the same way DELETE does
  • Assuming plain VACUUM returns disk space to the operating system
  • Forgetting the wraparound-prevention role of VACUUM FREEZE
  • Not knowing VACUUM can run concurrently with normal reads and writes

Best Answer (HR Friendly)

β€œVACUUM is PostgreSQL's cleanup process that reclaims space left behind by updates and deletes, since PostgreSQL keeps old row versions around briefly instead of overwriting them instantly. It also keeps the query planner's statistics fresh and prevents a serious issue called transaction ID wraparound, so it is essential background maintenance rather than optional cleanup.”

Code Example

Running VACUUM and checking dead tuples
-- Reclaim space in place and update statistics
VACUUM (VERBOSE, ANALYZE) orders;

-- Check dead tuple counts before/after
SELECT relname, n_dead_tup, n_live_tup
FROM pg_stat_user_tables
WHERE relname = 'orders';

-- Fully rewrite the table and return space to the OS
-- (locks the table, use during a maintenance window)
VACUUM FULL orders;

Follow-up Questions

  • What is the difference between VACUUM and VACUUM FULL?
  • How does autovacuum decide when to run on a table?
  • What is transaction ID wraparound and why does VACUUM prevent it?
  • How does the visibility map speed up index-only scans?

MCQ Practice

1. What does plain VACUUM do to a table’s file size on disk?

Plain VACUUM marks dead-tuple space as reusable within existing pages; only VACUUM FULL rewrites the table to shrink the file.

2. Why does PostgreSQL need VACUUM at all under MVCC?

MVCC keeps old row versions for concurrent visibility instead of overwriting in place, so VACUUM is needed to reclaim that dead space.

3. Besides space reclamation, what critical safety role does VACUUM play?

VACUUM advances the frozen transaction-ID horizon, preventing wraparound that would otherwise make old data appear to vanish.

Flash Cards

What does VACUUM reclaim? β€” Storage occupied by dead tuples left behind by UPDATE and DELETE under MVCC.

Does plain VACUUM shrink the file on disk? β€” No, it marks space reusable internally; only VACUUM FULL returns space to the OS.

What wraparound risk does VACUUM prevent? β€” Transaction ID wraparound, which could otherwise make committed data appear invisible.

What runs VACUUM automatically? β€” The autovacuum daemon, based on configurable dead-tuple thresholds per table.

1 / 4

Continue Learning