Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Turso 11 min read Announcement easyNovice

We're building Postgres in Rust. Using the LLVM of databases

Turso, a Rust rewrite of SQLite, is adding a Postgres frontend by parsing Postgres SQL into a common AST and compiling it to Turso's VDBE bytecode, turning the engine into an "LLVM of databases" with pluggable frontends. The argument is that SQL databases differ mostly in on-disk layout, not in core operations, so Postgres semantics can be expressed in or added to the VDBE. A C-to-VDBE compiler running Doom illustrates that the bytecode works as a real VM. The work starts from the earlier pgmicro experiment and is early-stage but runnable from source. Planned pieces include the Postgres wire protocol, embedded and in-browser use, live materialized views, and WASM-sandboxed extensions, aiming for broad rather than 100% compatibility.

Related posts

  • Running unmodified Doom in the SQLite bytecode language

    Turso hardMaster

    Turso compiles unmodified Doom C source to SQLite's VDBE bytecode by using LLVM as a frontend and a custom backend, vdbecc, that lowers LLVM IR to VDBE. The VM's unbounded register file removes the need for register allocation, so every SSA value gets its own register. Calls map to Gosub/Return, function pointers become integers dispatched through a binary search, and XOR is synthesized from OR, AND and subtraction. C's address space is a single 64 MiB BLOB row accessed via BlobRead/BlobWrite with a cached overflow-page table, each frame is emitted as a ResultRow from a never-ending SELECT, input arrives as rebound bind parameters, and a save game is just the SQLite database file.

  • Postgres WASM by Snaplet and Supabase

    Supabase mediumAdept

    postgres-wasm runs a full PostgreSQL 14 server in the browser by emulating an x86 machine with v86 rather than compiling Postgres to WASM directly. It boots a minimal Buildroot Linux image and lazily loads the rest over a 9P filesystem via HTTPS, and it uses the page_poison=on kernel parameter so freed memory compresses well, which brings the snapshot of a running Postgres down to about 12MB. Networking goes through a forked websockproxy that converts raw ethernet frames into TCP, with an nginx reverse proxy that maps private IPs to ports so external clients like psql, PgAdmin or a logical replication peer can connect. Other hurdles included a v86 JIT bug that caused PG14 segfaults, worked around by switching Postgres to sysv shared memory, and an inbox-folder listener for running scripts inside the VM.

  • Replacing Protobuf with Rust to go 5 times faster

    PgDog mediumAdept

    PgDog forked pg_query.rs and replaced libpg_query's Protobuf serialization layer with direct bindgen-generated C-to-Rust conversions, making SQL parsing 5.45x faster and deparsing 9.64x faster, with a 25% gain in pgbench throughput. Profiling with samply showed the Protobuf entrypoint dominated CPU time while the Postgres parser itself barely registered, and an LRU AST cache keyed on query text broke down with ORM-generated unique statements and drivers lacking prepared statements. The roughly 6,000-line recursive converter was generated by Claude and checked automatically by comparing its output byte-for-byte against the existing Protobuf-based parse via PartialEq. A recursive per-node walk beat an iterative version, which lost to Prost because of extra allocations, hashmap lookups and repeated tree passes.

  • How We Built the Turso Shell in the Browser

    Turso mediumAdept

    Turso's SQLite-compatible database can run entirely in the browser via WebAssembly, persisting data to OPFS with no backend. The walkthrough builds a SQL shell with Vite, React and TypeScript. It covers importing from the @tursodatabase/database-wasm/vite export to work around a Vite dev-server WASM issue, and setting COOP/COEP headers so SharedArrayBuffer is available. It then builds a query executor that maps prepared-statement results into a table, and upgrades the textarea to a CodeMirror editor with SQL highlighting, autocomplete and a Mod-Enter keybinding.

  • How Turso made connections to SQLite databases 575x faster

    Turso mediumAdept

    SQLite re-reads and parses the sqlite_schema table on every new connection, so opening a connection to a database with 10,000 tables takes about 23ms. Turso, a Rust rewrite of SQLite, parses the schema once and shares it across connections in the same process. It uses Arc::make_mut copy-on-write, so a connection only clones the schema when it changes it inside a transaction, and committed changes propagate to all connections. New connections then open in about 40 microseconds no matter how many tables exist, which is faster even than SQLite's 130 microseconds for a single-table database.

The quest for software engineering knowledge.

Look:
Layout: