0
0
PostgreSQLquery~20 mins

Read committed behavior in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Read Committed Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Understanding Read Committed Isolation Level

Consider two transactions running concurrently under the Read Committed isolation level in PostgreSQL.

Transaction 1 updates a row but has not committed yet. Transaction 2 tries to read the same row.

What will Transaction 2 see?

PostgreSQL
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
-- Transaction 1 has not committed yet

-- Transaction 2 starts
BEGIN;
SELECT balance FROM accounts WHERE id = 1;
COMMIT;
ATransaction 2 sees a partial update causing inconsistent data.
BTransaction 2 sees the updated balance even though Transaction 1 has not committed.
CTransaction 2 is blocked until Transaction 1 commits.
DTransaction 2 sees the original balance before the update.
Attempts:
2 left
💡 Hint

Read Committed isolation level only shows committed data to other transactions.

query_result
intermediate
2:00remaining
Effect of Commit on Read Committed Reads

Transaction 1 updates a row and commits immediately. Transaction 2 reads the same row twice under Read Committed isolation.

What can Transaction 2 observe?

PostgreSQL
BEGIN;
UPDATE products SET price = price + 10 WHERE id = 5;
COMMIT;

-- Transaction 2
BEGIN;
SELECT price FROM products WHERE id = 5;
-- some time passes
SELECT price FROM products WHERE id = 5;
COMMIT;
ABoth SELECTs return the updated price after the commit.
BBoth SELECTs return the same original price before the update.
CThe first SELECT returns the original price; the second returns the updated price.
DThe first SELECT returns the updated price; the second returns the original price.
Attempts:
2 left
💡 Hint

Each query in Read Committed sees the latest committed data at its start.

📝 Syntax
advanced
2:00remaining
Detecting Read Committed Behavior in SQL

Which SQL statement correctly sets the transaction isolation level to Read Committed in PostgreSQL?

ABEGIN TRANSACTION ISOLATION LEVEL READ COMMITTED;
BSET ISOLATION LEVEL READ COMMITTED;
CSET TRANSACTION ISOLATION LEVEL READ COMMITTED;
DSET SESSION TRANSACTION READ COMMITTED;
Attempts:
2 left
💡 Hint

Check the exact syntax for setting isolation level per transaction.

🧠 Conceptual
advanced
2:00remaining
Why Read Committed Prevents Dirty Reads

Why does the Read Committed isolation level prevent dirty reads in PostgreSQL?

ABecause it locks all rows for reading until the transaction commits.
BBecause it only reads data that has been committed by other transactions at the time of the query.
CBecause it caches data locally to avoid reading uncommitted changes.
DBecause it uses snapshot isolation to provide a consistent view of data.
Attempts:
2 left
💡 Hint

Think about what data is visible to a query under Read Committed.

🔧 Debug
expert
2:00remaining
Analyzing Non-Repeatable Read under Read Committed

Transaction 1 updates a row and commits. Transaction 2 reads the same row twice under Read Committed isolation level.

Transaction 2 notices the two reads return different values. What is this phenomenon called?

ANon-repeatable read
BPhantom read
CDirty read
DLost update
Attempts:
2 left
💡 Hint

It happens when a row changes between two reads in the same transaction.