Skip to main content
InterviewHubPRO
Section Overview
Q37

Question 37 of 50

Part 3: Advanced Internals, Scale & Production Patterns (Senior / Lead)

74%
PostgreSQL Enterprise (50 Qs)•Part 3: Advanced Internals, Scale & Production Patterns (Senior / Lead)

PostgreSQL `VACUUM` & `AUTOVACUUM` Tuning.

senior LevelDatabases & SQLPostgreSQL

In Simple Words: VACUUM reclaims storage space occupied by dead tuples and prevents Transaction ID (XID) Wraparound:

  1. Standard VACUUM: Scans pages, reclaims dead tuple space for future inserts in the table without acquiring an exclusive table lock.
  2. VACUUM FULL: Rewrites the entire table into a new disk file to return space to the OS; requires an exclusive table lock (ACCESS EXCLUSIVE) that blocks all reads and writes!
  3. Transaction ID Wraparound: PostgreSQL transaction IDs are 32-bit integers ($\approx 4\text{ Billion}$). If VACUUM FREEZE does not advance the oldest XID before 2 billion transactions pass, Postgres halts all write operations to prevent catastrophic data corruption.
  4. Tuning autovacuum: For large tables, lower autovacuum_vacuum_scale_factor from 0.2 (20%) to 0.02 (2%) so vacuuming runs in smaller, frequent batches.