Pcornel
Databases · Transactions, MVCC & Locking

MVCC and Vacuum

Cheat sheet · https://pcornel.dk/topic.php?topic=6174

Back to the Topic

Answer

MVCC keeps old row versions for concurrent readers. In PostgreSQL, VACUUM removes dead versions and prevents transaction ID wraparound; autovacuum does this automatically.

Remember

Long-running or idle-in-transaction sessions stop vacuum from cleaning. Tune autovacuum per table for busy tables. Wraparound warnings are urgent.

First checks

  • SELECT relname,n_dead_tup,last_autovacuum FROM pg_stat_user_tables ORDER BY n_dead_tup DESC; — first check for this topic
  • psql -c "SELECT now(), version();" — PostgreSQL reachable and version
  • mysql -e "SELECT NOW(), VERSION();" — MariaDB/MySQL reachable and version
  • sqlcmd -S <server> -Q "SELECT @@VERSION" — SQL Server reachable and version

Verify in this order

updates/deletes create dead tuples → vacuum reclaims → visibility map → freeze

Where it usually breaks

An open transaction for hours causes bloat across the database.

Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=6174

Confirmation

Confirm action

Continue?