Bird
0
0

You have the following PostgreSQL code causing a deadlock:

medium📝 Debug Q14 of 15
PostgreSQL - Transactions and Concurrency
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?
ATransactions lock tables in different orders causing circular wait.
BUsing SHARE MODE lock instead of EXCLUSIVE MODE.
CUpdating different tables in the same transaction.
DMissing COMMIT statements after updates.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze lock order

    Transaction 1 locks orders first, then updates customers; Transaction 2 locks customers first, then updates orders.
  2. Step 2: Identify circular wait

    Each transaction waits for the other's locked table, causing deadlock due to different lock order.
  3. Final Answer:

    Transactions lock tables in different orders causing circular wait. -> Option A
  4. Quick Check:

    Different lock order = deadlock risk [OK]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes