Isolation Levels: Dirty Reads, Phantom Reads, Snapshot Isolation 00:00:01 Alice reads a price that vanishes Two cashiers use one ledger. One writes a tentative price and later changes her mind. What is the other cashier allowed to see in between? An isolation level constrains those observations. x is 50. B runs at read uncommitted. B gets a value that does not officially exist. B decides with 100, a value that was never committed. At read committed B would have read 50 at t4. Read uncommitted here names a textbook behavior. PostgreSQL implements a request for that level as read committed. Always check the guarantees of the database you actually operate. 00:00:42 The same receipt changes on rereading Now the first price really commits, but between two reads in the other transaction. Both returned values can be legitimate at their respective times and still spoil a report that expects one consistent view. x is 50. Pending, so A would still read 50. Now 100 is the official value. At repeatable read A keeps the snapshot of its first statement and reads 50 again. In PostgreSQL, read committed gives each statement a fresh view. Under repeatable read, consistent reads in the transaction share a snapshot. Other engines and locking reads can differ. 00:01:22 A sixth row walks into the room Count matching orders twice while someone inserts a qualifying order. Even if nobody edits the five original rows, the membership of the answer can change. That new match is a phantom. Five rows match the condition. Row locks on the five rows do not stop a sixth. Six rows now. A range lock makes B wait at t3. A snapshot lets B go on and keeps showing 5 to A. A snapshot can keep the reader's count stable while letting the writer work. Protecting a rule that will itself drive writes is a stronger requirement than repeatedly displaying the same count. 00:02:00 Two doctors independently go off call Alice and Bob both see two doctors on duty. Each believes one colleague will remain, and each edits a different row. Their local checks succeed while the shared rule fails under snapshot isolation. The rule: at least one stays. A sets Alice off. B sets Bob off. Different rows, no conflict. Run again at serializable. The database sees that each read what the other wrote, and aborts one. Lock both rows when reading them, or store a counter per shift that both must change. Serializable execution must reject or block a conflicting history. The application has to retry a rejected transaction from the beginning and reevaluate the decision in its fresh view. 00:02:47 Two sales and only one decrement Both cashiers read ten pastries, subtract one on paper, then write nine. The wrong result did not require a dirty read. Reading a value and later writing a constant opened a race. Two workers read the stock: 10. A sold one. B sold one too, but the stock is 9 and not 8. SET stock equals stock - 1 uses the newest value under a row lock. This works at read committed. An atomic conditional decrement is enough for many single-row inventory rules. For a cross-row rule, model the conflict explicitly or request a suitable isolation guarantee and implement its retry behavior.