7.10 DBMS Differences and Phenomena Encountered in Practice

The most practical difference in this chapter is the default isolation level.

DBMSDefault isolation levelNotes
PostgreSQLRead CommittedSnapshot-based Repeatable Read also prevents Phantoms. Serializable uses the SSI approach
MySQL (InnoDB)Repeatable ReadThe only one of the four products with Repeatable Read as the default. Prevents Phantoms with gap locks
OracleRead CommittedRead Uncommitted and Repeatable Read not provided. Serializable is snapshot-based
SQL Server (on-premises)Read Committed (lock-based)Turning on RCSI switches to snapshot-based Read Committed
Azure SQL DatabaseRead Committed Snapshot (RCSI)RCSI is on by default, so behavior differs from on-premises SQL Server

Learn more — PostgreSQL isolation levels and SSI, InnoDB gap locks and next-key locks, RCSI and snapshot isolation

Table 7-6. Comparison of default isolation levels.

NOTE Detailed behavior and defaults may differ by version.

This table matters because the same code behaves differently when you move to another DBMS. Here are a few representative cases.

After moving from MySQL to PostgreSQL, the same query returns different results within one transaction. MySQL defaults to Repeatable Read, so it sees the same snapshot throughout the transaction, but PostgreSQL defaults to Read Committed, so the snapshot changes with each statement. The code is unchanged, yet Non-repeatable Reads start to appear. The fix is to specify the isolation level explicitly, or to change the code so that it does not read the same thing twice in one transaction.

After moving from on-premises SQL Server to Azure SQL, blocking disappeared (or results changed). The on-premises default is lock-based Read Committed, so reads waited for writes, but Azure SQL defaults to RCSI, so reads see the old version. This is mostly a good change, but code that depended on the behavior of "waiting for the write to finish and then reading the latest value" will now read stale values.

After raising to Serializable, transactions fail intermittently. A serialization failure is not a bug but designed behavior. The database judged that "allowing these two transactions to run concurrently would produce a result that matches no sequential execution," and rolled one back. When the application receives this error, it must retry the transaction from the beginning, and using Serializable without retry logic is a design error. Transactions rolled back by a deadlock are retried the same way.

Lost Update occurs under Read Committed. The pattern of "read the balance, compute in the application, write it back" is not protected by MVCC. If two transactions each read 500,000 won from their own snapshots and each write their own computed value, one is lost. The fixes are to have the database compute, as in UPDATE account SET balance = balance - 100000; to take an exclusive lock up front at read time with SELECT ... FOR UPDATE; or to add a version column and check at write time whether the row has changed (optimistic locking). These three means were covered in Section 7.6. Keep in mind that MVCC eliminating read-write conflicts does not mean write-write conflicts are eliminated as well.

What looks like a "lock wait" but is not a lock: latches. Monitoring tools show names such as latch wait, LWLock, and mutex, and these are often mistaken for lock problems. A latch is different from the locks a transaction uses to protect rows. It is a very short lock that prevents multiple threads from modifying in-memory structures at the same time, such as buffer pool pages, the log buffer, and the internal data structures of the lock manager itself. It is held not until the transaction ends but for a few microseconds, released as soon as one operation completes, and it does no deadlock detection. Many latch waits mean that access is concentrated on a particular page or internal structure, and looking for long transactions does not solve it. A typical example is the last leaf page of a sequential-key index (Section 3.3), where concurrent inserts pile onto the same page. When looking at wait events, first distinguish whether it is a transaction lock or a latch.

TIP Summary. MVCC keeps old versions of rows and shows readers the value as of the snapshot time, so reads and writes do not wait for each other, while write-write conflicts are still resolved with locks. The isolation level is a difference in when the snapshot is taken, and because old versions are cleaned only after the oldest transaction ends, long transactions are the root of problems in every DBMS.

End of Part 2. The question from Chapter 4, "if the server goes down in the middle of a transfer, where does the money go?", can now be answered. The change is written to the log before the data pages (Chapter 5), the log is accepted as committed only after fsync confirms it on disk (Chapter 6), and the restarted server reads the log, redoing what was committed and undoing what was not. While that transfer is in progress, other people see the old version and so never witness the intermediate state (Chapter 7). The money goes nowhere. Part 3 looks at how data stored this safely is read quickly, covering joins and the optimizer.