Bird
0
0

Given this transaction in PostgreSQL:

medium📝 Debug Q14 of 15
PostgreSQL - Transactions and Concurrency

Given this transaction in PostgreSQL:

BEGIN ISOLATION LEVEL REPEATABLE READ;
UPDATE products SET stock = stock - 1 WHERE id = 10;
SELECT stock FROM products WHERE id = 10;
COMMIT;

But you get an error: ERROR: syntax error at or near "ISOLATION". What is the fix?

AChange to <code>BEGIN; SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;</code>
BRemove the isolation level, just use <code>BEGIN;</code>
CUse <code>START TRANSACTION ISOLATION LEVEL REPEATABLE READ;</code>
DUse <code>SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL REPEATABLE READ;</code> before BEGIN
Step-by-Step Solution
Solution:
  1. Step 1: Identify syntax error cause

    PostgreSQL does not support specifying isolation level directly in BEGIN statement.
  2. Step 2: Correct syntax to set isolation level

    You must first BEGIN; then run SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; before queries.
  3. Final Answer:

    Change to BEGIN; SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; -> Option A
  4. Quick Check:

    Set isolation after BEGIN, not inside [OK]
Quick Trick: Set isolation after BEGIN with SET TRANSACTION [OK]
Common Mistakes:
  • Trying to put isolation level inside BEGIN
  • Using unsupported START TRANSACTION syntax
  • Setting session level instead of transaction level

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes