Sub topic 3
Snapshot isolation and repeatable read
This chapter assumes you know basic SQL (SELECT, INSERT, UPDATE), how to use BEGIN, COMMIT and ROLLBACK, and that several clients use one database at the same time.
Snapshot isolation and repeatable read
Snapshot isolation gives each transaction a frozen view of the data. The database keeps older versions of rows, a method called MVCC (multiversion concurrency control). A transaction reads only the versions that were committed before it started, so repeating a query returns the same rows. Readers do not block writers, and writers rarely block readers.
The SQL standard's REPEATABLE READ level forbids non-repeatable reads. PostgreSQL implements it as snapshot isolation. Other engines implement it with locks, so the behavior differs.
Write skew: the anomaly snapshots allow
Snapshot isolation does not prevent write skew. Two transactions read the same set of rows, then each updates a different row. Neither update conflicts with the other, but together they break a rule.
Only SERIALIZABLE prevents this in PostgreSQL. It detects the dependency and aborts one transaction.
How PostgreSQL, MySQL, SQL Server and Oracle differ
| Engine | Default level | What REPEATABLE READ does | Write skew under that level |
|---|---|---|---|
| PostgreSQL | READ COMMITTED | Snapshot isolation | Possible |
| MySQL InnoDB | REPEATABLE READ | Plain SELECT reads a snapshot, but UPDATE and locking reads use locks | Possible with plain SELECT |
| SQL Server | READ COMMITTED | Lock-based | Blocked by locks |
| Oracle | READ COMMITTED | Not offered. SERIALIZABLE is snapshot-based | Possible even at SERIALIZABLE |
To use snapshot isolation in SQL Server, enable it on the database first:
ALTER DATABASE shop SET ALLOW_SNAPSHOT_ISOLATION ON;
SET TRANSACTION ISOLATION LEVEL SNAPSHOT;
BEGIN TRANSACTION;
SELECT count(*) FROM on_call WHERE on_duty = 1;
COMMIT;Handling serialization failures and deadlocks
Under SERIALIZABLE, and in some lock-based engines, the database rejects a transaction instead of letting it finish. Your application must catch the error and retry the whole transaction.
- PostgreSQL reports a serialization failure with SQLSTATE 40001 and a deadlock with 40P01.
- MySQL reports a deadlock with error 1213.
- SQL Server reports a deadlock victim with error 1205.
Retry a limited number of times, and rerun every read inside the retried transaction. Do not reuse results from the failed attempt.
> The following callout is a tip, not a quote.
Choosing an isolation level for your workload
| Workload | Suggested level |
|---|---|
| Mostly reads and simple single-row updates | READ COMMITTED (the default) |
| Reports that must see one consistent snapshot | REPEATABLE READ or SNAPSHOT |
| Rules that span several rows, such as "at least one doctor on duty" | SERIALIZABLE with retries, or explicit locks |
Exercise
Three nurses are on shift. Two sessions each check that at least one nurse is on shift, then each removes a different nurse. Under PostgreSQL REPEATABLE READ, is the rule guaranteed to hold?
Show answer
No. This is write skew. Each session reads a snapshot that shows enough nurses, and their updates touch different rows, so no conflict is detected. Use SERIALIZABLE and retry on SQLSTATE 40001, or lock the rows with SELECT ... FOR UPDATE before checking the count.