What is MVCC in PostgreSQL?
sobes.tech AI
Answer from AI
MVCC (Multiversion Concurrency Control) in PostgreSQL is a mechanism for managing concurrent access to data, allowing multiple transactions to work with the database in parallel without read locks.
The main idea of MVCC is to store multiple versions of data rows with different timestamps (Transaction ID). Each transaction only sees the versions of rows that were current at the time it started, which ensures:
- Uninterrupted reading without locks — reading does not block writing and vice versa.
- Transaction isolation — each transaction works with a consistent snapshot of data.
In PostgreSQL, when a row is updated, a new version is created, and the old one is marked as outdated but remains until all transactions that could have seen it are finished. The VACUUM process is used to clean up old versions.
Example: if transaction A started, and transaction B updated the row afterward, transaction A will still see the old version of the row, while transaction B will see the new one.