Pick the fix
A report reads SUM(amount) twice in one transaction and gets two different totals. Smallest correct change in PostgreSQL?
Hint 1 of 1
How often does READ COMMITTED take a snapshot?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A Run the report at REPEATABLE READ so both reads use one snapshot
Correct. One snapshot per transaction makes reads repeatable, and read-only transactions never get 40001 at this level.
READ COMMITTED takes a new snapshot for every statement, so two SELECTs can see different data. REPEATABLE READ keeps one snapshot for the whole transaction.
Why not the others?
- B Add
FOR UPDATEto both SELECTs. Aggregates can't lock rows, and blocking writers to run a report is a heavy price. - C Use READ UNCOMMITTED. PostgreSQL treats it as READ COMMITTED; nothing changes.
- D Run both SELECTs in one statement with a CTE. Works for this pair, but isn't the general fix for multi-statement reports.