Bird
0
0

Consider this sequence in PostgreSQL:

medium📝 query result Q13 of 15
PostgreSQL - Transactions and Concurrency
Consider this sequence in PostgreSQL:
BEGIN;
SELECT * FROM products WHERE id = 1;
UPDATE products SET price = 20 WHERE id = 1;
COMMIT;

What does the SELECT see if another transaction updated the same row before this transaction started?
AAn error due to concurrent update
BThe new price updated by the other transaction
CNo rows returned because of the update
DThe old price before the other transaction's update
Step-by-Step Solution
Solution:
  1. Step 1: Understand snapshot isolation in MVCC

    The SELECT sees data as it was at transaction start, ignoring later committed changes.
  2. Step 2: Apply to given scenario

    Since another transaction updated before this one started, the snapshot at start excludes that committed update, so SELECT sees the old price.
  3. Final Answer:

    The old price before the other transaction's update -> Option D
  4. Quick Check:

    MVCC snapshot = data at tx start [OK]
Quick Trick: SELECT sees snapshot at transaction start, not later changes [OK]
Common Mistakes:
  • Expecting an error due to concurrent update
  • Thinking SELECT sees the old price before the other transaction's update
  • Thinking no rows returned because of the update

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes