Skip to content

feat(sqlite): self-heal FTS index corruption on writes - #406

Merged
medcl merged 1 commit into
mainfrom
sqlite-fts-self-heal
Aug 24, 2026
Merged

feat(sqlite): self-heal FTS index corruption on writes#406
medcl merged 1 commit into
mainfrom
sqlite-fts-self-heal

Conversation

@medcl

@medcl medcl commented Aug 23, 2026

Copy link
Copy Markdown
Member

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, while PRAGMA integrity_check still reports ok - only the FTS shadows are damaged.

All SQLite ORM write/delete statements now go through execHealing:

  1. execute the statement
  2. on a corruption error (malformed/corrupt in 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 rows
  3. retry the statement once; only a second failure surfaces to the caller

Rebuilds 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
  • E2E: reproduce a stuck record on a killed instance, delete via UI -> heals automatically

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.
@medcl
medcl merged commit 61cb99c into main Aug 24, 2026
5 checks passed
@medcl
medcl deleted the sqlite-fts-self-heal branch August 24, 2026 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants