0
0
PostgreSQLquery~20 mins

Serializable isolation in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Serializable Isolation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Effect of Serializable Isolation on Concurrent Updates

Consider two transactions running concurrently under serializable isolation in PostgreSQL. Both try to update the same row in a table accounts with columns id and balance. What will happen if both transactions commit?

PostgreSQL
BEGIN;
UPDATE accounts SET balance = balance + 100 WHERE id = 1;
-- Transaction 1 waits
COMMIT;
ABoth transactions abort due to deadlock errors.
BBoth transactions commit successfully, and the balance increases by 200.
COne transaction commits successfully; the other aborts with a serialization failure error.
DBoth transactions commit, but the balance only increases by 100.
Attempts:
2 left
💡 Hint

Serializable isolation prevents concurrent transactions from producing anomalies by aborting one if conflicts occur.

🧠 Conceptual
intermediate
1:30remaining
Understanding Serializable Isolation Guarantees

Which of the following best describes the guarantee provided by serializable isolation in PostgreSQL?

ATransactions only prevent dirty reads but allow non-repeatable reads and phantom reads.
BTransactions appear to execute in some sequential order, preventing anomalies like dirty reads and phantom reads.
CTransactions execute without any locking or blocking, improving concurrency at the cost of consistency.
DTransactions can see uncommitted changes from other transactions, allowing faster reads.
Attempts:
2 left
💡 Hint

Serializable isolation is the strictest standard isolation level.

📝 Syntax
advanced
1:30remaining
Setting Serializable Isolation Level in PostgreSQL

Which SQL command correctly sets the transaction isolation level to serializable for the current session in PostgreSQL?

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

Consider the scope of the setting: session vs transaction.

optimization
advanced
2:00remaining
Avoiding Serialization Failures in High Contention Scenarios

You have a high-traffic PostgreSQL application using serializable isolation. Serialization failures are frequent, causing many transaction retries. Which approach can reduce serialization failures without sacrificing serializability?

ADisable autocommit mode to batch multiple statements in one transaction.
BSwitch to read committed isolation level to avoid serialization failures.
CIncrease the max_connections setting to allow more concurrent transactions.
DUse explicit locking with <code>SELECT FOR UPDATE</code> to serialize access to rows.
Attempts:
2 left
💡 Hint

Explicit locking can help control concurrency conflicts.

🔧 Debug
expert
2:30remaining
Diagnosing Serialization Failure in a Complex Transaction

Given the following two transactions running concurrently under serializable isolation, which statement best explains why Transaction 2 fails with a serialization error?

-- Transaction 1
BEGIN;
UPDATE inventory SET quantity = quantity - 10 WHERE product_id = 5;
COMMIT;

-- Transaction 2
BEGIN;
SELECT quantity FROM inventory WHERE product_id = 5;
UPDATE inventory SET quantity = quantity - 5 WHERE product_id = 5;
COMMIT;
ATransaction 2 reads a stale value and tries to update based on it, causing a serialization conflict.
BTransaction 2 deadlocks because it tries to update before reading the row.
CTransaction 2 fails because it uses SELECT without FOR UPDATE, which is not allowed in serializable isolation.
DTransaction 2 fails because the inventory table has no primary key defined.
Attempts:
2 left
💡 Hint

Think about how concurrent reads and writes interact under serializable isolation.