SQLite: how one SQL statement runs

From sqlite3_prepare_v2 to bytes on disk: the compiler, the virtual machine and the storage engine underneath.

Source
sqlite/sqlite @ 77c6cc6
Checked
11 boxes, 12 arrows, 28 source lines
Drawn
, by an AI agent
Press play to watch the page being drawn. The video has no sound.

What it shows

SQLite does not interpret SQL. It compiles each statement into a small program and then runs that program on a virtual machine. The diagram follows one statement through both halves, and then down to the file.

Compiling. sqlite3_prepare_v2 hands the SQL text to the tokenizer, which feeds tokens one at a time to a parser generated from parse.y. When the grammar rule for a whole statement completes, its action hands the finished parse tree straight to the code generator: sqlite3Select for a SELECT. The code generator appends instructions to a program, one sqlite3VdbeAddOp call at a time.

Running. sqlite3_step runs that program in sqlite3VdbeExec, a single loop over instructions. The instructions that touch data open cursors on B-trees.

Storing. The B-tree layer never touches a file. It asks the pager for pages by number. The pager owns the cache and the transaction, and reads or writes through an OS layer with one implementation per platform; on Unix a read ends in pread. In write-ahead-log mode, changed pages are appended to the log as frames instead of being written in place, and the log is itself a file written through the same OS layer.

Where every box and arrow comes from

Before this drawing was accepted, a script looked up each box and each arrow in sqlite/sqlite at commit 77c6cc6 (20 November 2026) and failed if one was missing. These are the lines it found.

Arrows
From, toProof in the source
Your application → SQL interfaceSQL
SQL interface → Tokenizerprepare
  • src/prepare.c:814prepare hands the SQL text to the tokenizersqlite3RunParser(&sParse, zSqlCopy);
Tokenizer → Parsertokens
  • src/tokenize.c:713each token is fed to the parsersqlite3Parser(pEngine, tokenType, pParse->sLastToken);
Parser → Code generatorparse tree
  • src/parse.y:525a finished SELECT is passed to the code generatorsqlite3Select(pParse, X, &dest);
Code generator → Virtual machinebytecode
  • src/select.c:766the generator appends instructions to the programsqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regOut);
  • src/vdbeaux.c:274adds one instruction to a virtual machine programint sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
SQL interface → Virtual machinestep
  • src/vdbeapi.c:940stepping a statement runs the virtual machinerc = sqlite3VdbeExec(p);
Virtual machine → B-treecursors
  • src/vdbe.c:4612the OpenRead and OpenWrite instructions open a B-tree cursorrc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
B-tree → Pagerpages
  • src/btree.c:2376the B-tree asks the pager for a pagerc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
Pager → OS interfaceread / write
  • src/pager.c:3102a page missing from the cache is read from the filerc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, iOffset);
  • src/pager.c:4542changed pages are written backrc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
OS interface → Database file
  • src/os.c:90the call is passed to the implementation for this operating systemreturn id->pMethods->xRead(id, pBuf, amt, offset);
  • src/os_unix.c:3474which reads the bytes with pread()got = osPread(id->h, pBuf, cnt, offset);
Pager → Write-ahead logframes
  • src/pager.c:3280in WAL mode, changed pages go to the log insteadrc = sqlite3WalFrames(pPager->pWal,
Write-ahead log → OS interface
  • src/wal.c:4048the log is itself a file written through the OS interfacerc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset);
Boxes
BoxProof in the source
Your application

Your own program: it is not part of SQLite's source.

SQL interface
Tokenizer
  • src/tokenize.c:273returns the next token of the SQL texti64 sqlite3GetToken(const unsigned char *z, int *tokenType){
Parser
  • src/parse.y:59the grammar the parser is generated from%name sqlite3Parser
Code generator
  • src/select.c:7988generates the program for a SELECTint sqlite3Select(
  • src/where.c:7005generates the loops for a WHERE clauseWhereInfo *sqlite3WhereBegin(
  • src/expr.c:5252generates the code that evaluates an expressionint sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
Virtual machine
  • src/vdbe.c:903the loop that executes the bytecode programint sqlite3VdbeExec(
B-tree
Pager
  • src/pager.c:4796the page cache with transactions and rollbackint sqlite3PagerOpen(
OS interface
Database file

The database file on disk.

Write-ahead log
  • src/wal.c:4376appends changed pages to the log as framesint sqlite3WalFrames(

How it was made

An AI agent read the project's source at the commit above and drew this page on a WireMagic canvas through Agent Bridge, the same tools any agent gets when you connect it to the app. The result is an ordinary WireMagic file: download it, open it, and move things around.

You can have your own agent do this for a codebase you know. Connect it, then ask it to draw the architecture of the repository.