Bird
0
0

How can you prevent inconsistent intermediate states without changing the isolation level?

hard📝 Application Q9 of 15
PostgreSQL - Transactions and Concurrency
A transaction under Read Committed isolation runs multiple queries that depend on each other's results. Another transaction updates some rows and commits between these queries. How can you prevent inconsistent intermediate states without changing the isolation level?
ASet the transaction to autocommit mode.
BUse explicit row-level locks with SELECT FOR UPDATE in the first query.
CRestart the transaction after each query.
DUse advisory locks unrelated to rows.
Step-by-Step Solution
Solution:
  1. Step 1: Identify cause of inconsistency

    Other transactions committing changes between queries cause inconsistent reads.
  2. Step 2: Apply explicit locking to prevent concurrent updates

    Using SELECT FOR UPDATE locks rows so others cannot modify them until transaction ends.
  3. Final Answer:

    Use explicit row-level locks with SELECT FOR UPDATE in the first query. -> Option B
  4. Quick Check:

    Row locks prevent concurrent changes under Read Committed [OK]
Quick Trick: Lock rows early to avoid mid-transaction changes [OK]
Common Mistakes:
  • Restarting transactions wastes resources
  • Autocommit does not solve consistency
  • Advisory locks do not lock rows automatically

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes