feat(sqlite): self-heal FTS index corruption on writes - #406
Merged
Conversation
After an unclean shutdown, torn pages in an FTS5 external-content index (doclist shadows) leave the main table rows readable but their INSERT/UPDATE/DELETE failing with SQLITE_CORRUPT_VTAB (267, 'database disk image is malformed') - some records deletable, others stuck, while PRAGMA integrity_check still reports ok. All ORM write/delete statements now go through execHealing: on a corruption error the FTS5 indexes are rebuilt from their content tables (derived data - always safe to regenerate) and the statement is retried once, so a stuck record deletes without operator intervention.
SteveLauC
approved these changes
Aug 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
After an unclean shutdown (SIGKILL mid-write, crash), torn pages in an FTS5 external-content index (doclist shadow tables) leave the main table rows readable but their INSERT/UPDATE/DELETE failing with
SQLITE_CORRUPT_VTAB(267,database disk image is malformed). Symptom seen in the field: some records delete fine, others are stuck forever, whilePRAGMA integrity_checkstill reports ok - only the FTS shadows are damaged.All SQLite ORM write/delete statements now go through
execHealing:malformed/corruptin the message), rebuild every FTS5 index from its content table (INSERT INTO fts(fts) VALUES('rebuild')) - FTS indexes are derived data, regeneration can never lose rowsRebuilds are serialized with a mutex so concurrent writers cannot double-heal.
Field-verified: the manual equivalent of this heal (
rebuild+ retry) fixed a production DB where a stuck job record had been undeletable through the UI.Test plan
go build,go vet, full `TestSQLiteORM*" suite passes