PostgreSQL Enterprise (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)
How does Multi-Version Concurrency Control (MVCC) work internally in PostgreSQL?
senior LevelCode ExampleSystem DesignDatabases & SQLPostgreSQL
In Simple Words: PostgreSQL implements MVCC by retaining historical tuple versions directly inside table data pages (Heap files):
- Every table row header contains hidden transaction metadata:
xmin: The Transaction ID (txid) of the transaction that created/inserted the row.xmax: The Transaction ID of the transaction that deleted or updated the row (0 for live active rows).
- Update Mechanism: In PostgreSQL, an
UPDATEdoes not overwrite data in place! It marks the existing row withxmax = current_txid(soft delete) and inserts a completely new row tuple withxmin = current_txid! - Readers compare
xminandxmaxagainst their transaction snapshot to determine row visibility without locking.
text
PostgreSQL Update via MVCC:
[Old Tuple: Row 1] ──► Header: xmin = 100, xmax = 105 (Marked Deleted by Tx 105)
│ Pointer to new version (t_ctid)
▼
[New Tuple: Row 1'] ──► Header: xmin = 105, xmax = 0 (Active live row!)