// dialect comparison

SET TRANSACTION ISOLATION LEVEL in every SQL dialect

Isolation levels trade concurrency for correctness: dirty reads, non-repeatable reads and phantoms shift between levels.

Filed under Sessions & admin

The verdict

Four levels in SQL Server (plus SNAPSHOT) and MySQL, three effective levels in PostgreSQL, two plus READ ONLY in Oracle, none to pick in SQLite. Scope differs too: SQL Server's SET lasts for the session, while Oracle's and PostgreSQL's SET TRANSACTION and MySQL's bare SET TRANSACTION cover a single transaction.

Where they differ

SET TRANSACTION ISOLATION LEVEL exists in four engines but the accepted values differ, and SQLite has exactly one behavior.

How each engine writes it

SQL Server

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
-- levels: READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ, SERIALIZABLE, SNAPSHOT

All four classic levels plus SNAPSHOT, and the setting lasts for the session until changed. READ COMMITTED takes shared locks unless READ_COMMITTED_SNAPSHOT is on for the database (it is on by default in Azure SQL Database).

PostgreSQL

BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
-- levels: READ COMMITTED (default), REPEATABLE READ, SERIALIZABLE

SET TRANSACTION works only inside a transaction block; outside one it warns and does nothing. READ UNCOMMITTED is accepted but behaves as READ COMMITTED. SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL ... sets the session default.

MySQL

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
-- levels: READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ, SERIALIZABLE

REPEATABLE READ is the default. Without SESSION or GLOBAL, SET TRANSACTION applies only to the next transaction; SET SESSION TRANSACTION ISOLATION LEVEL ... lasts for the connection.

SQLite

-- no isolation levels; all transactions are effectively serializable

Writers are serialized and every transaction reads a consistent snapshot, which amounts to SERIALIZABLE. The only knob is PRAGMA read_uncommitted, and it applies only to connections sharing a cache.

Oracle

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
-- levels: READ COMMITTED (default), SERIALIZABLE, plus SET TRANSACTION READ ONLY

SET TRANSACTION must be the first statement of a transaction and covers only that one; ALTER SESSION SET ISOLATION_LEVEL = SERIALIZABLE changes the session. SERIALIZABLE reads a snapshot from transaction start and raises ORA-08177 if it updates a row changed since. READ ONLY gives reports a consistent snapshot.

Questions that come up

Which level should I use?

READ COMMITTED, the default in SQL Server, PostgreSQL and Oracle, suits most work. Escalate to REPEATABLE READ or SERIALIZABLE only where a specific anomaly hurts, and verify the cost under load.

Why does SQL Server block readers at READ COMMITTED?

Classic locking behavior: readers take shared locks that wait on writers. Turning on READ_COMMITTED_SNAPSHOT gives PostgreSQL- and Oracle-style statement snapshots and usually removes reader-writer blocking.

What is MySQL's default again?

REPEATABLE READ, unlike PostgreSQL, Oracle and SQL Server which default to READ COMMITTED.

Keep reading

// write it once, run it anywhere

Switch engines without relearning SQL

SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.