Understanding Isolation Levels

1. Understanding Read Committed

PropertyDetail
DefaultYes
SeesSnapshot at start of each statement
PreventsDirty reads
AllowsNon-repeatable reads, phantoms

2. Understanding Repeatable Read

PropertyDetail
SeesSnapshot at first statement of TX
PreventsDirty + non-repeatable reads + phantoms (PG-specific)
ConflictsRaises serialization_failure (40001) on concurrent updates

3. Understanding Serializable

BEGIN ISOLATION LEVEL SERIALIZABLE;
-- SSI: predicate locks detect conflicts
COMMIT;   -- may raise 40001 — retry
ImplementationSSI (Serializable Snapshot Isolation)
PreventsAll anomalies
CostPredicate locks + retry logic required

4. Setting Transaction Isolation Level

BEGIN;
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
-- or
BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY DEFERRABLE;

5. Setting Default Isolation Level

ALTER DATABASE shop SET default_transaction_isolation = 'repeatable read';
SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL SERIALIZABLE;

6. Understanding Read Phenomena

PhenomenonRCRRS
Dirty readNoNoNo
Non-repeatableYesNoNo
PhantomYesNo (PG)No
Serialization anomalyYesYesNo

7. Handling Serialization Failures

-- Pseudocode retry loop
for attempt in 1..5 loop
   BEGIN ISOLATION LEVEL SERIALIZABLE;
   ... ;
   COMMIT;     -- on 40001 retry
end loop;
Warning: Application MUST retry on SQLSTATE 40001 / 40P01 — server cannot transparently retry.

8. Using READ ONLY Transactions

BEGIN READ ONLY;            -- forbids writes; allows planner optimizations
SELECT ... ;
COMMIT;

9. Using READ WRITE Transactions

BEGIN READ WRITE;           -- default mode

10. Understanding MVCC

ConceptDetail
Tuple visibilityxmin / xmax + snapshot
WritersNever block readers
ReadersNever block writers
GarbageVACUUM reclaims dead tuples