#7552·surrealdb

Bug: concurrent writes during `DEFINE`/`REBUILD INDEX … CONCURRENTLY` can leave stale (and occasionally missing) index entries

Author: omer9564Created Sep 29, 2026Updated Sep 29, 2026

What component does this affect?

SurrealDB Server

Describe the bug

Auto-commit UPDATEs that change an indexed field while REBUILD INDEX … CONCURRENTLY runs can leave the index wrong after INFO FOR INDEX reports status: 'ready'. The same happens during a first DEFINE INDEX … CONCURRENTLY. Mostly the index keeps extra (stale) entries: it still maps some rows to values they no longer hold, usually values they held only briefly during the build. In two attempts on 3.2.4, the index also missed rows under their current value (lost entries).

  • No BEGIN, COMMIT or SLEEP is involved; every statement is its own transaction.
  • Every UPDATE that wrote a stale value returned OK, and nothing reports an error.
  • The wrong entries do not go away by themselves. We checked 163 wrong attempts with the script's REPAIR=1 option: 90 from the runs in the first table below, 64 from a second set of runs, and 9 from four shorter runs. In all of them, the same stale entries were still there at least 2 s after the writers stopped, and so were the lost ones where there were any. A REBUILD INDEX … CONCURRENTLY with no writers then made the index exact.

Queries that use the index without re-reading the record return wrong results. On 3.3.0, after the build reported ready, row t:2 holds 'w2_1590', but the index still has it under 'w2_1380':

Query Result Correct result
SELECT id, x FROM t WHERE x = 'w2_1380' AND record::id(id) >= 1 (plan: IndexScan on ix, then Filter) [{ id: t:2, x: 'w2_1590' }] []
SELECT count() FROM t WHERE x = 'w2_1380' GROUP ALL [{ count: 1 }] no row counted: no row holds 'w2_1380' (next query)
SELECT id, x FROM t WITH NOINDEX WHERE x = 'w2_1380' [] []
SELECT id, x FROM t WHERE x = 'w2_1380' [] []
SELECT count() FROM t WHERE x >= 'w' AND x < 'x' GROUP ALL count: 88 count: 80, as the same query returns WITH NOINDEX

record::id(id) >= 1 is true for every row, so the first query asks the same thing as the plain one. It returns a row whose x does not match x = 'w2_1380'. The plain query appears to recheck the record and is correct.

Lost entries. One attempt on 3.2.4 (memory engine, REBUILD, 8 writers) reported stale=52 lost=6 after ready. Six rows were not returned by the index for their current value, so a query that goes through the index misses them. The range count agrees: 126 through the index, 80 WITH NOINDEX (80 − 6 + 52). The log shows two of the six, and both were still indexed under their previous value (excerpt):

     stale ('w1_780', t:1): t:1.x is now 'w1_790'; 'w1_780' written at +143.5ms, replaced by 'w1_790' at +143.9ms
     stale ('w1_781', t:9): t:9.x is now 'w1_791'; 'w1_781' written at +143.6ms, replaced by 'w1_791' at +143.9ms
     lost: t:1.x = 'w1_790' is not returned by the index for that value
     lost: t:9.x = 'w1_791' is not returned by the index for that value

Their current values were written about 114 ms before ready was first seen (+257.5 ms). 2 s later, with no writers, all six were still missing (lost=6). A REBUILD INDEX … CONCURRENTLY with no writers restored them (stale=0 lost=0). A later 4-attempt run on 3.2.4 showed the same shape once more (stale=5 lost=1: t:8 missing under its current value 'w0_590' and still indexed under 'w0_580'), with the same result 2 s later and after the repair REBUILD. These are the only lost entries in all our logs: 7 entries in 2 of 1,104 attempts, both on 3.2.4 with 8 writers.

How often. Official Docker images, memory engine, the script below with its defaults plus REPAIR=1: 20,000 rows, 8 writer connections over 80 hot rows, 20 attempts per run, each attempt in a fresh database.

Build Runs that reproduced Attempts with a wrong index Stale entries Lost entries
3.2.4+20260803.93ab219 3/3 55/60 754 6
3.3.0 3/3 14/60 91 0
3.4.0-nightly+20260925.df34ba7 3/3 21/60 114 0

A second set of runs with the same settings (3 runs per build) gave 49/60 attempts on 3.2.4 (645 stale, 0 lost) and 15/60 on 3.3.0 (87 stale, 0 lost).

Many UPDATEs fail with the retryable Transaction conflict: Write conflict error while the build runs: about 8% of them on 3.2.4 and about half on 3.3.0 and nightly. The script does not retry them. No stale value came from a failed UPDATE.

