Bird
0
0

Given this PostgreSQL transaction block:

medium📝 Debug Q14 of 15
PostgreSQL - Transactions and Concurrency

Given this PostgreSQL transaction block:

BEGIN;
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
UPDATE products SET stock = stock - 1 WHERE id = 10;
COMMIT;

After running this, you get an error: ERROR: could not serialize access due to concurrent update. What is the best way to fix this?

AIgnore the error and continue.
BRemove the COMMIT statement.
CChange isolation level to Read Uncommitted.
DRetry the entire transaction from the beginning.
Step-by-Step Solution
Solution:
  1. Step 1: Understand Serialization Failure Cause

    The error means a concurrent transaction caused a conflict; PostgreSQL aborts the transaction to maintain correctness.
  2. Step 2: Apply Recommended Fix

    The correct fix is to retry the entire transaction, as the conflict may not happen again on retry.
  3. Final Answer:

    Retry the entire transaction from the beginning. -> Option D
  4. Quick Check:

    Serialization errors require transaction retry [OK]
Quick Trick: On serialization error, retry transaction fully [OK]
Common Mistakes:
  • Ignoring the error and proceeding
  • Lowering isolation level unsafely
  • Removing COMMIT causing open transactions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes