A write's cost depends on where its bytes land
Appends cost one sequential trip. In-place updates cost scattered seeks plus a redo record, so a crash can finish the job instead of leaving a broken tree.
An index makes reads faster and writes slower, and how much slower depends on where new bytes land. Appending to a write-ahead log is one sequential disk trip with no seek. An in-place update can rewrite a leaf page and several ancestors, each a separate seek. A redo record, written first and in sequence, lets the database finish that scattered work after a crash. Write cost is set by placement, not by the disk alone.
A write-ahead log only adds to its end. Each record sits beside the one before it, so the disk head never moves and the write is fast. A B-tree update is harder. A node holds a fixed number of keys, and an insert must keep it sorted, so the node is rewritten. If the node is full, it splits in two, and the parent must record the new boundary. The parent may be full too, so the split can climb toward the root, where the tree grows a new root above it. One logical row update therefore becomes a log record, a leaf page, and some number of ancestor pages scattered across the disk, each a separate seek. Before touching the tree, the writer appends the intended change to the log, so a crash can replay it.
Suppose power fails after the writer creates two new pages but before the parent's pointers are updated. The tree is now inconsistent: pointers lead to the wrong places, and some pages refer to pages that no longer match. The log record and the page describe the same change on purpose, because the copies fail differently. The log is sequential and replayable from any point, while the page is useful only once fully written. A nightly job that updates five percent of a large table's rows, one at a time, barely changes the byte count, yet each update is its own seek, log record, and split-and-reparent chain. Throughput collapses before bandwidth does, and recovery slows because replay scales with the log.
Before tuning, ask whether this write can be append-shaped: record intent in order, apply it later, and merge instead of overwriting. If it can, take that trade and budget for the read side, where a lookup may check several tables until Bloom filters and sparse indexes skip most of them. If the write must happen in place, count what one logical write costs on disk: the leaf page, every ancestor page it reparents, and the log record. That number is write amplification, and it predicts what happens when the write rate triples. Latency measured on an idle disk tells you nothing about it.
- disk page
- 4-8 KB
- pages rewritten per in-place update
- 1-4
- write amplification, B-tree
- ~1-2x
- write amplification, LSM
- ~10-30x
- fsync on NVMe
- ~0.1-1 ms
The unit the storage stack moves. Pages this size come from 1980s hardware and virtual memory design, and every database, filesystem and SSD still organises I/O around them.
One leaf page, plus the ancestors whose keys change when the leaf splits. A tree of depth four can dirty four pages for a single row.
Each logical write costs a page rewrite and a log append, so the physical bytes stay close to the logical bytes.
Every byte is rewritten once per compaction level. Rosenblum and Ousterhout described the original log-structured design in 1991; the price of turning random writes into sequential ones was rewriting data in the background.
The call that turns a buffered write into a durable one. In 2018 Craig Ringer found PostgreSQL ignoring fsync errors on XFS, which meant a committed transaction could vanish after a crash; the fix made an fsync failure panic instead of being swallowed.
The tempting shortcut is to trust fsync. You write the log, call fsync, return success to the client, and treat the storage stack as a contract that holds. That is exactly how most application code is written, and it is why the 2018 PostgreSQL bug mattered beyond PostgreSQL.
The wall appeared on XFS. fsync reported an error, the database retried, the retry appeared to succeed, and the write was gone. Errors from earlier writes could be discarded and never reported, so a transaction acknowledged as durable was not on disk. The failure surfaced only under crash-recovery testing, which is the only place it can surface.
What was tried first was retrying the call and logging the error. That does not work, because the kernel has already dropped the dirty page: a retry of fsync has nothing left to flush. The accepted fix throws the database into a panic so recovery runs before more writes are accepted, and PostgreSQL 11 shipped it.
The non-obvious lesson is that durability is not a stored bit, it is an ordering plus a verification. The redo record only protects you if the record is on stable media before the page is, and if the failure of that assumption stops the system instead of being retried quietly. A write path that cannot tell you whether the barrier held has no durability guarantee, only a hope.