Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Two PostgreSQL sessions run at READ COMMITTED on table account(id int primary key, balance int) with id 1 balance 100. Session A:

SQL
BEGIN;
SELECT balance
FROM account
WHERE id = 1;

Session B:

SQL
UPDATE account
SET balance = 50
WHERE id = 1;

(autocommit). Session A:

SQL
SELECT balance
FROM account
WHERE id = 1;

COMMIT. What does each SELECT in A return, and what changes if A starts with BEGIN ISOLATION LEVEL REPEATABLE READ?

Try it yourself

Isolation levels: what a second read sees

Step through two transactions and see exactly when read committed and repeatable read give different answers.

Read this example as text

T1 at read committed

  1. T1 begins. Transaction T1 starts with isolation level READ COMMITTED. The row holds balance 100.
  2. T1 reads the balance. T1 reads 100. Under read committed each statement looks afresh at what is committed when the statement starts.
  3. T2 changes the row. A second transaction, T2, sets the balance to 150 but has not committed. The database keeps both versions: 100 is still the committed one.
  4. T1 reads again, before T2 commits. T1 reads 100 again. Neither level shows another transaction's uncommitted change, so there is no dirty read. T1 does not wait for T2 either.
  5. T2 commits. T2 commits. The committed balance is now 150.
  6. T1 reads a third time. T1 reads 150. The same query, inside one transaction, returned 100 and then 150. This is a non-repeatable read, and read committed allows it.
  7. T1 commits. T1 ends. A transaction that starts now reads 150 at either level.

T1 read 100, 100, then 150: a non-repeatable read.

T1 at repeatable read

  1. T1 begins. Transaction T1 starts with isolation level REPEATABLE READ. The row holds balance 100.
  2. T1 reads the balance. T1 reads 100. Under repeatable read this first read fixes T1's snapshot: T1 will keep seeing the data as it was committed at this moment.
  3. T2 changes the row. A second transaction, T2, sets the balance to 150 but has not committed. The database keeps both versions: 100 is still the committed one.
  4. T1 reads again, before T2 commits. T1 reads 100 again. Neither level shows another transaction's uncommitted change, so there is no dirty read. T1 does not wait for T2 either.
  5. T2 commits. T2 commits. The committed balance is now 150.
  6. T1 reads a third time. T1 still reads 100. Its snapshot was fixed at its first read, so T2's commit is invisible to it. The read is repeatable.
  7. T1 commits. T1 ends. A transaction that starts now reads 150 at either level.

T1 read 100 three times: its reads were repeatable.

What this example leaves out
  • Modelled on PostgreSQL and MySQL InnoDB, where a plain SELECT reads a snapshot and never waits for a writer.
  • SQL Server's default read committed uses locks instead: there, T1's second read would wait for T2 to finish.
  • Only reads are compared. What happens when T1 also writes the row (a lost update, or an error in PostgreSQL) is not modelled.

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.

SQL
-- Session A -- Session B
BEGIN; -- READ COMMITTED by default
SELECT balance FROM account WHERE id = 1; -- 100
UPDATE account SET balance = 50 WHERE id = 1;
-- UPDATE 1, committed at once
SELECT balance FROM account WHERE id = 1; -- 50
COMMIT;
Step throughSession A at READ COMMITTED
Session APostgreSQLSession B
SELECT balance (new snapshot)
100
UPDATE balance = 50, autocommit
UPDATE 1, committed
SELECT balance (another new snapshot)
50

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.

SQL
-- Session A -- Session B
BEGIN ISOLATION LEVEL REPEATABLE READ;
SELECT balance FROM account WHERE id = 1; -- 100, the snapshot is taken here
UPDATE account SET balance = 50 WHERE id = 1;
-- UPDATE 1, committed
SELECT balance FROM account WHERE id = 1; -- 100, same snapshot as before
COMMIT;

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.

SQL
-- At REPEATABLE READ, after B has committed balance = 50:
UPDATE account SET balance = balance - 10 WHERE id = 1;
-- ERROR: could not serialize access due to concurrent update (SQLSTATE 40001)
ROLLBACK; -- the transaction is aborted; we run it again from BEGIN
-- At READ COMMITTED, same timeline:
UPDATE account SET balance = balance - 10 WHERE id = 1;
-- UPDATE 1, no waiting: B already committed
SELECT balance FROM account WHERE id = 1; -- 40
COMMIT;
Session A's results by isolation level
Step in AREAD COMMITTEDREPEATABLE READ
First SELECT100100
Second SELECT after B commits50 (non-repeatable read)100 (same snapshot)
UPDATE balance = balance - 10succeeds, row becomes 40ERROR 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.

What to say in the interviewAbout 80 seconds aloud

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.
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/