Use it when results are inconsistent under concurrency or transactions fail with serialization errors.
Isolation Levels
Isolation levels (Read Uncommitted, Read Committed, Repeatable Read, Serializable, plus SQL Server Snapshot) decide which concurrency anomalies a transaction can see.
1. Current level: SHOW transaction_isolation; / SELECT @@transaction_isolation; / DBCC USEROPTIONS.
2. Anomaly observed (non-repeatable read, phantom, lost update)?
3. Raise level or use SELECT … FOR UPDATE.
4. Handle retries.
5. Test.
transaction starts with an isolation level → snapshot/locks → visible data
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 How to verify Verify boundary
Reproduce with two psql or mysql sessions. Confirm the application retries on serialization or deadlock errors.
Step 2 Common failures Narrow the fault
Read-then-update without FOR UPDATE can lose updates under read committed. Serializable without retry logic turns conflicts into user-facing errors. MySQL repeatable read uses gap locks that can block inserts.
Defaults: PostgreSQL and SQL Server Read Committed, MySQL InnoDB Repeatable Read. Higher isolation means more retries or blocking — applications must retry serialization failures.
SHOW transaction_isolation;— first check for this topicpsql -c "SELECT now(), version();"— PostgreSQL reachable and versionmysql -e "SELECT NOW(), VERSION();"— MariaDB/MySQL reachable and versionsqlcmd -S <server> -Q "SELECT @@VERSION"— SQL Server reachable and version
ReferenceWhat it is1 section · depth on demand
How it works
Under MVCC each transaction reads from a snapshot. In read committed, each statement gets a new snapshot; in repeatable read, the whole transaction uses one. Serializable adds checks for dangerous dependency patterns and aborts one transaction.
ReferenceImportant terms & boundaries3 sections · depth on demand
Levels and anomalies
| Level | Dirty read | Non-repeatable read | Phantom |
|---|---|---|---|
| Read Uncommitted | Possible | Possible | Possible |
| Read Committed (PostgreSQL, SQL Server default) | No | Possible | Possible |
| Repeatable Read (MySQL InnoDB default) | No | No | Possible (prevented by InnoDB next-key locks / PG snapshot) |
| Serializable | No | No | No |
Higher levels mean more retries (serialization failures) or blocking; applications must retry such transactions.
In depth
The lost update — and three ways to prevent it
Two sessions read stock 10, each subtracts 1 and writes 9: one sale is lost. Under read committed this happens silently.
-- 1. Do it in one statement (best when possible)
UPDATE stock SET qty = qty - 1 WHERE sku = 'A1' AND qty > 0;
-- 2. Lock the row while deciding
BEGIN;
SELECT qty FROM stock WHERE sku = 'A1' FOR UPDATE; -- second session waits here
UPDATE stock SET qty = 9 WHERE sku = 'A1';
COMMIT;
-- 3. Optimistic: version column, retry if 0 rows updated
UPDATE stock SET qty = 9, version = version + 1 WHERE sku = 'A1' AND version = 7;
Behaviour that differs between engines
| PostgreSQL | MySQL InnoDB | SQL Server | |
|---|---|---|---|
| Default level | Read committed | Repeatable read | Read committed (locking, unless RCSI is on) |
| Repeatable read conflict | Error could not serialize access → retry | Updates see the latest committed row (no error) | Shared locks held to end |
| Serializable | SSI: no blocking, may abort with serialization failure | Turns plain SELECT into locking reads | Range locks |
| Readers block writers? | No | No (consistent reads) | Yes, unless READ_COMMITTED_SNAPSHOT |
Retry is part of the design
At repeatable read and serializable in PostgreSQL, and on any deadlock everywhere, the database may abort a transaction that did nothing wrong. Retry the whole transaction on SQLSTATE 40001 (serialization failure) and 40P01 (deadlock); MySQL reports error 1213 for deadlocks.
Common mistakes
- Assuming default isolation prevents all anomalies.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.