With a lighter load (WRITERS=4 HOT=40), the results were:

  • 3.2.4: 59 of 100 attempts, and all 5 runs reproduced.
  • 3.3.0: 10 of 200 attempts, and 7 of 10 runs reproduced.
  • Nightly: 0 of 60 attempts.

These lighter-load runs used an earlier revision of the script, and so did the 4-writer variants, the single-writer run and 40 of the 50 RocksDB attempts below. That revision ran the same checks, but its defaults were WRITERS=4 HOT=40 ATTEMPTS=10 MAX_WRITE_SECS=5 (we set the attempt counts explicitly), and it had no REPAIR option. The other runs used the script below, except that its timestamp parsing was changed afterwards: it now parses full dates, so timings stay right across midnight UTC. Two short runs with the changed script gave the same kind of results.

What the stale entries look like. Values are unique per write, so each stale value can be traced to the UPDATE that wrote it. The table compares each stale value with the next successful UPDATE of the same row. "Reruns" is the second set of runs.

Build Stale values Next successful write of the row came after the first poll that saw ready Time from the stale write to that next write: median (range)
3.3.0 91 91 141 ms (103–397 ms)
3.3.0, reruns 87 86 138 ms (21–371 ms)
nightly 114 106 136 ms (20–340 ms)
3.2.4 754 5 0.7 ms (0.3–129 ms)
3.2.4, reruns 643 (plus 2 'old') 17 0.8 ms (0.3–205 ms)

Every stale value other than 'old' was written before ready was first seen.

  • On 3.3.0 and nightly, the row's next successful write usually came only after ready, a median of about 140 ms after the stale write. This timing fits writes to the indexed field being held while the build is closing, as #7425 reports for an earlier version. We have confirmed that wait only in the v3.2.4 source (see below). For 3.3.0 and nightly it is an inference from this timing.
  • On 3.2.4, stale values were written from just before the REBUILD was sent until a few milliseconds before ready. 23 of 754 were written in the 10.9 ms before it was sent, while the writers were already running (reruns: 3 of 643, up to 2.6 ms before). The last one was written 4.5 ms before ready was first seen. The row's next successful write followed after a median of 0.7 ms, so that next write was not held up. They often came in runs of consecutive UPDATEs from one connection, one per row. Some entries are worse than an old value left behind:
    • 18 of 754 map a row to a value that was only ever written to a different row (reruns: 8 of 645). In one attempt, seven rows of one connection (t:19, t:27, …, t:67) were all indexed under 'w3_199', which that connection only ever wrote to t:75.
    • Some keep the seed value 'old' for a row that has been updated since: 16 entries in 5 runs, all on 3.2.4. In one of the reruns, ('old', t:7) and ('old', t:52) were left, and count() for x = 'old' returned 19922 through the index and 19920 WITH NOINDEX. Of the others, 11 came from the two 8-writer runs where the writers start after the DDL (5 with DEFINE, 6 with REBUILD), and 3 from 4-writer runs. With DEFINE there is no index before the build, so the build itself wrote those 'old' entries. None of the three 3.2.4 runs in the first table had one.
    • The lost entries above.

Narrowing it down. 8 writers over 80 hot rows. The first row is the three runs per build from the first table (60 attempts). Every other cell is one run of 20 attempts.

Variant 3.2.4 3.3.0 nightly
REBUILD, writers already running when it is sent (default) 55/60 14/60 21/60
REBUILD, writers start only after the REBUILD statement returned 18/20 8/20 6/20
First DEFINE INDEX … CONCURRENTLY on a table with no index, writers start only after the DEFINE statement returned 17/20 4/20 4/20

With 4 writers over 40 hot rows (two runs of 20 attempts per cell), the two "writers start after" variants gave 13/40 (REBUILD) and 21/40 (DEFINE) on 3.2.4, and 2/40 and 0/40 on 3.3.0.

  • Writes do not need to race the start of the build.
  • In the DEFINE row, every write starts after the DEFINE statement has returned, so after its transaction has committed. So this is not about writes that commit before the new index definition is visible.
  • On 3.2.4, REBUILD with a single writer connection left no wrong index in 40 attempts (WRITERS=1 HOT=40). That run differs from the others in more than the number of connections. Only 2 of its 39,600 UPDATEs failed with a write conflict, and its builds were shorter: ready came 74–149 ms after the REBUILD (median 83 ms), against a median of 136 ms with 4 writers and 165 ms with 8. So this result cannot tell apart a cause that needs concurrent ticket reservations (see below), one that needs write conflicts and failed UPDATEs, and one that needs a longer build.

