Scrivio Interview library
InternalsIntermediate6 min read · updated 12 November 2026

How does PostgreSQL's REPEATABLE READ differ from MySQL InnoDB's REPEATABLE READ when two transactions update the same row?

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.

PostgreSQL's REPEATABLE READ is snapshot isolation: if another transaction committed an update to the same row after our snapshot was taken, our UPDATE fails with a serialization error, SQLSTATE 40001, and we must retry. InnoDB reads from a snapshot only for plain SELECTs; UPDATEs and locking reads use the latest committed version, so the second update simply waits for the lock and then proceeds on the newer data. InnoDB also takes next-key and gap locks at REPEATABLE READ, which can cause unexpected lock waits and deadlocks, and on both databases Spring translates these errors to concurrency failure exceptions that our code should retry.

Background to this question

Both databases call their default or near-default level REPEATABLE READ, so it is natural to assume they behave the same. They do not, and the difference shows up exactly when two transactions update the same row, which is when correctness matters most. Teams that move a service from MySQL to PostgreSQL, or the other way, meet this as new errors or as silently lost updates. In this answer, we will recap snapshots, run the same two-session scenario on each database, explain the InnoDB locks that surprise people, and finish with how to handle the errors in Spring.

1A Quick Recap: Snapshots and Current Reads

A snapshot is a consistent view of the database as of one moment. Under REPEATABLE READ, both PostgreSQL and InnoDB give the transaction a snapshot taken at its first statement, so plain SELECTs keep returning the same data however many other transactions commit.

The difference is what happens when we write. A write must act on a real row version, not on a view of the past. PostgreSQL insists that the row we update is still the version our snapshot saw; InnoDB instead performs a current read, reading the latest committed version and locking it, for every UPDATE, DELETE and SELECT ... FOR UPDATE.

2The Same Scenario on PostgreSQL

Two sessions both read a product's stock and then decrement it. The stock starts at 10.

SQL
-- PostgreSQL, both sessions REPEATABLE READ
-- Session A -- Session B
BEGIN ISOLATION LEVEL REPEATABLE READ;
SELECT stock FROM product WHERE id = 1; -- 10
BEGIN ISOLATION LEVEL REPEATABLE READ;
SELECT stock FROM product WHERE id = 1; -- 10
UPDATE product SET stock = 9 WHERE id = 1;
UPDATE product SET stock = 9 WHERE id = 1;
-- waits for A's row lock
COMMIT;
-- ERROR: could not serialize access due to concurrent update
-- SQLSTATE 40001; B must ROLLBACK and retry

Session B's snapshot says the stock is 10, but A has committed a newer version since B's snapshot began. PostgreSQL refuses to let B write on top of a row it never saw, so B fails. On retry, B starts a new snapshot, reads 9 and writes 8. The lost update is prevented, at the price of a retry.

3The Same Scenario on InnoDB

Now the same steps on MySQL 8.4 with InnoDB's default REPEATABLE READ.

SQL
-- MySQL InnoDB, REPEATABLE READ
-- Session A -- Session B
START TRANSACTION;
SELECT stock FROM product WHERE id = 1; -- 10
START TRANSACTION;
SELECT stock FROM product WHERE id = 1; -- 10
UPDATE product SET stock = 9 WHERE id = 1;
UPDATE product SET stock = 9 WHERE id = 1;
-- waits for A's row lock
COMMIT;
-- proceeds: Query OK, rows affected
COMMIT;
SELECT stock FROM product WHERE id = 1; -- 9: one sale was lost
Same row, two REPEATABLE READs
BehaviourPostgreSQLMySQL InnoDB
Plain SELECTSnapshot of the transactionSnapshot of the transaction
UPDATE of a row changed since the snapshotFails with SQLSTATE 40001Waits, then updates the latest version
Lost update from read-then-writePrevented by the errorPossible without FOR UPDATE or a version
Range locking for readsNone at this levelNext-key and gap locks on locking reads
What the application must doRetry on 40001Use atomic updates, FOR UPDATE or versions

B's UPDATE waited for the lock and then ran against the latest committed row. Nothing failed, so B's write of 9, computed from the stale 10, overwrote A's change. Two sales, one decrement. Notice also that, before its update, B's plain SELECT kept showing 10 even after A had committed 9, because plain reads stay on the snapshot while writes see current data, a mix that surprises people.

With InnoDB, we must avoid read-then-write on stale values: write UPDATE product SET stock = stock - 1 so the database computes from the current value, read with SELECT ... FOR UPDATE, or use a version column for optimistic locking.

4InnoDB's Next-Key and Gap Locks

