What is MVCC in PostgreSQL?
IntermediateConceptPostgreSQL
Answer
Short answer
MVCC (Multi-Version Concurrency Control) means PostgreSQL keeps multiple versions of a row instead of overwriting it. An UPDATE writes a new row version and marks the old one as dead. Each transaction sees a consistent snapshot, so readers never block writers and writers never block readers.
How it plays out
- Transaction A starts reading
orders. - Transaction B updates order 42 and commits → a new version of row 42 is written.
- A still sees the old version (its snapshot), B and new transactions see the new one.
- Later, when no transaction needs the old version, VACUUM cleans it up.
Why DevOps cares
- Dead rows accumulate → table and index bloat unless autovacuum keeps up.
- Long-running transactions (an open
BEGINin someone's psql session, a stuck report) prevent cleanup of everything newer — the database grows and slows.
SELECT pid, state, now() - xact_start AS age, query
FROM pg_stat_activity WHERE xact_start IS NOT NULL ORDER BY age DESC LIMIT 5;
What to say in the interview
"MVCC keeps row versions so each transaction sees a snapshot and reads don't block writes. The cost is dead tuples that VACUUM must remove, so I watch autovacuum and kill long-running or idle-in-transaction sessions that block cleanup."
More on PostgreSQL
How do you set up PostgreSQL and log in with psql?BeginnerHow do you create a database, tables, users and query data in PostgreSQL?BeginnerHow do you back up and restore a PostgreSQL database?BeginnerWhat is PostgreSQL and why is it so popular?BeginnerHow is PostgreSQL organised: cluster, database, schema and table?BeginnerHow do you connect to PostgreSQL from the command line?Beginner