Database Pipelines — Part 2/5
Part 1 followed a query from SQL text to an execution plan. Plans still have to touch bytes that outlive the process. This article is about storage engines: how B+ tree / heap designs (PostgreSQL’s usual shape) differ from LSM-tree designs, and how WAL plus checkpoints make durability real without flushing every page on every commit. Ideas are engine-generic; PostgreSQL is the concrete B-tree/heap example. No fabricated timings.
This is Part 2 of 5 in the Database Pipelines series.
The same schema, different engines underneath
We keep the Part 1 tables:
CREATE TABLE customers (
customer_id bigint PRIMARY KEY,
email text NOT NULL UNIQUE,
city text NOT NULL,
created_at timestamptz NOT NULL
);
CREATE TABLE orders (
order_id bigint PRIMARY KEY,
customer_id bigint NOT NULL REFERENCES customers (customer_id),
status text NOT NULL,
total_cents integer NOT NULL,
created_at timestamptz NOT NULL
);
CREATE INDEX orders_customer_id_idx ON orders (customer_id);
CREATE INDEX orders_status_created_idx ON orders (status, created_at);
CREATE INDEX customers_city_idx ON customers (city);
In PostgreSQL these live as heap files plus B-tree index files (by default). Other systems might store the same logical rows in an LSM family engine (RocksDB, Cassandra, LevelDB-style stores, some NewSQL storage layers). SQL on top can look similar; the read/write amplification story does not.
B+ trees and heaps vs LSM-trees
B+ tree / heap style (PostgreSQL example)
Write path (simplified):
- Append intent to the WAL (see below);
- Modify pages in the buffer pool—heap tuple versions and any index leaf/internal pages that must change;
- Mark pages dirty; they may sit in memory for a while;
- Acknowledge commit once the WAL commit record is durable enough for your settings—not once every dirty page is on disk.
Read path: walk a B+ tree (short fan-out path to a leaf), then fetch the heap page by tuple id (ctid in PostgreSQL), apply visibility (Part 3), return the row.
PostgreSQL’s default indexes are B-trees in this family. Updates often create new heap versions rather than overwriting a single immortal row image; vacuum later reclaims what no snapshot needs.
Do not generalize “all SQL databases work like PostgreSQL.” InnoDB clusters the primary key; some engines are index-organized; column stores and LSM engines rearrange the same logical schema differently.
LSM-tree style (illustrative; not how PostgreSQL stores heap tables)
Write path (simplified):
- Append to a WAL (or equivalent redo);
- Insert into an in-memory memtable (often a balanced tree or skiplist);
- When the memtable fills, flush it to an immutable sorted file—an SSTable (sorted string table) on disk;
- Background compaction merges overlapping SSTables, dropping obsolete keys and reclaiming space.
Read path: check the memtable, then potentially several SSTable levels (with bloom filters and indexes to skip files). A point lookup can fan out across levels until compaction consolidates keys.
Amplification and workloads
| Concern | B+ tree / heap (typical OLTP) | LSM (typical write-heavy) |
|---|---|---|
| Write amplification | Updating a row may touch heap + several index pages; random page writes over time | Sequential SSTable writes; compaction rewrites data multiple times |
| Read amplification | Usually a few page reads for a point lookup via a short tree | May read memtable + multiple SSTables before compaction catches up |
| Space amplification | Dead MVCC versions / index bloat until vacuum | Overlapping keys across levels until compaction |
Workload fit (rules of thumb, not laws):
- Update-in-place OLTP with selective indexes (our Fairfax orders pattern) often lands on B-tree/heap engines.
- Very high ingest, append-ish keys, compaction-tolerant reads often lands on LSM stores.
- Hybrid systems exist; always ask what your engine actually implements.
PostgreSQL read path for the running query
When Part 1’s planner chooses an index nested loop on orders_customer_id_idx:
- Descend the B-tree to the leaf for
customer_id; - Read
ctids; - Fetch heap pages through shared buffers;
- On miss, read from OS/disk into the pool;
- Apply MVCC visibility and return rows.
Seq Scan walks heap pages in file order—cheap when most pages are needed, expensive when you needed one city.
WAL, dirty pages, and checkpoints
The WAL-before-data invariant
Durable engines follow a write-ahead rule: a change must be described in the WAL (redo log) before the corresponding durable effect depends on that description for crash recovery. In practice for PostgreSQL-style systems:
- You may modify a page in the buffer pool after logging;
- You must not acknowledge a commit until the commit’s WAL record is flush-safe per configuration;
- You must not recycle WAL segments that recovery might still need.
Commit does not mean every dirty page is on disk
A successful COMMIT means the transaction’s effects are recoverable, not that every touched heap/index page has already been fsynced as a data file write. Dirty pages are flushed later by background writers and checkpoints. Assuming “commit ⇒ all pages flushed” is false—and would destroy sequential WAL benefits.
Symmetrically, the buffer pool can hold pages for uncommitted work; abort/rollback and MVCC visibility decide what readers see. Do not assume “if it is in memory, it is committed,” or “if it is not yet flushed, it is uncommitted.”
What a checkpoint is (and is not)
A checkpoint advances a recovery bound:
- Flush enough dirty pages (and related state) so that redo does not need arbitrarily old WAL;
- Record a redo starting position (checkpoint redo pointer) in control data;
- After a crash, recovery replays WAL from that redo point forward, not from the beginning of time.
Checkpoint ≠ LSM memtable flush. Flush turns a memtable into an SSTable inside an LSM engine. Checkpoint ≠ compaction. Compaction merges SSTables. PostgreSQL checkpoints are about buffer/WAL recovery bounds for its page-oriented store.
Too-rare checkpoints → long recovery and huge flush storms when one finally runs. Too-frequent checkpoints → extra write traffic. Tune from monitoring, not slogans.
Crash recovery in one sentence
After a crash, the engine locates the latest checkpoint’s redo start, replays WAL, and restores a state where every committed transaction’s effects are present and uncommitted effects are not durable as commits—without requiring that data pages had been flushed at each commit time.
Free space, bloat, and indexes (PostgreSQL angle)
Heavy updates to orders.status create new heap versions; old ones linger until vacuum. Indexes point at versions and can bloat too. A composite B-tree (status, created_at) helps Part 1’s filter when the leftmost prefix matches—storage layout and planner estimates interact (Part 1).
What to take into Part 3
- PostgreSQL-style engines are B-tree + heap + WAL + checkpoints; LSM engines optimize a different amplification tradeoff.
- Commit durability is primarily a WAL property; dirty page flush is asynchronous relative to commit.
- Checkpoints bound redo; they are not memtable flush or compaction.
- Row versions in the heap are why concurrency needs clear visibility rules—next up.
Limits and next
This part skipped TOAST, tablespaces, checksums, and physical replication internals. Part 3 covers transactions: ACID, isolation, MVCC, locking, and two-phase commit when more than one participant must agree.
References
- PostgreSQL Documentation: Database Physical Storage — pages and file layout
- PostgreSQL Documentation: Write-Ahead Logging (WAL) — WAL-before-data and durability
- PostgreSQL Documentation: WAL Configuration / checkpoints — checkpoints and recovery
- PostgreSQL Documentation: B-Tree Indexes — default index access method
- RocksDB wiki: Implementation details (LSM) — memtable, SST, compaction overview for contrast
About the author
I write about Applied AI, MCP, Azure AI Search, and RAG. Follow me on Dev.to and Hashnode for more.



Top comments (0)