All work

05 · Storage layer · Database engine

Pythia

SQL in, bytes on disk out.

A compact, embeddable relational database in portable C99: one file, B+tree storage, real transactions and a query planner.

Role
Independent systems development
Stack
C99 · B+ trees · SQL · CMake
Status
Open source
Layer
05 — Storage

01 · Context

The question behind the build.

Applications lean on databases they never see inside. Pages, trees, journals and planners decide what a query costs and whether a crash loses data.

Objective

Build a relational engine from the bottom up with nothing but a C99 compiler: storage, transactions, SQL and planning, runnable on Windows, Linux and macOS.

Approach

SQL text passes through a lexer and parser to a virtual machine with a planner and executor. One B+tree implementation backs tables, indexes and the schema catalog, on 4 KiB pages with an LRU cache and a rollback journal.

02 · Architecture

How it fits together.

  1. 01SQL text
  2. 02Lexer and parser
  3. 03Planner and executor
  4. 04B+ trees on 4 KiB pages
Architecture as drawn in the project README, from SQL text down to the single database file.

03 · Decisions

The choices that matter.

  1. 01

    One tree, three uses

    The same B+tree stores table rows by rowid, index keys in an order-preserving encoding, and the schema catalog itself.

  2. 02

    Durability first

    BEGIN, COMMIT and ROLLBACK with a write-ahead rollback journal, fsync on commit and automatic crash recovery when the file is opened.

  3. 03

    A planner that cannot miss rows

    Each WHERE clause gets a primary-key seek, an index seek or a full scan. Residual predicates are always re-evaluated, so a chosen path can only narrow the candidates.

The hard part

Every layer constrains the next: the page format shapes the tree, the tree shapes what the planner can seek, and the journal must make all of it recoverable.

04 · Implementation

From idea to working code.

Pythia accepts MySQL and Oracle type spellings, enforces keys and constraints per row, and loads CSV inside a single transaction. It runs as a web console, an interactive terminal or a line-oriented TCP server.

SQL / Both dialects, from the documentation
-- Oracle-style DDL
CREATE TABLE emp (
    empno  NUMBER(5) PRIMARY KEY,
    ename  VARCHAR2(30) NOT NULL,
    salary NUMBER(10,2)
);

-- MySQL-style DDL
CREATE TABLE cust (id BIGINT PRIMARY KEY, name VARCHAR(50));

05 · Outcome

The result, and the next question.

An open-source database engine with no dependencies beyond the C standard library. It is the deepest layer in this portfolio: the place where data finally rests.

Summarized from the public README. No performance comparison with other engines is claimed. Read the project documentation (opens in a new tab) ↗

Next · 01 Interface layerSolaris