{"record":{"id":"77681417479ac661","repo":"tursodatabase/turso","slug":"rewind-on-non-btree-materialized-view-cursor","errorCode":null,"errorMessage":"Rewind on non-btree/materialized-view cursor","messagePattern":"Rewind on non-btree/materialized-view cursor","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/vdbe/execute.rs","lineNumber":1829,"sourceCode":"\npub fn op_vrename(\n    program: &Program,\n    state: &mut ProgramState,\n    insn: &Insn,\n    _pager: &Arc<Pager>,\n) -> InsnResult {\n    load_insn!(\n        VRename {\n            cursor_id,\n            new_name_reg\n        },\n        insn\n    );\n    let name = state.registers[*new_name_reg].get_value().to_string();\n    let cursor = state.get_cursor(*cursor_id);\n    let cursor = cursor.as_virtual_mut();\n    let vtabs = &program.connection.syms.read().vtabs;\n    let vtab = vtabs\n        .iter()\n        .find(|p| {\n            p.1.id().eq(&cursor\n                .vtab_id()\n                .expect(\"non ext-virtual table used in VRollback\"))\n        })\n        .expect(\"Could not find virtual table for VRollback\");\n    vtab.1.rename(&name)?;\n    state.pc += 1;\n    Ok(InsnFunctionStepResult::Step)\n}\n\npub fn op_open_pseudo(\n    _program: &Program,\n    state: &mut ProgramState,\n    insn: &Insn,\n    _pager: &Arc<Pager>,\n) -> InsnResult {","sourceCodeStart":1811,"sourceCodeEnd":1847,"githubUrl":"https://github.com/tursodatabase/turso/blob/8d4a589f8d13ac184700d2a8f724f27e1995be3b/core/vdbe/execute.rs#L1811-L1847","documentation":"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.","triggerScenarios":"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.","commonSituations":"Queries mixing ORDER BY/DISTINCT with flattened subqueries; engine upgrades changing flattening or loop-emission rules.","solutions":["Report the SQL — Rewind must only target BTreeTable/BTreeIndex/MaterializedView cursors","Confirm the shape by removing ORDER BY / DISTINCT / vtab references one at a time to find which cursor kind is mis-targeted","Restructure so the suspicious construct sits in its own query level","Upgrade"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Rewind must target btree-backed cursors only\nfor insn in &program.insns {\n    if let Insn::Rewind { cursor_id, .. } = insn {\n        match program.cursor_ref.get(*cursor_id).map(|(_, t)| t) {\n            Some(CursorType::BTreeTable(_)) | Some(CursorType::BTreeIndex(_))\n            | Some(CursorType::MaterializedView(..)) => {}\n            other => crate::bail_parse_error!(\"Rewind on non-btree cursor ({other:?})\"),\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Include ORDER BY/DISTINCT/vtab plus subquery mixes in the CI corpus","Debug-build program audits for loop opcodes vs cursor kinds","Boundary catch_unwind to convert engine panics into query failures"],"tags":["vdbe","executor","cursor-type","table-scan","panic","runtime"],"backgroundTag":"bytecode-cursor-type-mismatch","analyzedSha":"8d4a589f8d13ac184700d2a8f724f27e1995be3b","analyzedAt":"2026-08-20T07:02:18.389Z","contentChangedAt":"2026-08-20T07:02:18.389Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}