Sub topic 12
How PostgreSQL, MySQL, SQL Server and Oracle differ
This chapter assumes you know the four SQL standard isolation levels, the anomalies they are meant to prevent (dirty read, non-repeatable read, phantom, lost update, write skew), and the basic ideas of locking and MVCC (multiversion concurrency control). The standard describes behavior in terms of anomalies, but it does not say how to build an engine. Each product made its own choices. So the same level name can give different results depending on where you run it.
Why the names mislead
The standard says what a level must forbid. It does not say what it may not forbid. An engine can therefore give you more protection than the name promises, and many do. The difference shows up in three ways:
- Level mapping. A requested level may be silently upgraded to a stronger one.
- Mechanism. Locks, snapshots, or a mix of both produce different blocking and different errors.
- Statement versus transaction scope. A snapshot may be taken for each statement or once for the whole transaction.
A schedule that passes on one engine can fail on another. Code that sets REPEATABLE READ and works in PostgreSQL does not automatically behave the same way in MySQL.
PostgreSQL
PostgreSQL uses MVCC for everything. Readers never block writers and writers never block readers.
- READ UNCOMMITTED is accepted but behaves exactly like READ COMMITTED. PostgreSQL has no way to show uncommitted data, so dirty reads cannot happen at any level.
- READ COMMITTED (the default) takes a new snapshot for every statement. If an
UPDATEfinds a row that another transaction changed and committed after the statement began, it waits for that transaction, then re-checks the row'sWHEREcondition against the new version. - REPEATABLE READ is snapshot isolation. The snapshot is taken at the first statement of the transaction. Phantoms are impossible, which is stronger than the standard requires. Write skew is still possible. If your transaction tries to update a row that a concurrent transaction changed and committed since your snapshot, you get an error with SQLSTATE
40001instead of a lost update. - SERIALIZABLE uses Serializable Snapshot Isolation (SSI). It runs like snapshot isolation but tracks read-write dependencies between transactions. When it finds a pattern that could not occur in any serial order, it aborts one transaction with
40001.
SSI does not block while it works. It lets transactions run and fails some of them at the end. Your application must therefore retry.
MySQL InnoDB
InnoDB defaults to REPEATABLE READ, unlike the other three engines. It combines MVCC with locks, and it treats two kinds of reads differently.
- A consistent read is a plain
SELECT. It uses the snapshot established by the first consistent read in the transaction. - A locking read (
SELECT ... FOR UPDATE,SELECT ... FOR SHARE,UPDATE,DELETE) ignores the snapshot. It reads the latest committed data and takes locks.
This split has consequences. In one REPEATABLE READ transaction, a plain SELECT can show 100 while a following UPDATE operates on a row that is already 50. A plain read-modify-write can therefore lose an update, which PostgreSQL's REPEATABLE READ would reject. For locking reads, InnoDB uses next-key locks (a record lock plus a gap lock on the range before it). These block inserts into the scanned range and stop phantoms for locking reads.
Other points:
READ UNCOMMITTEDreally allows dirty reads in InnoDB.- In
SERIALIZABLE, with autocommit off, InnoDB converts plainSELECTstatements intoSELECT ... FOR SHARE. This is lock-based serializability, so it produces more blocking and more deadlocks than PostgreSQL's SSI. - When InnoDB detects a deadlock, it rolls back one transaction with error 1213 (SQLSTATE
40001).
SQL Server
SQL Server was built around locks, and its defaults reflect that.
- READ COMMITTED (the default) takes shared locks while reading and releases them right away. Readers block on uncommitted writes, and writers block on readers that hold a shared lock.
- READ COMMITTED SNAPSHOT (RCSI) is a database option, not a separate level. With
READ_COMMITTED_SNAPSHOT ON, the level named READ COMMITTED uses row versions and behaves like PostgreSQL'sREAD COMMITTED. Azure SQL Database turns this option on by default, while a typical on-premises database has it off. - SNAPSHOT is a separate level that must be enabled with
ALLOW_SNAPSHOT_ISOLATION ON. It gives transaction-wide snapshots. A conflicting update fails with error 3960. Write skew is possible. - REPEATABLE READ holds shared locks until commit. It does not prevent phantoms.
- SERIALIZABLE adds key-range locks, which prevent phantoms.
- READ UNCOMMITTED allows dirty reads. The
NOLOCKtable hint has the same effect for one table.
Deadlocks raise error 1205, and the engine picks a victim to roll back.
-- SQL Server: enable row versioning, then use it
ALTER DATABASE shop SET READ_COMMITTED_SNAPSHOT ON;
ALTER DATABASE shop SET ALLOW_SNAPSHOT_ISOLATION ON;
SET TRANSACTION ISOLATION LEVEL SNAPSHOT;
BEGIN TRANSACTION;
SELECT balance FROM accounts WHERE id = 1;
COMMIT;Oracle
Oracle supports only two real isolation levels, plus a read-only mode.
- READ COMMITTED (the default) takes a snapshot per statement, like PostgreSQL. Readers never block writers.
- SERIALIZABLE is snapshot isolation. The transaction sees data as of its start, and a conflicting update fails with
ORA-08177: can't serialize access for this transaction. Despite the name, write skew is possible. - READ ONLY gives a transaction-wide snapshot and forbids writes.
READ UNCOMMITTEDandREPEATABLE READare not available.
-- Oracle: must be the first statement of the transaction
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
SELECT balance FROM accounts WHERE id = 1;
COMMIT;Oracle deadlocks raise ORA-00060.
Side-by-side comparison
| Level | PostgreSQL | MySQL InnoDB | SQL Server | Oracle |
|---|---|---|---|---|
| READ UNCOMMITTED | Same as READ COMMITTED | Dirty reads allowed | Dirty reads allowed | Not available |
| READ COMMITTED | Default, snapshot per statement | Snapshot per statement | Default, locks (or versions with RCSI) | Default, snapshot per statement |
| REPEATABLE READ | Snapshot isolation, no phantoms | Default, snapshot plus locking reads | Shared locks held, phantoms possible | Not available |
| Snapshot-style level | REPEATABLE READ | REPEATABLE READ (consistent reads) | SNAPSHOT (must be enabled) | SERIALIZABLE |
| SERIALIZABLE | SSI, aborts with 40001 | Locking, plain reads become shared reads | Key-range locks | Snapshot isolation, write skew possible |
Behavior of one schedule on each engine
Take the write skew schedule from the earlier chapter. Two transactions each read the on-duty count, then each takes a different doctor off duty.
| Engine and level | Result |
|---|---|
| PostgreSQL REPEATABLE READ | Both commit, nobody on duty |
| PostgreSQL SERIALIZABLE | One transaction aborts with `40001` |
| MySQL REPEATABLE READ with plain `SELECT` | Both commit, nobody on duty |
| MySQL SERIALIZABLE | The reads take shared locks, so the two updates wait on each other and one is rolled back as a deadlock victim |
| SQL Server SNAPSHOT | Both commit |
| SQL Server SERIALIZABLE | Locks block, then one transaction is chosen as a deadlock victim |
| Oracle SERIALIZABLE | Both commit |
Only some paths detect the problem, and they do it in different ways. In some engines you get a serialization failure. In others you get a deadlock. In others you get nothing.
Checking what you actually have
Do not rely on defaults you have not verified. Query the current setting from the session.
-- PostgreSQL
SHOW transaction_isolation;
-- MySQL 8.0 and later
SELECT @@transaction_isolation;
-- SQL Server: is row versioning on for this database?
SELECT name, is_read_committed_snapshot_on, snapshot_isolation_state_desc
FROM sys.databases
WHERE name = DB_NAME();Practical rules for portable code
- Name the level you need explicitly at the start of the transaction. Do not rely on the default.
- For lost-update protection that works everywhere, use an atomic
UPDATE balance = balance + 10orSELECT ... FOR UPDATE. Both behave consistently across all four engines. - For write skew, use
SERIALIZABLEon PostgreSQL. On the others, lock the rows you read, or enforce the rule with a constraint or a single row that all the transactions must update. - Treat serialization failures (
40001, 3960,ORA-08177) and deadlocks (1213, 1205,ORA-00060) as expected events that trigger a retry of the whole transaction. - Test concurrency behavior on the engine you deploy to. A schedule that passes on one engine says little about another.
Exercise
A team moves an application from MySQL InnoDB to PostgreSQL and keeps REPEATABLE READ. The code runs SELECT balance, computes a new value in the application, then runs UPDATE accounts SET balance = <new value>. On MySQL this sometimes lost updates silently. What happens on PostgreSQL when two such transactions overlap, and what must the application now do?
Show answer
In PostgreSQL's REPEATABLE READ, the second transaction's UPDATE touches a row that a concurrent transaction changed and committed after its snapshot, so it fails with SQLSTATE 40001 ("could not serialize access due to concurrent update"). The lost update is turned into an error. The application must catch 40001 and retry the whole transaction. It could also avoid the problem with an atomic UPDATE ... SET balance = balance + n or SELECT ... FOR UPDATE.
Exercise
Why is SQL Server's READ COMMITTED not a single behavior, and how would you tell which one a database uses?
Show answer
With READCOMMITTEDSNAPSHOT OFF, READ COMMITTED uses shared locks that are released after each read, so readers and writers block each other. With it ON, the same level uses row versions and readers do not block. Check is_read_committed_snapshot_on in sys.databases for the current database.