Bird
0
0

Given two transactions:

medium📝 query result Q5 of 15
PostgreSQL - Transactions and Concurrency
Given two transactions:
-- Transaction 1
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;

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

What is the risk of deadlock if both transactions update each other's rows in reverse order?
AHigh risk of deadlock due to conflicting row locks
BNo risk because updates are on different rows
CSyntax error will prevent deadlock
DDeadlock only occurs with table locks
Step-by-Step Solution
Solution:
  1. Step 1: Understand row-level locking in updates

    Each UPDATE locks the row it modifies. If transactions update rows in reverse order, they wait on each other's locks.
  2. Step 2: Recognize deadlock risk

    This waiting cycle creates a deadlock risk, so High risk of deadlock due to conflicting row locks is correct.
  3. Final Answer:

    High risk of deadlock due to conflicting row locks -> Option A
  4. Quick Check:

    Row lock conflicts = Deadlock risk [OK]
Quick Trick: Updating rows in reverse order risks deadlock [OK]
Common Mistakes:
  • Thinking different rows mean no deadlock
  • Confusing syntax errors with deadlocks
  • Assuming deadlocks only happen with table locks

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes