0
0
PostgreSQLquery~10 mins

MVCC mental model in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - MVCC mental model in PostgreSQL
Transaction Start
Read Snapshot of Data
Perform Reads/Writes
Create New Versions for Writes
Commit or Rollback
Old Versions Visible to Other Transactions Until Cleanup
This flow shows how a transaction in PostgreSQL reads a snapshot of data, creates new versions for writes, and commits or rolls back, while old versions remain visible to other transactions until cleaned up.
Execution Sample
PostgreSQL
BEGIN;
SELECT * FROM accounts WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 1;
COMMIT;
A transaction reads an account balance, updates it by adding 100, and commits the change.
Execution Table
StepActionData Version ReadNew Version CreatedVisibility to Other Transactions
1BEGIN transactionSnapshot of current committed dataNoNo changes yet
2SELECT balance WHERE id=1Version visible at transaction startNoNo changes yet
3UPDATE balance + 100 WHERE id=1Reads old versionCreates new version with updated balanceNew version visible only to this transaction
4COMMIT transactionN/ANew version becomes committedNew version visible to all new transactions
5Other transactions still see old version until cleanupOld version remains until vacuumNoOld version visible to other active transactions
💡 Transaction commits, new version becomes visible; old versions remain until cleanup.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
balance_versionv1 (old)v1 (read)v2 (new created)v2 (committed)v2 (final visible)
transaction_stateidleactiveactivecommittedcommitted
Key Moments - 3 Insights
Why does the transaction read the old version of data even after an update?
Because PostgreSQL uses MVCC, the update creates a new version invisible to other transactions until commit. The current transaction sees its own new version, but others still see the old version until cleanup. See execution_table step 3.
When do other transactions see the updated data?
Other transactions see the new version only after the committing transaction finishes and commits. Before that, they see the old version. See execution_table step 4.
What happens to old versions after commit?
Old versions remain in the database until a cleanup process (vacuum) removes them. This allows other transactions that started earlier to still see consistent data. See execution_table step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the new data version created?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Check the 'New Version Created' column in execution_table rows.
According to variable_tracker, what is the transaction_state after the COMMIT?
Aidle
Bcommitted
Cactive
Drolled back
💡 Hint
Look at the 'transaction_state' row after Step 4 in variable_tracker.
When do other transactions stop seeing the old data version?
AImmediately after UPDATE
BAfter BEGIN
CAfter COMMIT and cleanup
DBefore COMMIT
💡 Hint
Refer to execution_table steps 4 and 5 about visibility and cleanup.
Concept Snapshot
MVCC in PostgreSQL:
- Each transaction reads a snapshot of data.
- Writes create new versions, not overwrite.
- New versions visible only after commit.
- Old versions kept for other transactions until vacuum.
- Ensures consistent reads without locking.
Full Transcript
In PostgreSQL, MVCC means each transaction works with a snapshot of the database. When a transaction starts, it sees the data as it was at that moment. If it updates data, PostgreSQL creates a new version of that data row instead of changing the old one. This new version is only visible to the transaction that made the change until it commits. Other transactions continue to see the old version until the first transaction commits and the old versions are cleaned up later. This system allows many transactions to work at the same time without blocking each other, keeping data consistent and safe.