r/sqlite • u/SippingOnLean4 • 5d ago
Using Lean to formally verify an optimised SQLite B-tree traversal
Wanted to share an exploration I did into using Lean4 and the SQLite B-tree API for reading data.
In this experiment, I use Lean 4 to formally prove that a custom SQLite B-tree traversal produces the same result as a conventional SQL query (ORDER BY id DESC LIMIT n).
I also implemented an interactive SQLite page inspector and benchmarked the different approaches.
The article goes through the reasoning, Lean proofs, SQLite internals and benchmarks.
Disclaimer: Super new to Lean4
0
Upvotes
2
u/Discordbothost 5d ago
In a rowid table, INT PRIMARY KEY leaves a separate rowid. Insert ids 10 then 1 and ORDER BY id DESC LIMIT 1 returns 10. The reverse table walk starts at id 1. Handy example for Db.Sorted.