Chapter 7. MVCC and Isolation Levels: Reading and Writing at the Same Time

Chapters 5 and 6 dealt with protecting committed changes when a server goes down, that is, durability. This chapter deals with what happens when several transactions access the same data at the same time, that is, the isolation in ACID. While a transaction that transfers money from Hong Gildong to Kim Cheolsu is in progress, what value should another transaction see when it reads Hong Gildong's balance? 500,000 won? 400,000 won? Or should it wait until the transfer finishes?

The mechanism that answers this question is database concurrency control, and the approach that modern DBMSs have converged on is MVCC. The chapter proceeds in this order.

  • Section 7.1: Anomalies caused by concurrent access (Dirty Read, Lost Update, Non-repeatable Read, Phantom Read)
  • Section 7.2: The simplest solution, locks, plus two-phase locking and deadlocks
  • Sections 7.3-7.4: Lock types (S, U, X, intent locks), compatibility rules, and lock escalation
  • Section 7.5: The principle of MVCC and its visibility rules
  • Section 7.6: Locking reads (FOR UPDATE) and optimistic locking
  • Section 7.7: Where old versions are stored
  • Section 7.8: Isolation levels and Write Skew
  • Section 7.9: Cleaning up old versions (Vacuum, Purge) and long transactions
  • Section 7.10: Differences among DBMSs and the phenomena you meet in practice

The difference seen in Section 1.4, "PostgreSQL appends a new row on UPDATE, while SQL Server modifies in place", is also explained in this chapter.