0
0
PostgreSQLquery~20 mins

Transaction isolation levels in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Transaction Isolation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Effect of READ COMMITTED isolation on concurrent transactions

Consider two concurrent transactions in PostgreSQL with the default READ COMMITTED isolation level. Transaction A updates a row but does not commit yet. Transaction B tries to read the same row.

What will Transaction B see when it reads the row before Transaction A commits?

PostgreSQL
BEGIN;
UPDATE accounts SET balance = balance + 100 WHERE id = 1; -- Transaction A
-- Transaction A does not commit yet

-- Transaction B starts
BEGIN;
SELECT balance FROM accounts WHERE id = 1; -- Transaction B reads
COMMIT;
ATransaction B sees the updated balance immediately.
BTransaction B gets an error due to concurrent update.
CTransaction B's read blocks until Transaction A commits.
DTransaction B sees the old balance before Transaction A's update.
Attempts:
2 left
💡 Hint

Think about how READ COMMITTED isolation works in PostgreSQL regarding visibility of uncommitted changes.

🧠 Conceptual
intermediate
2:00remaining
Understanding phantom reads in REPEATABLE READ isolation

Which of the following phenomena can still occur under the REPEATABLE READ isolation level in PostgreSQL?

APhantom reads
BNon-repeatable reads
CDirty reads
DLost updates
Attempts:
2 left
💡 Hint

Recall the differences between isolation levels and what each prevents.

📝 Syntax
advanced
2:00remaining
Setting SERIALIZABLE isolation level in PostgreSQL

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

ASET ISOLATION LEVEL SERIALIZABLE;
BSET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL SERIALIZABLE;
CSET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
DALTER SESSION SET ISOLATION LEVEL SERIALIZABLE;
Attempts:
2 left
💡 Hint

Consider the correct syntax for setting isolation level for the session versus a single transaction.

optimization
advanced
2:00remaining
Avoiding serialization failures in SERIALIZABLE isolation

In PostgreSQL, when using SERIALIZABLE isolation, transactions may fail with serialization errors. Which approach helps reduce these failures?

AUse optimistic concurrency control by retrying failed transactions.
BSwitch to READ UNCOMMITTED isolation level.
CIncrease the statement timeout to avoid aborts.
DUse explicit locking with <code>FOR UPDATE</code> to serialize access.
Attempts:
2 left
💡 Hint

Think about how PostgreSQL handles conflicts under SERIALIZABLE isolation.

🔧 Debug
expert
2:00remaining
Diagnosing unexpected dirty reads in PostgreSQL

A developer reports that a transaction running under READ COMMITTED isolation level is seeing uncommitted changes from another transaction (dirty reads). Which is the most likely cause?

AThe developer is querying a materialized view that is not refreshed.
BThe transaction is using explicit locking that bypasses isolation.
CThe database is using a non-standard isolation level like READ UNCOMMITTED.
DThe developer is reading from a different database instance with replication lag.
Attempts:
2 left
💡 Hint

Recall PostgreSQL's default isolation levels and what it supports.