Skip to content

fix(db): stop message saves from stalling the whole server - #582

Merged
ArgaFairuz merged 1 commit into
mainfrom
fix/message-save-stalls
Nov 18, 2026
Merged

ArgaFairuz merged 1 commit into
mainfrom
fix/message-save-stalls

Conversation

@ArgaFairuz

Copy link
Copy Markdown
Collaborator

Summary

Make message saves stop blocking the server, so panels load instantly while a chat is streaming.

Why

While any chat was streaming, the server stalled for 1.3–3.3 s every 5–10 s. Every stall began within 50 ms of a message save: each save ran DELETE FROM messages_fts WHERE message_id = ?, and message_id is an UNINDEXED FTS5 column, so the delete scanned the whole index on the only thread (up to 1.8 s per save, 3–5 saves per turn).

Changes

  • Migration 088: messages_fts_map (message → FTS rowid, plus session), filled from the index in one pass
  • Every FTS delete (message, truncation, session, project, old-data cleanup) goes through the map by rowid
  • Tests for each write/delete path

Notes

Measured on a copy of a real 4 GB database (165k indexed messages): the migration runs once in 3.8 s; a save takes 1–5 ms instead of 37–1,775 ms.

While any chat was streaming, every panel took seconds to load: on a
4 GB database the server stopped answering for 1.3-3.3 s every 5-10 s.
Each stall started within 50 ms of a message save, and the main thread
spent it reading the messages_fts index from disk.

Every save first ran DELETE FROM messages_fts WHERE message_id = ?.
message_id is an UNINDEXED FTS5 column, so the delete walked the whole
index - up to 1.8 s per save, 3-5 saves per turn - on the only thread
that also serves panels, terminals and the preview.

Record each message's FTS rowid in a narrow messages_fts_map table
(migration 088) and delete through it everywhere: single messages,
truncation after a timestamp, whole sessions and whole projects. The
old-data cleanup now drops its FTS rows too; it never did. The map is
filled from the index in one pass and drops rows nothing pointed at.

Measured on a copy of a real 4 GB database (165k indexed messages):
the migration runs once in 3.8 s, and a save takes 1-5 ms instead of
37-1,775 ms.
@ArgaFairuz
ArgaFairuz merged commit c3edcad into main Nov 18, 2026
1 check passed
@ArgaFairuz
ArgaFairuz deleted the fix/message-save-stalls branch November 18, 2026 02:44
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.

1 participant