Sub topic 13
Concurrent access and crash safety
This chapter assumes you know how a B-tree search and a node split work, and that you have used basic SQL. It covers two questions. How do many sessions use one index at the same time? How does an index survive a crash in the middle of a split?
Why indexes need their own protection
A B-tree index is a set of fixed-size pages shared by every session. A split changes at least three pages: the full page, the new sibling page, and the parent. If a reader sees these pages half-updated, it can miss keys or follow a pointer to a page that is not yet linked.
Two kinds of protection solve this, and they work at different levels:
- Latches are short locks on pages. They protect the physical structure of the tree.
- Transaction locks and row versions protect logical data. They decide which rows a query is allowed to see.
Do not confuse them. A latch lasts for microseconds while one page is being read or changed. A transaction lock can last for the whole transaction.
Latching while walking the tree
A simple approach is to latch the whole path from root to leaf. This is safe but it makes the root a bottleneck, because every operation starts there.
Most systems use latch coupling, also called crabbing:
- Latch the root.
- Latch the child you will visit next.
- If the child is safe, release the parent latch. A child is safe when the operation cannot cause it to split or merge, so the parent will not change.
- Repeat until you reach the leaf.
A reader releases the parent as soon as it holds the child. A writer keeps the parent latched only while the child is full (for an insert) or at its minimum size (for a delete). Since most pages are not full, writers usually hold only one or two latches at a time.
PostgreSQL takes a different route. Its B-tree follows the Lehman-Yao design. Every page has a right-link to its right sibling and a high key, which is the upper bound of the keys on that page. A reader that arrives at a page just after it split can compare its search key with the high key and move right. This lets splits happen without blocking readers for long.
Index changes and row visibility
Latches do not give snapshots. In PostgreSQL, an index entry points to a row version, and the index does not record which transactions can see that version. After finding an entry, the executor checks visibility in the table. This is also why an index-only scan sometimes still visits the table: it must confirm that the row is visible, unless the page is marked all-visible.
In engines such as MySQL InnoDB, the clustered index holds the row versions and a secondary index entry is checked against them. In both designs, a reader and a writer on the same key do not block each other at the row level, because each sees its own version.
Crash safety with a write-ahead log
A split touches several pages, and a crash can happen after only some of them reach disk. The fix is the write-ahead log (WAL): before a changed page is written to the data file, a record describing the change is written to a sequential log and flushed.
On restart, recovery replays the log and brings every page to a consistent state. A split is logged as one atomic group of changes, so recovery either applies the whole split or none of it. If the log shows a split was interrupted, the engine finishes it or rolls it back.
This is why pages can be written lazily. The log is the durable record, and the data pages catch up later.
Practical effects
| Situation | Effect | Common response |
|---|---|---|
| Many inserts of increasing keys | All writers hit the right-most leaf, which causes latch contention | Use a different key, or spread writes |
| Large `CREATE INDEX` on a busy table | A plain build blocks writes | In PostgreSQL, use `CREATE INDEX CONCURRENTLY` |
| Crash during a split | Recovery replays the log | No action needed |
Exercise
A writer inserts into a leaf that has free space. Why can it release the parent page latch before it finishes the insert, and which kind of protection is the latch?
Show answer
The leaf has free space, so the insert cannot cause a split, and the parent page will not change. The latch is a short physical lock on a page. It protects the tree structure, not the logical visibility of rows, which transaction locks and row versions handle.
Exercise
Why does the write-ahead log let the database write index pages to disk lazily without risking a half-finished split?
Show answer
The log record describing the split is flushed to disk before the changed pages are written. After a crash, recovery replays the log and either applies the whole split or none of it, so the pages end up consistent.