RocksDB: no stale entries, but the test did not cover the same case. No wrong index appeared in 50 attempts on RocksDB, 25 each on 3.2.4 and 3.3.0. 40 of them ran with WRITERS=4 HOT=40 and a 5 s cap on writing, and 10 with WRITERS=8 HOT=80 and a 10 s cap. In all 50, the build had not reached ready when the cap was hit, so the script stopped the writers first, and the build finished with no writes in flight. RocksDB also took far fewer writes: 425–677 UPDATEs/s per attempt with 4 writers and 1,155–1,226/s with 8. On memory, the rate was 4,331–27,460/s with 4 writers, and 5,597–37,186/s with 8 in the runs of the first table (10th to 90th percentile about 7,200–28,200/s). The rate counts UPDATE statements, OK or failed, per second between the attempt's first and last successful write. On memory, hitting the cap first also gave no wrong index: 3.3.0 with WRITERS=16 HOT=160 hit its 5 s cap before ready in all 20 attempts, and none of them left a wrong index. So the RocksDB result is more likely due to the cap than to the engine. It does not show that RocksDB is unaffected.

Code we looked at. This is from reading the v3.2.4 source only. We have not read the 3.3.0 or nightly source, and we have not confirmed the cause.

One explanation that would fit, but that we have not verified: two queued writes end up under the same queue key, and one overwrites the other. The overwritten write is then never replayed, so the index keeps its row under the value from before that write, which is a stale entry. For a row's first write, that value can be the seed 'old' from the initial scan. If it was the row's last write, the row is also missing under its current value, which is a lost entry. A marker that points at an overwritten key makes the initial scan index its row under the other row's old value, which matches the entries that map a row to a value only another row held. The comment on ensure_single_ticket_allocator (kvs/index/builder.rs L989–L1006) describes this outcome for clusters running mixed versions: "one writer's !br and !bg entries overwrite the other's". Our test is a single node running one version, so we do not know how two queue keys could coincide here. A cause in the write-conflict path would fit what we saw as well, and the single-writer result does not rule out either (see above). The 3.3.0 and nightly pattern may have a different cause.

Steps to reproduce

Outline, on surreal start --user root --pass root memory:

surql
-- connection A
DEFINE TABLE t SCHEMALESS;
FOR $i IN 1..=20000 { CREATE type::record('t', $i) SET x = 'old' RETURN NONE; };
DEFINE INDEX ix ON t FIELDS x CONCURRENTLY;   -- no writers; wait until INFO FOR INDEX ix ON t reports ready

-- connections W0..W7, already running: each owns the rows t:j with j % 8 = w (j in 1..=80)
-- and sends requests of 200 back-to-back auto-commit statements, one unique value per statement:
UPDATE t:8 SET x = 'w0_0' RETURN VALUE time::now();
UPDATE t:16 SET x = 'w0_1' RETURN VALUE time::now();
-- ...

-- connection A, 20 ms after the writers start:
REBUILD INDEX ix ON t CONCURRENTLY;           -- poll INFO FOR INDEX until ready, then stop the writers

-- check, for every value v that was ever written, and for 'old':
SELECT VALUE record::id(id) FROM t WHERE x = '<v>' AND record::id(id) >= 1;   -- rows the index has under v
SELECT record::id(id) AS i, x FROM t WITH NOINDEX;                         -- what the rows really hold

The script below does this in Docker and prints one line per attempt. For a wrong attempt it prints every stale entry, where the stale value was written on the build's timeline, the queries above for the first two stale entries, and up to two lost entries. It exits 0 if the bug reproduced and 2 if it did not.

IMAGE=surrealdb/surrealdb:v3.3.0 bash repro.sh
IMAGE=surrealdb/surrealdb:v3.3.0 REPAIR=1 bash repro.sh               # also re-check after 2 s, then REBUILD with no writers
IMAGE=surrealdb/surrealdb:v3.3.0 MODE=define WRITERS_START=after bash repro.sh

One wrong attempt on 3.3.0, from IMAGE=surrealdb/surrealdb:v3.3.0 REPAIR=1 bash repro.sh (steps 4 and 5 are printed only with REPAIR=1). The plan JSON is shortened to its operators. The script also printed the same four queries for ('w0_1433', t:32), with the same pattern of results; that block is left out.

