tursodatabase/turso · error

Next on non-btree/materialized-view cursor

Error message

Next on non-btree/materialized-view cursor

What it means

Insn::Next (advance a scan cursor to the following row) executed on a cursor outside the handled set — the match covers BTree, MaterializedView, and IndexMethod; sorter, pseudo, and virtual table cursors are not (sorters advance via SorterNext, vtabs via VNext). The program advanced a cursor with the wrong opcode — a codegen inconsistency caught in op_next.

Solutions

  1. Report the SQL — the loop's advance opcode must match the cursor kind
  2. Test removing ORDER BY / GROUP BY / DISTINCT and vtab references to isolate which cursor is mis-targeted
  3. Restructure so sorting happens in an outer query level
  4. Upgrade
Defensive patterns

Strategy: validation

Validate before calling

// Next must only advance cursors that support it
for insn in &program.insns {
    if let Insn::Next { cursor_id, .. } = insn {
        match program.cursor_ref.get(*cursor_id).map(|(_, t)| t) {
            Some(CursorType::BTreeTable(_)) | Some(CursorType::BTreeIndex(_))
            | Some(CursorType::MaterializedView(..)) | Some(CursorType::IndexMethod(..)) => {}
            other => crate::bail_parse_error!("Next on unsupported cursor ({other:?})"),
        }
    }
}

Prevention

When it happens

Trigger: Loop emission picking the generic Next for a cursor allocated as a sorter (ORDER BY/GROUP BY pipeline), pseudo, or virtual table — e.g. after flattening rules changed or DISTINCT handling was reworked, so the loop opcode no longer matches the cursor kind.

Common situations: ORDER BY / GROUP BY / DISTINCT queries mixed with joins and subqueries; engine upgrades altering loop emission.

Related errors


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

Appendix: source

Thrown at core/vdbe/execute.rs:3438

fn blob_reg_usize(state: &ProgramState, reg: usize, opcode: &str, what: &str) -> Result<usize> {
    match state.registers[reg].get_value() {
        Value::Numeric(Numeric::Integer(n)) if *n >= 0 => Ok(*n as usize),
        other => Err(LimboError::InternalError(format!(
            "{opcode}: {what} register must hold a non-negative integer, got {other:?}"
        ))),
    }
}

/// Resolve `cursor` to the b-tree cursor the blob opcodes require, or error. Only
/// table-row cursors expose byte-level column access, so this is the single gate all
/// three opcodes (BlobRead/BlobWrite/BlobLen) share.
fn blob_btree_cursor<'a>(
    state: &'a mut ProgramState,
    cursor: usize,
    opcode: &str,
) -> Result<&'a mut dyn crate::storage::btree::CursorTrait> {
    match state.get_cursor(cursor) {
        Cursor::BTree(btree) => Ok(btree.as_mut()),
        Cursor::Dyn(btree) => Ok(btree.as_mut()),
        _ => Err(LimboError::InternalError(format!(
            "{opcode} requires a b-tree cursor"
        ))),
    }
}

/// Signal incremental-blob expiry to `Blob::read`/`write`: store NULL in `dest` and
/// advance. Expiry must fail the operation without aborting the program — the paused
/// blob program owns the handle's transaction, and writes made before the expiry
/// must still commit at close (sqlite3_blob semantics). Shared by BlobRead/BlobWrite.
fn blob_expired_ack(state: &mut ProgramState, dest: usize) -> InsnFunctionStepResult {
    state.registers[dest].set_value(Value::Null);
    state.pc += 1;
    InsnFunctionStepResult::Step
}

pub fn op_blob_read(

View on GitHub (pinned to 8d4a589f8d)