{"record":{"id":"3bfc27221cfb289b","repo":"clockworklabs/SpacetimeDB","slug":"invalid-sequence-increment-must-be-less-than-or-e","errorCode":null,"errorMessage":"Invalid sequence: increment must be less than or equal to the range between min_value and max_value","messagePattern":"Invalid sequence: increment must be less than or equal to the range between min_value and max_value","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/datastore/src/locking_tx_datastore/sequence.rs","lineNumber":42,"sourceCode":"    }\n}\n\nimpl Sequence {\n    pub(super) fn new(schema: SequenceSchema, previous_allocation: Option<i128>) -> Self {\n        if schema.start < schema.min_value || schema.start > schema.max_value {\n            panic!(\n                \"Invalid sequence: start value {} is out of bounds for sequence with min_value {} and max_value {}\",\n                schema.start, schema.min_value, schema.max_value\n            );\n        }\n        if schema.max_value <= schema.min_value {\n            panic!(\"Invalid sequence: max_value must be greater than min_value\");\n        }\n        if schema.increment == 0 {\n            panic!(\"Invalid sequence: increment must be non-zero\");\n        }\n        if schema.increment.unsigned_abs() >= (schema.max_value - schema.min_value) as u128 {\n            panic!(\n                \"Invalid sequence: increment must be less than or equal to the range between min_value and max_value\"\n            );\n        }\n        let start = if let Some(prev) = previous_allocation {\n            if prev < schema.min_value || prev > schema.max_value {\n                // Previous versions set allocated to 0 as a default,\n                // so we have this special case.\n                if prev == 0 {\n                    schema.start\n                } else {\n                    panic!(\n                        \"Invalid sequence: previous allocation value {prev} is out of bounds for sequence with min_value {} and max_value {}\",\n                        schema.min_value, schema.max_value\n                    );\n                }\n            } else {\n                prev\n            }","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/6dee26c6efc2856793e12b148a59742964f5d783/crates/datastore/src/locking_tx_datastore/sequence.rs#L24-L60","documentation":"Fourth invariant in Sequence::new: increment.unsigned_abs() must be strictly less than (max_value - min_value) computed as u128 — the check panics on `>=`. (The message text says 'less than or equal to', but the code enforces strictly-less-than: a step equal to the whole span leaves at most one usable value and is rejected.) Hit by sequence DDL whose INCREMENT BY magnitude is >= the declared range.","triggerScenarios":"Creating a sequence like MINVALUE 0 MAXVALUE 10 INCREMENT BY 10 (or -10), where |increment| >= max_value - min_value.","commonSituations":"Small test sequences with large steps; negative increments sized to the full range; adjusting MAXVALUE downward without re-checking the increment.","solutions":["Reduce |INCREMENT| below (MAXVALUE - MINVALUE), e.g. 1.","Or widen the range so the span exceeds the step magnitude.","Validate increment against the span in migration tooling before applying DDL."],"exampleFix":"-- before: step as large as the whole range\nCREATE SEQUENCE s MINVALUE 0 MAXVALUE 10 INCREMENT BY 10;\n\n-- after: step smaller than the range\nCREATE SEQUENCE s MINVALUE 0 MAXVALUE 10 INCREMENT BY 1;","handlingStrategy":"validation","validationCode":"fn increment_fits_range(s: &SequenceSchema) -> Result<(), String> {\n    let span = (s.max_value - s.min_value) as u128;\n    if s.increment.unsigned_abs() >= span {\n        return Err(format!(\"|increment| {} must be < range span {}\", s.increment.unsigned_abs(), span));\n    }\n    Ok(())\n}","typeGuard":"fn valid_sequence_schema(s: &SequenceSchema) -> bool {\n    s.min_value <= s.start && s.start <= s.max_value\n        && s.max_value > s.min_value\n        && s.increment != 0\n        && s.increment.unsigned_abs() < (s.max_value - s.min_value) as u128\n}","tryCatchPattern":null,"preventionTips":["Cross-check INCREMENT against (MAXVALUE - MINVALUE) in DDL review; the check is strict (<), despite the panic message wording.","Avoid large steps on small test ranges.","Re-validate increments whenever bounds are tightened in migrations."],"tags":["sql","sequence","ddl","schema-validation","spacetimedb"],"backgroundTag":"sequence-definition-invalid","analyzedSha":"6dee26c6efc2856793e12b148a59742964f5d783","analyzedAt":"2026-08-20T06:08:37.179Z","contentChangedAt":"2026-08-20T06:08:37.179Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}