image:  surrealdb/surrealdb:v3.3.0
server: 3.3.0 for linux on aarch64
engine: memory
mode=rebuild writers-start=before rows=20000 hot=80 writers=8 chunk=200 lead=0.02s attempts=20
...
attempt 5/20
  1. DEFINE INDEX ix ON t FIELDS x CONCURRENTLY, no writers -> ready; stale=0 lost=0 count(x='old') index/NOINDEX 20000/20000
  2. REBUILD INDEX ix ON t CONCURRENTLY sent at +0.0ms, returned OK at +0.9ms; first write at -40.3ms
     INFO FOR INDEX: indexing@+20.4ms -> ready@+1506.1ms; at ready: {"compacting":false,"initial":20000,"pending":0,"status":"ready","updated":2247}
     writers stopped once ready; last write at +1597.8ms: 11600 UPDATEs, 3704 OK, 7896 failed (e.g. ['There was a problem with the key-value store: Transaction conflict: Write conflict, retry the transa'])
  3. check: stale=8 lost=0 | count(x='old') index/NOINDEX 19920/19920 | count('w' <= x < 'x') index/NOINDEX 88/80
  RESULT attempt=5 stale=8 lost=0 updated=2247 writes_ok=3704 writes_failed=7896 -> BUG
  BUG: 8 stale / 0 lost index entries after the build reported ready; stale values by the status of the UPDATE that wrote them: {'OK': 8}; by kind: {'row held it earlier': 8, "seed value 'old'": 0, 'value never written to that row': 0}
     stale ('w2_1380', t:2): t:2.x is now 'w2_1590'; 'w2_1380' written at +1441.3ms, replaced by 'w2_1400' at +1583.6ms
     stale ('w0_1433', t:32): t:32.x is now 'w0_1593'; 'w0_1433' written at +1446.0ms, replaced by 'w0_1473' at +1580.6ms
     stale ('w3_1225', t:43): t:43.x is now 'w3_1395'; 'w3_1225' written at +1455.0ms, replaced by 'w3_1245' at +1580.6ms
     stale ('w7_1345', t:47): t:47.x is now 'w7_1395'; 'w7_1345' written at +1468.7ms, replaced by 'w7_1355' at +1580.5ms
     stale ('w4_1067', t:60): t:60.x is now 'w4_1397'; 'w4_1067' written at +1297.2ms, replaced by 'w4_1227' at +1580.5ms
     stale ('w5_1347', t:61): t:61.x is now 'w5_1397'; 'w5_1347' written at +1437.5ms, replaced by 'w5_1397' at +1580.5ms
     stale ('w1_1008', t:65): t:65.x is now 'w1_1198'; 'w1_1008' written at +1395.6ms, replaced by 'w1_1098' at +1580.6ms
     stale ('w6_1348', t:70): t:70.x is now 'w6_1598'; 'w6_1348' written at +1377.9ms, replaced by 'w6_1428' at +1580.5ms
     SELECT id, x FROM t WHERE x = 'w2_1380' AND record::id(id) >= 1;
        -> [{"id": "t:2", "x": "w2_1590"}]
        plan: SelectProject <- Filter(record::id(...) >= 1) <- IndexScan(ix, access: = 'w2_1380')
     SELECT id, x FROM t WHERE x = 'w2_1380';
        -> []
     SELECT count() FROM t WHERE x = 'w2_1380' GROUP ALL;
        -> [{"count": 1}]
     SELECT id, x FROM t WITH NOINDEX WHERE x = 'w2_1380';
        -> []
     [the same four queries for ('w0_1433', t:32) left out]
  4. 2 s later, no writers: stale=8 lost=0 (same entries: True) | count('w' <= x < 'x') index/NOINDEX 88/80
  5. REBUILD INDEX ix ON t CONCURRENTLY with no writers -> ready: stale=0 lost=0 | count(x='old') index/NOINDEX 19920/19920 | count('w' <= x < 'x') index/NOINDEX 80/80
...
SUMMARY: REBUILD INDEX ix ON t CONCURRENTLY (writers start before it): 4/20 attempts left a wrong index after INFO FOR INDEX reported ready (27 stale, 0 lost entries in total)
repro.sh
bash
#!/usr/bin/env bash
# Auto-commit writes that run while `REBUILD INDEX ... CONCURRENTLY` (or a first
# `DEFINE INDEX ... CONCURRENTLY`) is building can leave STALE index entries: after the build
# reports `ready`, the index still maps a row to a value that the row no longer holds.
# Rarely, it also leaves LOST entries: a row that the index does not return for its current value.
# No BEGIN/COMMIT and no SLEEP: every statement is its own auto-commit transaction.
#
# Each attempt, in a fresh database:
#   1. Seed ROWS rows t:1..t:ROWS with x = 'old'. Run `DEFINE INDEX ix ON t FIELDS x CONCURRENTLY;`
#      with no writers, wait until INFO FOR INDEX reports `ready`, and check that the index is exact.
#   2. Start WRITERS writer connections. Each owns a share of the hot rows