To stop phantoms for locking reads and writes, InnoDB at REPEATABLE READ locks not only the rows it finds but also the gaps between index entries around them. A next-key lock is a row lock plus a lock on the gap before it. An UPDATE ... WHERE status = 'NEW' without a good index may scan and lock a large range, and other transactions inserting into that range wait.

This explains lock waits that seem impossible: two transactions inserting different rows block each other because both fall into a locked gap. And because gap locks are taken by both sides of a conflict, two transactions can each hold a gap lock and wait for the other's insert intention lock, which InnoDB detects and resolves by rolling one back with error 1213, Deadlock found when trying to get lock. Good indexes on the columns we filter by, and READ COMMITTED for workloads that do not need gap locking, reduce this.

SQL
-- Session A: locks the gap where order_no 105 would go
SELECT * FROM orders WHERE order_no BETWEEN 100 AND 110 FOR UPDATE;
-- Session B:
INSERT INTO orders(order_no) VALUES (105); -- waits for A, even though 105 does not exist yet

5Handling the Errors in Spring

On PostgreSQL at REPEATABLE READ or SERIALIZABLE, serialization failures are normal and the application must retry the whole transaction. On InnoDB, deadlocks must be retried the same way. Spring translates both into subclasses of ConcurrencyFailureException, such as PessimisticLockingFailureException or CannotAcquireLockException; the exact subclass depends on the database and Spring version, so check yours and catch the common parent.

Java
@Retryable(retryFor = ConcurrencyFailureException.class, maxAttempts = 3) // Spring Retry; check your version
public void sell(long productId) {
stockService.decrement(productId); // @Transactional on another bean
}

The retry must wrap the whole transaction, outside the @Transactional boundary, because a transaction that hit a serialization failure is finished; only a new one, with a new snapshot, can succeed. Spring Framework 7 also has its own @Retryable in the core resilience support; check your version for which one you use.

6Follow-up questions

Why does PostgreSQL wait before failing B?

Because A might roll back. If A rolls back, B's snapshot is still the latest version and B's update can proceed. Only when A commits does PostgreSQL know B would be writing on top of a version it never saw, so it fails B then.

Is InnoDB REPEATABLE READ wrong, then?

Not wrong, but different. It provides consistent reads and phantom protection for locking reads, while letting writes proceed on current data. It means lost updates must be prevented by the application with atomic updates, locking reads or version columns.

Would READ COMMITTED behave the same on both?

Much closer. At READ COMMITTED both databases let B's UPDATE proceed on the latest version after A commits, so both allow lost updates from read-then-write, and neither raises a serialization error.

7Conclusion

In this answer, we ran the same two updates on both databases. PostgreSQL's REPEATABLE READ is snapshot isolation, so the second writer fails with SQLSTATE 40001 and must retry, which prevents lost updates. InnoDB's REPEATABLE READ keeps snapshots for plain reads but performs writes on the latest committed version, so the second update waits and then proceeds, and lost updates are possible. InnoDB also takes next-key and gap locks that cause surprising waits and deadlocks, and Spring translates these failures into ConcurrencyFailureException subclasses that we retry around the whole transaction.

What to say in the interviewAbout 80 seconds aloud

Open with

Both give each transaction a snapshot for plain SELECTs, but they differ on writes. PostgreSQL's REPEATABLE READ is true snapshot isolation. If two transactions update the same row and the first commits, the second one, whose snapshot predates that commit, fails with could not serialize access due to concurrent update, SQLSTATE 40001, and has to roll back and retry.

Then, if they want more

That prevents lost updates. InnoDB does a current read for UPDATE, DELETE and locking SELECTs: the second update waits for the row lock, then runs against the latest committed version without any error. So if the application read stock 10 and writes 9 in both transactions, one sale is lost on MySQL, whereas PostgreSQL would have refused it.

On InnoDB I'd use atomic updates like stock = stock - 1, SELECT FOR UPDATE, or optimistic versioning. InnoDB also uses next-key and gap locks at REPEATABLE READ for locking reads and writes, so inserts into a locked range wait, and you can get deadlocks, error 1213, that look impossible from the SQL; good indexes help.

In Spring, both serialization failures and deadlocks come through as ConcurrencyFailureException subclasses, and I'd retry the whole transaction from outside the transactional method, a few attempts with backoff.

About this answer

Last updated
12 November 2026
Versions it names
MySQL 8.4, Spring Framework 7
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 "How does PostgreSQL's REPEATABLE READ differ from MySQL InnoDB's REPEATABLE READ when...": https://scrivio-interview-library.netlify.app/databases/transactions/d416b0dd48/