0
0
PostgreSQLquery~20 mins

Repeatable read behavior in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Repeatable Read Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Repeatable Read: Consistent Snapshot Query

Consider two concurrent transactions in PostgreSQL using REPEATABLE READ isolation level. Transaction A reads a row, then Transaction B updates that row and commits. Transaction A reads the same row again.

What will Transaction A see on the second read?

PostgreSQL
BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;
SELECT balance FROM accounts WHERE id = 1; -- returns 100
-- Concurrently, another transaction updates balance to 200 and commits
SELECT balance FROM accounts WHERE id = 1; -- What is the output here?
A100 (the original value, snapshot is consistent)
B200 (the updated value from the other transaction)
CNULL (the row is locked and not visible)
DError: serialization failure
Attempts:
2 left
💡 Hint

Think about how REPEATABLE READ isolation keeps a consistent snapshot for the duration of the transaction.

🧠 Conceptual
intermediate
2:00remaining
Understanding Phantom Reads in Repeatable Read

Which of the following statements about phantom reads under REPEATABLE READ isolation level in PostgreSQL is true?

APhantom reads can occur because new rows inserted by other transactions are visible.
BPhantom reads cause immediate transaction abort in REPEATABLE READ.
CPhantom reads are prevented by locking all rows in the table at the start.
DPhantom reads cannot occur because the transaction sees a consistent snapshot including all rows at start.
Attempts:
2 left
💡 Hint

Consider how PostgreSQL's MVCC handles visibility of new rows in REPEATABLE READ.

📝 Syntax
advanced
2:00remaining
Setting Repeatable Read Isolation Level Syntax

Which of the following SQL commands correctly sets the transaction isolation level to REPEATABLE READ in PostgreSQL for the current transaction?

ABEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;
BSTART TRANSACTION WITH ISOLATION LEVEL REPEATABLE READ;
CSET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
DSET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
Attempts:
2 left
💡 Hint

Focus on the command that sets isolation level for the current transaction before it starts.

optimization
advanced
2:00remaining
Avoiding Serialization Failures in Repeatable Read

In PostgreSQL, under REPEATABLE READ isolation, serialization failures can occur due to concurrent updates. Which approach best reduces serialization failures while keeping repeatable read guarantees?

ASwitch to READ COMMITTED isolation level to avoid serialization failures.
BUse explicit row-level locking (SELECT ... FOR UPDATE) to serialize access.
CUse advisory locks to coordinate transactions externally.
DIncrease max_connections to reduce contention.
Attempts:
2 left
💡 Hint

Think about how locking can prevent concurrent conflicting updates.

🔧 Debug
expert
2:00remaining
Diagnosing Serialization Failure in Repeatable Read

Two concurrent transactions under REPEATABLE READ isolation update the same row and commit. One transaction gets a serialization failure error. What is the most likely cause?

AThe row was locked by a long-running SELECT query without FOR UPDATE.
BThe transactions used different isolation levels causing inconsistent visibility.
CBoth transactions tried to update the same row concurrently, causing a conflict detected by PostgreSQL's serialization mechanism.
DThe database ran out of disk space during the commit.
Attempts:
2 left
💡 Hint

Consider how PostgreSQL detects conflicts under REPEATABLE READ.