Transaction isolation levels and MVCC in PostgreSQL and InnoDB
Reproducing Non-Repeatable Read and Lost Update across two sessions, and why MVCC makes REPEATABLE READ behave differently in PostgreSQL and InnoDB.
Two databases can share the exact same isolation level name and still give you completely different concurrency guarantees. This post reproduces read and write anomalies across two sessions and explains why PostgreSQL and InnoDB handle
REPEATABLE READdifferently under the hood.
Tested with PostgreSQL 15.4 (default:
READ COMMITTED) and MySQL 8.0.34 InnoDB (default:REPEATABLE READ).
Background
When I first memorized the SQL isolation level matrix in a database class, my takeaway was simple: “REPEATABLE READ prevents non-repeatable reads, though phantom reads can still happen.” Later, I ran the exact same two-session test script against PostgreSQL and MySQL in a study group, and the two engines produced opposite outcomes. PostgreSQL aborted the transaction with a serialization error, while MySQL committed silently and overwrote the value.
That discrepancy exists because the SQL standard only specifies which anomalies an isolation level must forbid—it leaves the storage and concurrency mechanism up to each engine. Both PostgreSQL and InnoDB rely on MVCC (Multi-Version Concurrency Control), yet they store old row versions in different places and resolve concurrent write conflicts in fundamentally different ways.
How MVCC works under the hood
The core idea behind MVCC is keeping multiple versions of a row so readers never block writers and writers never block readers. Each transaction picks the row version that is visible according to its own snapshot.
flowchart TD
A["Transaction reads a row"] --> B{"Storage engine"}
B -->|"PostgreSQL"| C["Compare heap tuple xmin and xmax against snapshot"]
B -->|"InnoDB"| D["Compare latest DB_TRX_ID against Read View"]
D --> E["If not visible, follow DB_ROLL_PTR in undo log"]
C --> F["Return visible row version"]
E --> F
PostgreSQL doesn’t update tuples in place. Every UPDATE writes a brand-new tuple into the heap and marks the old tuple as expired. Each tuple header stores xmin (the transaction ID that created it) and xmax (the transaction ID that deleted or replaced it). A snapshot records (xmin, xmax, active transaction list). When a transaction scans a tuple, it checks whether xmin was committed before the snapshot and xmax is either empty or not yet visible. Old versions stay in the heap until VACUUM reclaims them.
InnoDB updates the row directly inside the clustered index and writes the previous version to the undo log. Every row carries two hidden system columns: DB_TRX_ID (the last transaction that modified the row) and DB_ROLL_PTR (a pointer to the previous record in the undo log). When a query reads a row whose DB_TRX_ID isn’t visible to its Read View, InnoDB walks the undo chain backward until it reconstructs a visible version. Once no active Read View needs those old records, background purge threads clean them up.
Snapshot timing also matters more than most people expect. Under READ COMMITTED, the engine takes a fresh snapshot for every statement. Under REPEATABLE READ, it creates one snapshot at the first read statement and reuses it until the transaction ends. Note that the snapshot isn’t created at BEGIN—it starts on the first query, which trips people up constantly when running two-session experiments.
PostgreSQL vs. InnoDB comparison
| Aspect | PostgreSQL 15 | MySQL 8.0 InnoDB |
|---|---|---|
| Default isolation level | READ COMMITTED | REPEATABLE READ |
| Old version storage | Heap (append-only tuple) | Undo log |
| Cleanup mechanism | VACUUM / autovacuum | Purge threads |
READ UNCOMMITTED | Behaves like READ COMMITTED | Allows dirty reads |
| Concurrent update conflict in RR | Throws could not serialize access | Updates against latest committed row |
| Phantoms in RR | Prevented by snapshot reads | Snapshot reads prevent them; locking reads use gap locks |
The biggest behavioral split shows up in how REPEATABLE READ handles concurrent write conflicts. In PostgreSQL, if your transaction tries to update a row that another transaction committed after your snapshot was taken, PostgreSQL rejects the update and asks the application to retry. In InnoDB, plain SELECT uses the Read View (consistent read), whereas UPDATE and SELECT ... FOR UPDATE perform a current read against the latest committed version. That means a plain SELECT and an UPDATE inside the same InnoDB transaction can see two different versions of the same row.
When not to raise the isolation level: Switching PostgreSQL to REPEATABLE READ or SERIALIZABLE makes serialization failures (SQLSTATE 40001) a normal part of operation. If your application doesn’t have retry logic for 40001, raising the isolation level turns concurrency into user-facing errors. Long-running REPEATABLE READ transactions also pin old snapshots, blocking VACUUM in PostgreSQL or purge in InnoDB and driving table bloat or undo log growth. Bumping the isolation level on simple read-only APIs usually hurts more than it helps.
Reproducing anomalies locally
Set up a minimal account table to test both engines side by side:
1
2
CREATE TABLE account (id int PRIMARY KEY, balance int NOT NULL);
INSERT INTO account VALUES (1, 1000);
First, reproduce a Non-Repeatable Read under PostgreSQL’s default READ COMMITTED using two terminal sessions:
1
2
3
4
5
6
7
8
9
10
-- Session A (READ COMMITTED)
BEGIN;
SELECT balance FROM account WHERE id = 1; -- returns 1000
-- Session B
UPDATE account SET balance = 900 WHERE id = 1; -- auto-committed
-- Session A
SELECT balance FROM account WHERE id = 1; -- returns 900 (new snapshot per statement)
COMMIT;
If you change Session A to BEGIN ISOLATION LEVEL REPEATABLE READ, the second SELECT still returns 1000. Next, test a Lost Update scenario across two REPEATABLE READ sessions in PostgreSQL:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
-- Reset balance
UPDATE account SET balance = 1000 WHERE id = 1;
-- Session A
BEGIN ISOLATION LEVEL REPEATABLE READ;
SELECT balance FROM account WHERE id = 1; -- returns 1000; snapshot starts here
-- Session B
BEGIN ISOLATION LEVEL REPEATABLE READ;
UPDATE account SET balance = balance - 300 WHERE id = 1;
COMMIT;
-- Session A
UPDATE account SET balance = balance - 500 WHERE id = 1;
-- ERROR: could not serialize access due to concurrent update
ROLLBACK;
Now run the exact same sequence in MySQL 8.0 InnoDB. Session A’s SELECT continues to show 1000, yet its UPDATE performs a current read on the latest committed value (700), subtracts 500, and commits 200. While relative arithmetic (balance - 500) survives here, most application code reads 1000 into memory, computes 1000 - 500 = 500, and sends SET balance = 500. That silently wipes out Session B’s -300 deduction—a classic Lost Update under InnoDB REPEATABLE READ.
You can inspect active snapshots and long-running transactions on both engines with these diagnostic queries:
1
2
3
4
5
6
7
8
9
10
11
-- MySQL: check current isolation level and active transactions
SELECT @@transaction_isolation;
SELECT trx_id, trx_state, trx_started, trx_isolation_level
FROM information_schema.innodb_trx;
-- PostgreSQL: check current snapshot and oldest active transactions
SELECT pg_current_snapshot();
SELECT pid, xact_start, state, backend_xmin
FROM pg_stat_activity
WHERE backend_xmin IS NOT NULL
ORDER BY xact_start;
To prevent Lost Updates in MySQL, either lock the row when reading it via SELECT ... FOR UPDATE or use optimistic locking with a version column. For a closer look at what range those locks actually hold, see InnoDB row locks, next-key locks, and deadlock diagnosis.
Production pitfalls and checklist
Under InnoDB
REPEATABLE READ, the Read View isn’t created atSTART TRANSACTION—it’s created on the firstSELECT. If you need a snapshot anchored to the exact start of the transaction, runSTART TRANSACTION WITH CONSISTENT SNAPSHOT. Also watchHistory list lengthinSHOW ENGINE INNODB STATUS: a steadily climbing number means a long-running uncommitted transaction is holding back purge.
Knowing when your engine takes a snapshot and what it does on a write conflict is far more useful than memorizing the textbook isolation table. Before shipping concurrent write paths, I check these four items:
- Do you know your database’s default isolation level (
READ COMMITTEDin PostgreSQL,REPEATABLE READin MySQL)? - Does every read-modify-write path use a row lock or an optimistic
versioncheck? - If you use
REPEATABLE READorSERIALIZABLEin PostgreSQL, does the caller retry onSQLSTATE 40001? - Are long-running transactions monitored through
pg_stat_activityorinformation_schema.innodb_trx?