Skip to content

Run queries on reader connections once one query has waited for another - #1780

Open
thekevinbot wants to merge 5 commits into
mainfrom
bot/1754-promote-trigger-1120
Open

thekevinbot wants to merge 5 commits into
mainfrom
bot/1754-promote-trigger-1120

Conversation

@thekevinbot

@thekevinbot thekevinbot commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Part of #1754 (PR 2 of 3; PR 3 adds the Node repro, the docs and Fixes #1754).

A DirSQL instance now answers queries concurrently after the first overlap, using Db::promote() and Db::reader() from #1761:

  • Trigger (wait once). Until one query finds another holding the index, every query runs on the writer's connection holding the index, as on main, and the default database stays anonymous, so watch latency is unchanged. The query that does find one running marks contention and waits as it always has.
  • Copy. The next turn on the index, whether a query or a watch batch, runs promote() before its statement. It holds the index, so watch batches and queries arriving during the copy wait for it. If the copy fails, queries stay on the writer.
  • Readers. From then on a query takes its turn as before, so it still waits for a batch in progress. It then takes a reader from a pool (one per query that has run at once, created on demand), lets the index go, and runs in a read transaction, so it reads one snapshot while later batches commit on the writer. Nothing switches back.
  • Batched functions. A statement that names a batched function (Db::may_call_batched, case-insensitive) holds a lock while it runs on a reader, because those workers gather one statement's arguments at a time.
  • A persisted index (--persist/Db::open) follows the same trigger. promote() is a no-op there, since it is already a WAL file.

The Rust repro is packages/rust/tests/concurrent_queries.rs. After one overlap, a cheap query returns while a slow query runs. A watch batch also lands mid-query, and the slow query does not read it.

Manual exercise

Release dirsql server -c .dirsql.toml over 8 JSON files, with SQLITE_TMPDIR set to a scratch directory. Each round sent a ~1.9 s query over POST /query and, 0.5 s later, a count(*):

round cheap query temp dir
before any query empty
1 (the overlap) 1.37 s (waited) dirsql-<pid>-0.db, -wal, -shm
2 0.67 ms same
3 0.35 ms same

A file written afterwards appeared through the watcher ([{"n":9}]). After SIGTERM the temp dir was empty.

Red: job 136443265863 (commit c2c856c, test only): concurrent_queries.rs:88 "a cheap query must be answered while a slow query runs once one overlap has been waited out" and :104 "a watch batch must land while a slow query runs once one overlap has been waited out".

E2E Verification

  • Ran e2e suites locally for every affected SDK
  • Python SDK e2e: pass (89/89) on the second run. The first run failed only watch_speed_test (median 1.117x, spread 70%) while the host load average was 16. An interleaved A/B on the same host showed main failing the same bar (main medians 0.94-1.44, branch 1.01-1.36). The watch path gains only an atomic swap per batch until the first overlap.
  • 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, 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 (twice), cargo test --workspace --features cli --no-fail-fast, at 0aa6c04 (final code; the later merge of main touched one test file only)
  • 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/

@thekevinbot
thekevinbot marked this pull request as ready for review November 20, 2026 19:54

This branch has not been deployed

No deployments
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.

1 participant