How it works
Postgres, MySQL with InnoDB, and SQL Server all index data with B-trees. A B-tree divides storage into fixed-size pages, by default 8 KB in Postgres and 16 KB in InnoDB. Each page holds sorted keys and references to child pages, and leaf pages hold the rows or pointers to them. Wide pages keep the tree shallow. At an example 400 entries per page, three levels reach 64 million entries and four reach 25.6 billion. With upper levels cached, a lookup usually needs one or two disk reads.
To change a key, the engine finds its leaf page and modifies it, and the page is later written back to the same location. If the page is full, it splits in two and the parent gains a key, which may split the parent too. Each key has one home, so reads are predictable and scans follow sorted leaves.
Overwriting in place is risky. A crash mid-split can leave a parent pointing at an unwritten page, and a power cut can tear a page, leaving it half old and half new. So the engine records each change in a write-ahead log, synced at commit, while changed (“dirty”) pages wait in memory. A checkpoint writes dirty pages out, so recovery only replays the log from that point. Torn pages need more: Postgres logs a full copy of a page the first time it changes after a checkpoint, and InnoDB writes pages to a doublewrite buffer first. So a small change costs a log record and, eventually, a whole page write.
Your Postgres primary checkpoints about once an hour (an example setting), and after a crash it spent 20 minutes replaying the log. The business wants restarts under 3 minutes. What do you change, and what does it cost?
Checkpoint more often, because recovery replays the log only from the last checkpoint. The cost is more background page writing and more log volume, since Postgres logs a full page copy the first time a page changes after each checkpoint. Spread the checkpoint writes out so they do not hurt foreground latency. If that is not enough, failing over to a replica may meet the availability goal without waiting for recovery.
Use this as a guide for self review; written answers are not graded.Keep in mind
- A B-tree keeps each key in one page, so reads are predictable and the tree stays shallow.
- Pages change in place, so the write-ahead log is what makes a crash mid-update recoverable.