#9433·turso

MVCC: JOIN through a secondary index misses rows that exist only in the un-checkpointed log (0.7.2)

Author: ackeCreated Sep 30, 2026Updated Sep 30, 2026

Summary

With PRAGMA journal_mode = 'mvcc', a JOIN that the planner resolves through a secondary index does not return a row that was inserted after the last checkpoint (i.e. lives only in the MVCC log), when an already-checkpointed row was UPDATEd to reference it. The same row is returned when the planner uses the primary-key index, by a plain scan, and after PRAGMA wal_checkpoint(TRUNCATE). Row size is irrelevant.

Observed in production (Turso 0.7.2, Rust turso crate and tursodb CLI behave identically); reproduced deterministically with the script below.

Repro (tursodb 0.7.2)

bash
#!/usr/bin/env bash
set -euo pipefail; B="${1:-tursodb}"
setup() { rm -f r.db r.db-log r.db-wal
  "$B" r.db "PRAGMA journal_mode='mvcc';
  CREATE TABLE app_contract(tenant_namespace TEXT, app_id TEXT, selected_candidate_id TEXT, PRIMARY KEY(tenant_namespace, app_id));
  CREATE TABLE app_contract_candidate(tenant_namespace TEXT NOT NULL, app_id TEXT NOT NULL, candidate_id TEXT NOT NULL, candidate_json TEXT NOT NULL, staged_at_unix INTEGER NOT NULL, PRIMARY KEY(tenant_namespace, app_id, candidate_id));
  $1
  INSERT INTO app_contract VALUES('T','a','a1'),('T','s','s1');
  INSERT INTO app_contract_candidate VALUES('T','a','a1','{}',1),('T','s','s1','{}',4);
  PRAGMA wal_checkpoint(TRUNCATE);
  INSERT INTO app_contract_candidate VALUES('T','s','s3','{}',6);   -- log-only row
  UPDATE app_contract SET selected_candidate_id='s3' WHERE tenant_namespace='T' AND app_id='s';" >/dev/null; }
q() { echo "WITH budget AS MATERIALIZED (SELECT COUNT(c.selected_candidate_id) AS selected FROM app_contract c LEFT JOIN app_contract_candidate k ON k.tenant_namespace=c.tenant_namespace AND k.app_id=c.app_id AND k.candidate_id=c.selected_candidate_id WHERE c.tenant_namespace='T'), family AS (SELECT k.app_id, $1 AS x FROM app_contract c JOIN app_contract_candidate k ON k.tenant_namespace=c.tenant_namespace AND k.app_id=c.app_id AND k.candidate_id=c.selected_candidate_id JOIN budget ON 1=1 WHERE c.tenant_namespace='T' AND budget.selected <= 256) SELECT budget.selected, (SELECT COUNT(*) FROM family) AS joined, (SELECT group_concat(app_id) FROM family) AS joined_apps, (SELECT group_concat(candidate_id) FROM app_contract_candidate WHERE app_id='s') AS scan_sees FROM budget;"; }
IDX="CREATE INDEX app_contract_candidate_lookup ON app_contract_candidate(tenant_namespace, app_id, staged_at_unix DESC);"
setup "$IDX"; echo "--- secondary index, project candidate_json  (BUG: joined=1, expected 2):"; "$B" r.db "$(q k.candidate_json)"
setup "$IDX"; echo "--- secondary index, project candidate_id     (PK-covered → planner uses PK, correct: 2):"; "$B" r.db "$(q k.candidate_id)"
setup "$IDX"; "$B" r.db "PRAGMA wal_checkpoint(TRUNCATE);" >/dev/null; echo "--- after checkpoint (correct: 2):"; "$B" r.db "$(q k.candidate_json)"
setup "";     echo "--- control: no secondary index (correct: 2):"; "$B" r.db "$(q k.candidate_json)"

Output on 0.7.2 (macOS aarch64 and Linux x86_64 identical):

--- secondary index, project candidate_json  (BUG: joined=1, expected 2):
│ selected=2 │ joined=1 │ joined_apps=a   │ scan_sees=s1,s3 │
--- secondary index, project candidate_id     (correct: 2):
│ selected=2 │ joined=2 │ joined_apps=a,s │ scan_sees=s1,s3 │
--- after checkpoint (correct: 2):
│ selected=2 │ joined=2 │ joined_apps=a,s │ scan_sees=s1,s3 │
--- control: no secondary index (correct: 2):
│ selected=2 │ joined=2 │ joined_apps=a,s │ scan_sees=s1,s3 │

NOT INDEXED and INDEXED BY sqlite_autoindex_app_contract_candidate_1 on the joined table also produce the correct result.

Expected

A committed row is visible to every access path (secondary index included) regardless of whether it has been checkpointed yet.

Environment

  • turso 0.7.2 (crate, pinned) and turso_cli v0.7.2 release binaries
  • journal_mode=mvcc, default mvcc_checkpoint_threshold (4120000)
  • Also reproduced on a production database snapshot: the verbatim query returned 3 of 4 joined rows before a checkpoint and 4 of 4 after.

Impact

For us this made a tenant-wide consistency check (joined_rows == selected_count) fail for every read until the log crossed the checkpoint threshold — days, in practice — taking the whole tenant's runtime catalog offline. Working around it on our side with INDEXED BY <pk autoindex> on every read of that table plus an explicit PRAGMA wal_checkpoint(TRUNCATE) at process start; happy to test a fix.

Side observation

In mvcc mode PRAGMA wal_checkpoint(TRUNCATE) always returns (0, 0, 0) (busy/log/checkpointed), even when it does fold a non-empty -log back to 0 bytes — so callers cannot tell from the result row whether anything was checkpointed. When another connection holds an open read transaction the pragma fails on row fetch with database is locked instead.