0
0
PostgreSQLquery~20 mins

Deadlock detection and prevention in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Deadlock Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
What is a deadlock in database systems?

Imagine two people trying to use two shared tools, but each holds one tool and waits for the other to release the second tool. What does this situation represent in databases?

AA deadlock where two transactions wait indefinitely for each other to release locks.
BA successful transaction commit without conflicts.
CA database backup process running in the background.
DA query optimization step that speeds up execution.
Attempts:
2 left
💡 Hint

Think about what happens when two processes wait forever for resources held by each other.

query_result
intermediate
2:00remaining
Which query shows current deadlocks in PostgreSQL?

PostgreSQL provides system views to monitor locks and deadlocks. Which query correctly lists current deadlocks?

ASELECT * FROM pg_locks WHERE NOT granted;
BSELECT * FROM pg_locks WHERE NOT granted AND pid IN (SELECT pid FROM pg_stat_activity WHERE wait_event_type = 'Lock');
CSELECT * FROM pg_stat_activity WHERE wait_event_type = 'Lock';
DSELECT * FROM pg_stat_activity WHERE state = 'idle';
Attempts:
2 left
💡 Hint

Look for locks that are waiting and relate them to processes waiting on locks.

📝 Syntax
advanced
2:30remaining
Which transaction block prevents deadlocks by locking tables in a consistent order?

Two transactions lock tables in different orders causing deadlocks. Which code block correctly locks tables in the same order to prevent deadlocks?

ABEGIN; LOCK TABLE orders IN EXCLUSIVE MODE; LOCK TABLE customers IN SHARE MODE; COMMIT;
BBEGIN; LOCK TABLE customers IN EXCLUSIVE MODE; LOCK TABLE orders IN EXCLUSIVE MODE; COMMIT;
CBEGIN; LOCK TABLE orders IN EXCLUSIVE MODE; LOCK TABLE customers IN EXCLUSIVE MODE; COMMIT;
DBEGIN; LOCK TABLE customers IN SHARE MODE; LOCK TABLE orders IN EXCLUSIVE MODE; COMMIT;
Attempts:
2 left
💡 Hint

Consistent locking order means all transactions lock tables in the same sequence and mode.

optimization
advanced
2:00remaining
How can you reduce deadlock chances in high-concurrency environments?

In a busy database, deadlocks happen often. Which approach best reduces deadlock frequency?

AKeep transactions short and access tables in a consistent order.
BIncrease transaction isolation level to SERIALIZABLE for all queries.
CUse long-running transactions to hold locks longer.
DDisable all locking mechanisms to avoid waiting.
Attempts:
2 left
💡 Hint

Think about how transaction length and lock order affect waiting.

🔧 Debug
expert
3:00remaining
What error does this PostgreSQL transaction raise?

Two transactions run concurrently:
Transaction 1:
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;

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

What error will occur if both run at the same time?

ANo error; both transactions commit successfully
BERROR: syntax error at or near "UPDATE"
CERROR: division by zero
DERROR: deadlock detected; transaction aborted
Attempts:
2 left
💡 Hint

Think about what happens when two transactions lock rows in opposite order.