Quick revision
The short answer
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.
What to remember
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.
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.
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.
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.
| Behaviour | PostgreSQL | MySQL InnoDB |
|---|---|---|
| Plain SELECT | Snapshot of the transaction | Snapshot of the transaction |
| UPDATE of a row changed since the snapshot | Fails with SQLSTATE 40001 | Waits, then updates the latest version |
| Lost update from read-then-write | Prevented by the error | Possible without FOR UPDATE or a version |
| Range locking for reads | None at this level | Next-key and gap locks on locking reads |
| What the application must do | Retry on 40001 | Use 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.
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.
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.
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.
- Official documentation
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
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/