0
0
PostgreSQLquery~20 mins

MVCC mental model in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MVCC Mastery in PostgreSQL
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
How does MVCC handle concurrent reads and writes?

In PostgreSQL's MVCC system, what happens when one transaction reads a row that another transaction is currently updating but not yet committed?

AThe reading transaction sees the old committed version of the row, ignoring the uncommitted changes.
BThe reading transaction sees the uncommitted changes immediately.
CThe reading transaction gets an error because the row is locked.
DThe reading transaction waits until the writing transaction commits or rolls back before reading the row.
Attempts:
2 left
💡 Hint

Think about how MVCC provides a consistent snapshot for readers.

query_result
intermediate
2:00remaining
Result of concurrent transactions with MVCC

Consider two transactions in PostgreSQL:

-- Transaction 1
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
-- Transaction 2
BEGIN;
SELECT balance FROM accounts WHERE id = 1;

What balance does Transaction 2 see if Transaction 1 has not committed yet?

AThe balance after subtracting 100 (uncommitted update).
BAn error due to concurrent update.
CNULL because the row is locked.
DThe balance before subtracting 100 (last committed value).
Attempts:
2 left
💡 Hint

Remember MVCC shows a consistent snapshot to readers.

📝 Syntax
advanced
2:00remaining
Identify the MVCC-related error in this transaction

Which option shows a transaction that will cause a serialization failure error due to MVCC in PostgreSQL?

-- Transaction 1
BEGIN;
SELECT balance FROM accounts WHERE id = 1;
UPDATE accounts SET balance = balance + 50 WHERE id = 1;
COMMIT;
ATwo concurrent transactions both read and update the same row without locking, causing serialization failure.
BA transaction that updates different rows, so no serialization failure occurs.
CA transaction that only reads data without updates, so no serialization failure occurs.
DA transaction that uses explicit locks to prevent serialization failure.
Attempts:
2 left
💡 Hint

Think about what causes serialization failures in MVCC.

optimization
advanced
2:00remaining
Optimizing MVCC vacuuming in PostgreSQL

Which approach best helps reduce bloat caused by MVCC dead tuples in PostgreSQL?

ADisabling autovacuum and manually running VACUUM FULL weekly.
BIncreasing autovacuum frequency and tuning vacuum cost parameters.
CUsing only DELETE statements without UPDATEs to avoid dead tuples.
DIncreasing transaction isolation level to SERIALIZABLE.
Attempts:
2 left
💡 Hint

Think about how PostgreSQL cleans up old row versions.

🔧 Debug
expert
3:00remaining
Diagnosing phantom reads under MVCC

In PostgreSQL, a transaction running at REPEATABLE READ isolation sees phantom rows after another transaction inserts new rows and commits. Why does this happen?

APhantom reads occur because the first transaction did not commit before the second started.
BMVCC blocks all phantom reads by locking all rows in the table.
CREPEATABLE READ in PostgreSQL does not prevent phantom reads; only SERIALIZABLE does.
DPhantom reads are impossible in PostgreSQL due to MVCC.
Attempts:
2 left
💡 Hint

Consider the difference between REPEATABLE READ and SERIALIZABLE isolation levels.