Delete and VACUUM — the file doesn't shrink

DELETE removes a cell pointer and adds the bytes to the page's freeblock chain — pages aren't compacted and aren't merged with siblings, so the file stays the same size until VACUUM rebuilds it (at ~2x disk transiently) or incremental_vacuum returns whole free pages to the OS.

Previously

Scene 5 ended with the tree symmetrically GROWING on insert. Junior intuition: it must symmetrically SHRINK on delete. It doesn't — and the difference is a half-step worth taking.

Scene 05a

Delete and VACUUM — the file doesn't shrink

  1. Watch
  2. Try it
  3. Predict
  4. Capture
DB FILE SIZE2,500 pages · 10 MBUTILIZATION100%TREE — depth 3p1 (root)p2p3p4p11p12p13p14p15p16p17p18p19p20p21p22LEAF ZOOM — p17 (events lea…0 freeblocks#100 alice#101 bob#102 carol#103 dave#104 ellen#105 frank#106 grace#107 henry#108 iris#109 jackRECLAIM MODEnonefreeblocks scattered; file size unchangedincremental_vacuumreturns whole free pages; partial leaves st…VACUUMfull rebuild; needs ~2× disk transientlyUTILIZATION100%freeblocks scattered; file size frozen10 rows in leaf p17. We're about to DELETE 6 of them and watch the file size — and the freeblock chain.
What to watch for

Watch what 6 DELETEs do to leaf p17 and to the file. Cells gray out, the freeblock chain (red curve) snakes through them, the page-utilization meter drops to 40%. The DB FILE SIZE readout above the tree does NOT move.

Continue unlocks when the animation finishes.

Where this sits in Build a B-tree storage engine (SQLite-style)

Scene 05a of 11, in the Mutations act — Insert, split, delete — and why the file doesn't shrink.. DELETE doesn't shrink the file — pages get freeblocks but never merge; only VACUUM rebuilds the file (at 2x disk cost).

Up next. We've covered table B-trees end-to-end. The next scene shows that every CREATE INDEX you write adds a SECOND B-tree beside this one — and what that costs on every write.

All 11 scenes in Build a B-tree storage engine (SQLite-style) · Every curriculum

Built with Arqly
Every scene in Build a B-tree storage engine (SQLite-style) builds on the one before it.All 11 Build a B-tree storage engine (SQLite-style) scenes