Skip to content

Add Db::promote to move the anonymous database into a shared WAL temp file - #1761

Merged
thekevinscott merged 14 commits into
mainfrom
bot/1754-concurrent-reads-1118
Nov 20, 2026
Merged

thekevinscott merged 14 commits into
mainfrom
bot/1754-concurrent-reads-1118

Conversation

@thekevinbot

@thekevinbot thekevinbot commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Part of #1754 (PR 1 of 3; the last PR carries Fixes #1754).

Db::new stays the anonymous temp database, so watch latency is unchanged. This PR adds the pieces the query path will use in PR 2:

  • Db::promote() copies an anonymous database (SQLite backup API) into a file of its own in SQLite's temp directory (SQLITE_TMPDIR -> TMPDIR -> /var/tmp -> /usr/tmp -> /tmp; std::env::temp_dir() on Windows), named dirsql-<pid>-<n>.db, owner-only, switches it to WAL, and makes it the Db's connection with the same path-table settings, extensions and declared functions. The file and its -journal/-wal/-shm files are deleted when the last connection drops. A Db that already has a file (promoted, or Db::open) is left alone.
  • Db::reader() opens another connection to a promoted or opened database, set up the same way (function workers shared, so an embedding model is not loaded per reader). On an anonymous database it returns DbError::Anonymous.

Nothing calls promote() yet. PR 2 adds the trigger (the first read that has to overlap another statement waits once, then the database is promoted before the next statement), the reader pool and the Rust repro.

Measurements

Promotion: about 0.7 ms per MiB of database (1M rows of 40-byte blobs: 36 ms; 63 MiB: 45 ms); the WAL switch is about 0.2 ms.

Promoted, 1.5M-row table, 1 s slow scan:

today (one connection) promoted, writer + 2 readers
cheap read during slow read 670-1190 ms 0.06-0.15 ms
write committing mid-slow-read n/a 0.1 ms
RSS growth 3 MiB 3 MiB

Manual exercise

A scratch crate against this branch: reader() before promote() returns Anonymous; promote() moved 1M rows in 36 ms into /var/tmp/dirsql-<pid>-0.db in WAL mode; the writer inserted and counted while a reader's 3M-row join ran, and the join returned a consistent 3,000,000; the file was gone after the last connection dropped.

Red: job 135280534246 (commit 54d1e3b, promote() a stub): db::tests::promote_moves_the_database_into_a_wal_temp_file failed its WAL assertion at db.rs:3915, and every reader test failed with called Result::unwrap() on an Err value: Anonymous. tests/concurrent_reads.rs asserts "Db::promote must move the database to a named file other connections can share" (fails the same way locally).

E2E Verification

  • Ran e2e suites locally for every affected SDK
  • Python SDK e2e: pass (89/89, including watch_speed_test)
  • TypeScript SDK e2e: pass
  • Rust core e2e: cargo test --workspace --features cli --no-fail-fast pass, except non_utf8_names/non_utf8_names_e2e, which fail identically on main on this host (ZFS utf8only rejects non-UTF-8 names, os error 84)
  • packages/python/e2e-attestations/<branch>.json receipt written (just e2e-attest-python)
  • packages/ts/e2e-attestations/<branch>.json receipt written (just e2e-attest-ts)
  • Command(s) run: just e2e-attest-ts, just e2e-attest-python, cargo test --workspace --features cli --no-fail-fast, all at d314bf2 (final code)
  • Result summary: all pass

Changelog / Migrations

  • Changelog fragment added under packages/rust/changelog.d/
  • Migration fragment with the same slug added under packages/rust/migrations.d/

…ons share

Db::new creates dirsql-<pid>-<n>.db in SQLite's temp directory and deletes it with its journal, -wal and -shm files once the last connection closes. Db::reader opens another connection to the same file, switching it to WAL so reads neither wait for nor see a write in progress, and carries over path-table settings, extensions and declared functions.
…reader opens

A rollback journal file created and deleted per commit made a watched write about 1.5x slower to reach an /events consumer than inotifywait; an in-memory journal under an exclusive lock brings a small commit within about 3 microseconds of the anonymous temp database.
…tions share it

Db::new goes back to the anonymous temp database; Db::promote is a stub, so every test that promotes and then opens a reader fails.
@thekevinbot thekevinbot changed the title Open the default database as a shared WAL temp file with reader connections Add Db::promote to move the anonymous database into a shared WAL temp file Nov 19, 2026
@thekevinbot
thekevinbot marked this pull request as ready for review November 19, 2026 03:50
@thekevinscott
thekevinscott merged commit b56edc3 into main Nov 20, 2026
89 checks passed
@thekevinscott
thekevinscott deleted the bot/1754-concurrent-reads-1118 branch November 20, 2026 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants