tursodatabase/turso · error

Rewind on non-btree/materialized-view cursor

Error message

Rewind on non-btree/materialized-view cursor

What it means

Insn::Rewind (position a scan cursor at its first row) executed on a cursor that is neither a BTree nor a MaterializedView runtime cursor. Rewind only works on B-tree-backed objects; sorter, pseudo, and virtual table cursors use different opcodes (SorterRewind, VFilter/VNext). The program advanced a cursor of the wrong kind — a codegen bug caught mid-execution in op_rewind.

Solutions

  1. Report the SQL — Rewind must only target BTreeTable/BTreeIndex/MaterializedView cursors
  2. Confirm the shape by removing ORDER BY / DISTINCT / vtab references one at a time to find which cursor kind is mis-targeted
  3. Restructure so the suspicious construct sits in its own query level
  4. Upgrade
Defensive patterns

Strategy: validation

Validate before calling

// Rewind must target btree-backed cursors only
for insn in &program.insns {
    if let Insn::Rewind { cursor_id, .. } = insn {
        match program.cursor_ref.get(*cursor_id).map(|(_, t)| t) {
            Some(CursorType::BTreeTable(_)) | Some(CursorType::BTreeIndex(_))
            | Some(CursorType::MaterializedView(..)) => {}
            other => crate::bail_parse_error!("Rewind on non-btree cursor ({other:?})"),
        }
    }
}

Prevention

When it happens

Trigger: Full-scan emission (Rewind) resolved to a cursor that was actually allocated as a sorter (ORDER BY pipeline), pseudo (VALUES/co-routine), or virtual table cursor — typically after cursor id reuse across subquery flattening or changes to which opcode the loop emitter picks.

Common situations: Queries mixing ORDER BY/DISTINCT with flattened subqueries; engine upgrades changing flattening or loop-emission rules.

Related errors


AI-assisted analysis of tursodatabase/turso@8d4a589f8d (2026-08-20). Data as JSON: /api/errors/77681417479ac661. Report an issue: GitHub.

Appendix: source

Thrown at core/vdbe/execute.rs:1829

pub fn op_vrename(
    program: &Program,
    state: &mut ProgramState,
    insn: &Insn,
    _pager: &Arc<Pager>,
) -> InsnResult {
    load_insn!(
        VRename {
            cursor_id,
            new_name_reg
        },
        insn
    );
    let name = state.registers[*new_name_reg].get_value().to_string();
    let cursor = state.get_cursor(*cursor_id);
    let cursor = cursor.as_virtual_mut();
    let vtabs = &program.connection.syms.read().vtabs;
    let vtab = vtabs
        .iter()
        .find(|p| {
            p.1.id().eq(&cursor
                .vtab_id()
                .expect("non ext-virtual table used in VRollback"))
        })
        .expect("Could not find virtual table for VRollback");
    vtab.1.rename(&name)?;
    state.pc += 1;
    Ok(InsnFunctionStepResult::Step)
}

pub fn op_open_pseudo(
    _program: &Program,
    state: &mut ProgramState,
    insn: &Insn,
    _pager: &Arc<Pager>,
) -> InsnResult {

View on GitHub (pinned to 8d4a589f8d)