Quick revision
The short answer
At READ COMMITTED, session A's first SELECT returns 100 and its second returns 50, because each statement takes a fresh snapshot and B's update committed in between. This is a non-repeatable read. At REPEATABLE READ, A keeps the snapshot from its first statement, so both SELECTs return 100, and an UPDATE of that row inside A would fail with a serialization error that must be retried.
What to remember
In this answer, we saw that at READ COMMITTED each statement in PostgreSQL takes its own snapshot, so session A reads 100 and then 50, a non-repeatable read. At REPEATABLE READ the snapshot is fixed by the first statement, so both reads return 100. When A then updates the row, READ COMMITTED re-reads the latest version and writes 40, while REPEATABLE READ refuses with SQLSTATE 40001 and the transaction has to be retried.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
At READ COMMITTED, session A's first SELECT returns 100 and its second returns 50, because each statement takes a fresh snapshot and B's update committed in between. This is a non-repeatable read. At REPEATABLE READ, A keeps the snapshot from its first statement, so both SELECTs return 100, and an UPDATE of that row inside A would fail with a serialization error that must be retried.
Why this question is asked
Isolation levels are easy to recite and surprisingly hard to predict in practice. Interviewers like this small two-session puzzle because it shows whether we know when PostgreSQL takes a snapshot, what a non-repeatable read looks like, and what happens when a transaction tries to write a row that changed under it. Every service that reads a value and then acts on it depends on this behaviour. In this answer, we will recap what a snapshot is, run the scenario at READ COMMITTED and at REPEATABLE READ, and then see what an UPDATE inside session A does at each level.
1A Quick Recap of Snapshots in PostgreSQL
PostgreSQL uses multi-version concurrency control, usually shortened to MVCC. When a row is updated, PostgreSQL does not overwrite it. It writes a new version of the row and keeps the old one for a while. A snapshot is the rule a statement uses to decide which of those versions it may see: versions written by transactions that had committed when the snapshot was taken are visible, and everything else is not.
The isolation level decides when the snapshot is taken. At READ COMMITTED, the PostgreSQL default, every statement takes a new snapshot when it starts. At REPEATABLE READ, the transaction takes one snapshot at its first statement and uses it until it ends. Notice that it is the first statement, not the BEGIN, that fixes the snapshot. That one difference explains the whole puzzle.
2Running the Scenario at READ COMMITTED
Here is the timeline from the question, written as two psql sessions side by side. Session B runs in autocommit mode, so its UPDATE commits as soon as it finishes.
6 messages in order. Play them, or step through them one at a time.
Each statement in A sees whatever had committed when that statement began.
The first SELECT takes a snapshot in which only the version with balance 100 exists. The second SELECT takes a new snapshot, and by then B's version with balance 50 has committed, so it is visible. The same query, in the same transaction, returned two different values. That is exactly what a non-repeatable read means.
This is not a bug in PostgreSQL. READ COMMITTED promises only that we never see uncommitted data, and both values we saw were committed. If our code read the balance once to make a decision and again to display it, the two could disagree.
3Running It Again at REPEATABLE READ
Now session A starts with BEGIN ISOLATION LEVEL REPEATABLE READ, and everything else stays the same.
Both SELECTs return 100. The second statement reuses the snapshot taken by the first, and in that snapshot B's transaction had not committed yet. So B's new version is invisible to A for the rest of A's transaction, even though it is committed and every other new session would see 50.
There is a catch worth knowing: if A had run BEGIN ISOLATION LEVEL REPEATABLE READ, waited, and only then run its first SELECT after B committed, it would have seen 50. The snapshot belongs to the first statement, not to the BEGIN.
4What Happens When A Tries to Update the Row
Reading an old snapshot is harmless. Writing based on it is not. Suppose session A, after its two SELECTs, runs UPDATE account SET balance = balance - 10 WHERE id = 1. The two isolation levels now behave very differently.
| Step in A | READ COMMITTED | REPEATABLE READ |
|---|---|---|
| First SELECT | 100 | 100 |
| Second SELECT after B commits | 50 (non-repeatable read) | 100 (same snapshot) |
| UPDATE balance = balance - 10 | succeeds, row becomes 40 | ERROR 40001, retry needed |
At REPEATABLE READ, A finds that the row version it would update is not the latest one: a transaction that committed after A's snapshot changed it. If PostgreSQL allowed the update, A would subtract 10 from a value it believes is 100 while the real value is 50, and B's change could be lost. So it aborts A with SQLSTATE 40001. The application must roll back and retry the whole transaction, and on the retry the new snapshot will show 50.
At READ COMMITTED, the UPDATE does not need to wait for any lock, because B has already committed and released its row lock. PostgreSQL re-reads the latest committed version of the row, which is 50, applies balance - 10 to it, and writes 40. If B had still been in progress, A's UPDATE would have waited for B to finish and then done the same re-read. That re-check keeps relative updates like balance = balance - 10 correct, but it does not protect code that read the value earlier and writes an absolute number.
5Choosing Between the Two Levels
READ COMMITTED is a good default for short transactions that do their writes with relative updates and conditions in SQL. REPEATABLE READ is the right choice when a transaction must see one consistent picture, such as a report that reads several tables, or when we would rather get an error than silently act on stale data. The price of REPEATABLE READ is that every transaction that writes must be ready for SQLSTATE 40001 and retried from the start, so the transaction should be short and free of side effects such as sending emails.
6Follow-up questions
In MySQL InnoDB at REPEATABLE READ, what would the UPDATE do instead?
InnoDB does not raise an error. Its plain SELECTs read from a consistent snapshot, so both SELECTs in A return 100, just as in PostgreSQL. But an UPDATE in InnoDB is a current read: it locks and reads the latest committed version of the row, which is 50, and writes 40 without complaint. Even stranger, A's next plain SELECT returns 40, because a transaction always sees its own changes. So InnoDB lets the update through on the latest data, which is why its REPEATABLE READ is not true snapshot isolation.
When exactly does a REPEATABLE READ transaction take its snapshot?
At its first statement that needs one, usually the first SELECT, INSERT, UPDATE or DELETE, not at BEGIN. Commands like SET do not take it. If we need the snapshot to start at a precise moment, we run the first query immediately after BEGIN.
Is a dirty read possible in PostgreSQL at any level?
No. PostgreSQL accepts READ UNCOMMITTED as a name but treats it as READ COMMITTED, so a transaction can never see another transaction's uncommitted changes.
7Conclusion
In this answer, we saw that at READ COMMITTED each statement in PostgreSQL takes its own snapshot, so session A reads 100 and then 50, a non-repeatable read. At REPEATABLE READ the snapshot is fixed by the first statement, so both reads return 100. When A then updates the row, READ COMMITTED re-reads the latest version and writes 40, while REPEATABLE READ refuses with SQLSTATE 40001 and the transaction has to be retried.
Open with
At READ COMMITTED, PostgreSQL takes a new snapshot for every statement. So A's first SELECT sees 100, B's autocommit update commits, and A's second SELECT takes a fresh snapshot and sees 50.
Then, if they want more
That's a non-repeatable read, and READ COMMITTED allows it because both values were committed. If A starts with REPEATABLE READ, the snapshot is taken at the first statement and kept for the whole transaction, so both SELECTs return 100, even though 50 is committed.
The interesting part is what happens if A then updates the row with balance = balance - 10.
At REPEATABLE READ, PostgreSQL sees that the row was changed by a transaction that committed after A's snapshot, and it fails with could not serialize access due to concurrent update, SQLSTATE 40001, so the application has to roll back and retry the whole transaction.
At READ COMMITTED, B has already committed, so there's no lock to wait for; PostgreSQL re-reads the latest version, 50, and writes 40. In MySQL InnoDB at REPEATABLE READ, the update would instead act on the latest committed row and write 40 without an error, which is one reason InnoDB's REPEATABLE READ isn't true snapshot isolation.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Two PostgreSQL sessions run at READ COMMITTED on table account(id int primary key...": https://scrivio-interview-library.netlify.app/databases/acid-and-isolation/2bbf4298f0/