0
0
PostgreSQLquery~20 mins

Why concurrency control matters in PostgreSQL - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Concurrency Control Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why is concurrency control important in databases?

Imagine multiple people trying to update the same bank account balance at the same time. What problem does concurrency control help to prevent?

AIt automatically backs up the database every time a user makes a change.
BIt prevents data from being lost or overwritten incorrectly when multiple users access the same data simultaneously.
CIt encrypts the data to protect it from unauthorized access.
DIt speeds up the database by allowing unlimited simultaneous writes without restrictions.
Attempts:
2 left
πŸ’‘ Hint

Think about what happens if two people try to change the same number at once without coordination.

❓ query_result
intermediate
2:00remaining
What is the output of this transaction sequence?

Two transactions run concurrently on a PostgreSQL database with a table accounts having a row with id=1 and balance=100.

Transaction 1: BEGIN; UPDATE accounts SET balance = balance + 50 WHERE id = 1; COMMIT;

Transaction 2: BEGIN; UPDATE accounts SET balance = balance - 30 WHERE id = 1; COMMIT;

If both transactions run at the same time without concurrency control, what could be the final balance?

PostgreSQL
SELECT balance FROM accounts WHERE id = 1;
A120
B150
C100
D80
Attempts:
2 left
πŸ’‘ Hint

Consider what happens if one update overwrites the other without seeing it.

πŸ“ Syntax
advanced
2:00remaining
Which SQL statement correctly locks a row to prevent concurrent updates?

In PostgreSQL, which statement locks a specific row in the accounts table to prevent other transactions from modifying it until the current transaction finishes?

ASELECT * FROM accounts WHERE id = 1 FOR UPDATE;
BLOCK TABLE accounts IN EXCLUSIVE MODE;
CSELECT * FROM accounts WHERE id = 1 LOCK ROW;
DUPDATE accounts SET balance = balance WHERE id = 1 LOCK;
Attempts:
2 left
πŸ’‘ Hint

Look for the standard SQL syntax that locks rows for update.

❓ optimization
advanced
2:00remaining
How can you reduce deadlocks in concurrent transactions?

Deadlocks happen when two transactions wait for each other’s locks. Which practice helps reduce deadlocks in a PostgreSQL database?

AUse random order to access tables to avoid predictability.
BAvoid using transactions altogether.
CAccess tables and rows in the same order in all transactions.
DIncrease the timeout for locks to wait longer.
Attempts:
2 left
πŸ’‘ Hint

Think about how consistent ordering can prevent circular waits.

πŸ”§ Debug
expert
3:00remaining
Why does this transaction cause a serialization failure?

Two concurrent transactions run these commands:

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

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

Both use SERIALIZABLE isolation level. Why might one transaction fail with a serialization error?

ABecause the transactions are missing explicit <code>LOCK TABLE</code> statements.
BBecause the <code>SELECT</code> statement locks the row exclusively, blocking the other transaction indefinitely.
CBecause <code>SERIALIZABLE</code> isolation allows dirty reads, causing inconsistent data.
DBecause both transactions read the same initial balance and then update it, causing a conflict that PostgreSQL detects to maintain correctness.
Attempts:
2 left
πŸ’‘ Hint

Think about how PostgreSQL ensures transactions appear as if they ran one after another.