#7553·surrealdb

Bug: embedded engine: `DEFINE TABLE IF NOT EXISTS` succeeds, but the next read in the same session reports the table does not exist (concurrent first access to a fresh database)

Author: omer9564Created Sep 30, 2026Updated Sep 30, 2026

What component does this affect?

SurrealDB SDK (Rust), embedded engine (kv-mem)

Describe the bug

Many sessions of one embedded datastore open the same fresh database at once. Each one runs DEFINE TABLE IF NOT EXISTS t SCHEMALESS, and the statement result is checked with Response::check(). The session then reads t:current. Sometimes the DEFINE returns OK, and the very next select in the same session fails with The table 't' does not exist.

A second DEFINE TABLE IF NOT EXISTS in that session can report OK again, with the table still invisible. DEFINE TABLE OVERWRITE t SCHEMALESS makes it visible every time we tried, and it keeps any records already in the table.

Concurrent first access also produces Transaction conflict: Write conflict … This transaction can be retried on use_ns().use_db() and on the DEFINE. Those are retryable and expected. The problem is the successful DEFINE followed by a missing table: nothing says to retry, and the code that ran the DEFINE believes the table exists.

Frequency

With 20 rounds × 32 concurrent sessions and a fresh database per round, the invisible-table result appears in some runs and not others:

engine runs DEFINE ok, then "does not exist" other failures (conflicts)
embedded mem:// 3 23, 5 and 1 of 640 USE: 56–86, DEFINE: 50–168
server surreal start … memory, SDK over ws:// 2 0 and 0 of 640 USE: 9 and 0
server, HTTP /sql (one request, or three requests per session) 6 0 of 640 in each 0–5 conflicts

We have seen it only on the embedded engine. We have not tried embedded RocksDB.

Steps to reproduce

A standalone crate (full source below; surrealdb = "=3.2.4", features kv-mem, protocol-ws, rustls):

bash
cargo run --release -- mem://

Each session does:

rust
db.use_ns("repro").use_db(format!("db{round}")).await?;
db.query("DEFINE TABLE IF NOT EXISTS t SCHEMALESS").await?.check()?;   // OK
let _: Option<Value> = db.select(("t", "current")).await?;             // "The table 't' does not exist"

The sessions are 32 clones of one Surreal<Any>, on a multi-thread tokio runtime with 8 workers.

Cargo.toml
toml
[package]
name = "define-invisible-repro"
version = "0.1.0"
edition = "2024"

[dependencies]
surrealdb = { version = "=3.2.4", default-features = false, features = ["kv-mem", "protocol-ws", "rustls"] }
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
serde_json = "1"

[workspace]
src/main.rs
rust
//! DEFINE TABLE IF NOT EXISTS succeeds, yet the next read in the same session
//! reports "The table 't' does not exist". SurrealDB 3.2.4.
//!
//!   cargo run --release -- mem://
//!   cargo run --release -- ws://127.0.0.1:8000   # surreal start --user root --pass root memory
use std::collections::BTreeMap;
use surrealdb::Surreal;
use surrealdb::engine::any::{Any, connect};
use surrealdb::opt::auth::Root;

const ROUNDS: usize = 20;
const CONCURRENCY: usize = 32;

async fn one(db: Surreal<Any>, database: String) -> String {
    if let Err(e) = db.use_ns("repro").use_db(&database).await {
        return format!("USE failed: {}", short(e));
    }
    let define = db
        .query("DEFINE TABLE IF NOT EXISTS t SCHEMALESS")
        .await
        .and_then(|r| r.check());
    if let Err(e) = define {
        return format!("DEFINE failed: {}", short(e));
    }
    let read: Result<Option<surrealdb::types::Value>, _> = db.select(("t", "current")).await;
    match read {
        Ok(_) => "ok".into(),
        Err(e) => format!("DEFINE ok, then SELECT failed: {}", short(e)),
    }
}

fn short(e: impl std::fmt::Display) -> String {
    e.to_string().chars().take(90).collect()
}

#[tokio::main(flavor = "multi_thread", worker_threads = 8)]
async fn main() {
    let url = std::env::args().nth(1).unwrap_or_else(|| "mem://".into());
    let db = connect(&url).await.expect("connect");
    if url.starts_with("ws") {
        db.signin(Root { username: "root".into(), password: "root".into() })
            .await
            .expect("signin");
    }
    let mut counts = BTreeMap::<String, usize>::new();
    for round in 0..ROUNDS {
        let mut tasks = tokio::task::JoinSet::new();
        for _ in 0..CONCURRENCY {
            // Each clone is its own session.
            tasks.spawn(one(db.clone(), format!("db{round}")));
        }
        while let Some(r) = tasks.join_next().await {
            *counts.entry(r.expect("task")).or_default() += 1;
        }
    }
    println!("surrealdb 3.2.4, {url}, {ROUNDS} rounds x {CONCURRENCY} sessions, fresh database per round");
    for (k, v) in counts {
        println!("{v:5}  {k}");
    }
}

Expected behavior

After DEFINE TABLE IF NOT EXISTS t returns OK, t exists for every later statement in that session. Otherwise the DEFINE returns an error, such as a retryable transaction conflict.

Actual behavior

The DEFINE returns OK, and the next select returns The table 't' does not exist. It happened in 1–23 of 640 sessions per run on mem://.

SurrealDB version

3.2.4. Rust SDK surrealdb = "=3.2.4", embedded kv-mem. macOS 15 (aarch64). Rust stable.

Is there an existing issue for this?

  • I have searched the existing issues