Skip to content

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

Turso 5 min read Deep dive mediumAdept

Something you probably want to know about if you're using SQLite in Golang

In SQLite's WAL mode, an unclosed Go database/sql rows object keeps a read transaction open, which stops checkpoints from moving changes from the WAL file into the main database file. The WAL then grows without bound until the process restarts. In the example, leaking rows left a 42 MB WAL and a 4.1 kB main file, versus about 406 kB and 4.1 MB normally, and a manual PRAGMA wal_checkpoint did not fix it. Always close the results of Query and QueryContext; the same leak also blocks VACUUM.

Related posts

  • A linter I really recommend you to use if you're doing SQL in Golang

    Turso mediumAdept

    The sqlclosecheck linter catches unclosed sql.Rows and sql.Stmt objects in Go code, which otherwise can cause deadlocks or inefficient disk use in databases like SQLite. A short example shows it flagging a missing rows.Close() and passing once defer rows.Close() is added. It can be added to CI as a GitHub Actions step via go vet -vettool, or enabled in golangci-lint through .golangci.yaml, and is recommended as a standard CI check.

  • How to get your database access code deadlocked in Golang

    Turso mediumAdept

    In Go's database/sql, failing to close a Rows result can deadlock the program when a loop over query results returns early on error. Rows holds a reader lock on the underlying sql.Conn until it is fully iterated or explicitly closed, so a deferred conn.Close() waits forever for the writer lock. The same leak hits plain db.QueryContext through the connection pool, which is easy to reproduce with SetMaxOpenConns(1). The happy path works fine, so the bug hides in under-tested error paths; the fix is to always defer rows.Close().

  • An adventure in writing compatible systems

    Turso mediumAdept

    Turso, a Rust rewrite of SQLite, hit a bug where every write after the database crossed 1GB made SQLite's integrity checks fail, even though Turso's own checks found the file intact. The cause was the SQLite "pending byte" page: once a file passes the 1GB mark, SQLite reserves a special page tied to file locking, and Turso did not reserve it. The deterministic simulation tests had missed the bug because every profile injected faults, so no test database ever grew to 1GB. Adding fault-free runs fixed that gap, a reminder that compatible rewrites must match undocumented-feeling quirks of the original implementation.

  • Birth and death of a row in Turso's MVCC

    Turso hardMaster

    Turso's MVCC, modeled on Hekaton, tracks each row as a chain of versions in a lock-free SkipMap, with begin/end fields holding a TxId while a transaction is active and a commit timestamp once it commits. The walkthrough follows one row from the INSERT bytecode executed by the VDBE, through the commit protocol (timestamp acquisition, first-committer-wins conflict checks, dependency waits, logical-log write and fsync), crash recovery by replaying the logical log, and checkpointing into the SQLite WAL and B-tree. Checkpoints default to a blocking Truncate mode triggered at about 4 MB, with an experimental Passive mode that lets writers proceed. Reads merge the in-memory store and the B-tree with a two-pointer iteration. Deletes set the end field under the version-chain lock, so concurrent writers to the same row get a WriteWriteConflict.

  • Turso v0.4.0

    Turso mediumAdept

    Turso v0.4.0 overhauls its experimental MVCC mode, which lifts SQLite's single-writer limit, adding index support, checkpointing and recovery. MVCC is now enabled with `PRAGMA journal_mode = 'experimental_mvcc'`, persisted in the file header, and makes the file unreadable by stock SQLite until you switch back to WAL. Partial sync fetches pages on demand, with bootstrap queries and prefetching (persistent mode is Linux-only because it needs sparse files), and sync now ships in the Python and Go SDKs. The release also adds page cache spilling for large queries, ANALYZE support for better join ordering, and a disk-spilling hash join for non-indexed columns in place of SQLite's ephemeral B-tree indexes.

The quest for software engineering knowledge.

Look:
Layout: