Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a deadlock in a database system?
A deadlock happens when two or more transactions wait forever for each other to release locks, so none can proceed.
Click to reveal answer
intermediate
How does PostgreSQL detect deadlocks?
PostgreSQL periodically checks for cycles in the waiting transactions graph to find deadlocks and then aborts one transaction to break the cycle.
Click to reveal answer
beginner
Name one common method to prevent deadlocks.
One method is to always acquire locks in a consistent order across transactions to avoid circular waiting.
Click to reveal answer
intermediate
What happens when PostgreSQL detects a deadlock?
PostgreSQL cancels one of the transactions involved in the deadlock and returns an error to that transaction, allowing others to continue.
Click to reveal answer
advanced
Why is deadlock prevention better than detection in some cases?
Prevention avoids deadlocks before they happen, improving performance and user experience by not having to roll back transactions.
Click to reveal answer
What causes a deadlock in PostgreSQL?
ATwo transactions waiting on each other's locks
BA transaction running too long
CA missing index
DA syntax error in SQL
✗ Incorrect
Deadlocks occur when transactions wait on each other's locks, creating a cycle with no progress.
How does PostgreSQL resolve a detected deadlock?
AIt cancels one transaction involved in the deadlock
BIt restarts the database server
CIt ignores the deadlock and waits
DIt locks all tables
✗ Incorrect
PostgreSQL cancels one transaction to break the deadlock cycle.
Which practice helps prevent deadlocks?
AAcquiring locks in random order
BAcquiring locks in a consistent order
CUsing only read locks
DDisabling transactions
✗ Incorrect
Consistent lock order prevents circular waiting and deadlocks.
What is a sign of a deadlock in PostgreSQL logs?
ANOTICE: query completed
BWARNING: slow query
CERROR: deadlock detected
DINFO: connection established
✗ Incorrect
PostgreSQL logs 'ERROR: deadlock detected' when it finds a deadlock.
Why might deadlock prevention be preferred over detection?
AIt uses more CPU
BIt disables locking
CIt allows deadlocks to happen
DIt avoids transaction rollbacks
✗ Incorrect
Prevention avoids deadlocks and the need to rollback transactions, improving performance.
Explain what a deadlock is and how PostgreSQL detects it.
Think about transactions waiting on each other's locks.
You got /4 concepts.
Describe two ways to prevent deadlocks in database transactions.
Focus on how to avoid circular waiting and long lock holding.
You got /4 concepts.
Practice
(1/5)
1. What is a deadlock in PostgreSQL?
easy
A. A performance optimization technique for faster queries.
B. A syntax error in SQL statements causing query failure.
C. A backup process that locks tables during data export.
D. A situation where two or more transactions wait indefinitely for each other to release locks.
Solution
Step 1: Understand transaction locking
Transactions acquire locks on resources to maintain data integrity.
Step 2: Define deadlock
A deadlock occurs when transactions wait on each other in a cycle, causing indefinite waiting.
Final Answer:
A situation where two or more transactions wait indefinitely for each other to release locks. -> Option D
Quick Check:
Deadlock = cyclic waiting [OK]
Hint: Deadlock means transactions wait forever on each other [OK]
Common Mistakes:
Confusing deadlock with syntax errors
Thinking deadlock improves performance
Mixing deadlock with backup locking
2. Which of the following is the correct way to acquire locks to prevent deadlocks in PostgreSQL?
easy
A. Acquire locks on resources in random order.
B. Acquire locks on resources in the same order in all transactions.
C. Never acquire any locks in transactions.
D. Acquire locks only after committing the transaction.
Solution
Step 1: Understand lock acquisition order
Acquiring locks in a consistent order prevents circular waiting.
Step 2: Identify correct practice
All transactions should acquire locks on resources in the same order to avoid deadlocks.
Final Answer:
Acquire locks on resources in the same order in all transactions. -> Option B
Quick Check:
Consistent lock order = no deadlock [OK]
Hint: Always lock resources in the same order [OK]
Common Mistakes:
Locking resources randomly
Not locking resources at all
Locking after commit
3. Consider two transactions in PostgreSQL: -- Transaction 1 BEGIN; UPDATE accounts SET balance = balance - 100 WHERE id = 1; UPDATE accounts SET balance = balance + 50 WHERE id = 2; -- waits here
-- Transaction 2 BEGIN; UPDATE accounts SET balance = balance + 100 WHERE id = 2; UPDATE accounts SET balance = balance - 50 WHERE id = 1; -- waits here What will PostgreSQL do when both transactions wait for each other?
medium
A. Both transactions will wait forever causing a deadlock.
B. Both transactions will succeed without any issue.
C. PostgreSQL will detect the deadlock and abort one transaction automatically.
D. PostgreSQL will merge both transactions into one.
Solution
Step 1: Identify deadlock scenario
Both transactions hold locks and wait for the other's lock, creating a cycle.
Step 2: PostgreSQL deadlock detection
PostgreSQL automatically detects deadlocks and aborts one transaction to break the cycle.
Final Answer:
PostgreSQL will detect the deadlock and abort one transaction automatically. -> Option C
Quick Check:
Deadlock detected = abort one transaction [OK]
Hint: PostgreSQL aborts one transaction on deadlock detection [OK]
Common Mistakes:
Assuming infinite waiting without abort
Thinking transactions merge automatically
Believing both succeed without conflict
4. You have the following PostgreSQL code causing a deadlock:
BEGIN; LOCK TABLE orders IN ACCESS EXCLUSIVE MODE; UPDATE customers SET name = 'Alice' WHERE id = 1; -- Transaction 2 starts here BEGIN; LOCK TABLE customers IN ACCESS EXCLUSIVE MODE; UPDATE orders SET status = 'shipped' WHERE id = 10;
What is the main issue causing the deadlock?
medium
A. Transactions lock tables in different orders causing circular wait.
B. Using SHARE MODE lock instead of EXCLUSIVE MODE.
C. Updating different tables in the same transaction.
D. Missing COMMIT statements after updates.
Solution
Step 1: Analyze lock order
Transaction 1 locks orders first, then updates customers; Transaction 2 locks customers first, then updates orders.
Step 2: Identify circular wait
Each transaction waits for the other's locked table, causing deadlock due to different lock order.
Final Answer:
Transactions lock tables in different orders causing circular wait. -> Option A
Quick Check:
Different lock order = deadlock risk [OK]
Hint: Lock tables in same order to avoid deadlock [OK]
Common Mistakes:
Blaming lock mode instead of order
Thinking updating different tables causes deadlock
Ignoring missing COMMIT as cause
5. You want to prevent deadlocks in a multi-user PostgreSQL system updating inventory and sales tables. Which strategy is best?
hard
A. Keep transactions short and acquire locks on inventory then sales in all transactions.
B. Acquire locks on sales first, then inventory, but only in some transactions.
C. Avoid using transactions to prevent locking.
D. Use long transactions to batch updates and reduce lock frequency.
Solution
Step 1: Understand deadlock prevention
Keeping transactions short reduces lock time; consistent lock order prevents cycles.
Step 2: Apply best practice
Always lock inventory first, then sales, in all transactions to avoid deadlocks.
Final Answer:
Keep transactions short and acquire locks on inventory then sales in all transactions. -> Option A
Quick Check:
Short transactions + consistent lock order = deadlock prevention [OK]
Hint: Short transactions + consistent lock order prevent deadlocks [OK]