Repository navigation
fix(db): stop message saves from stalling the whole server - #582
Merged
Merged
Conversation
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.
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
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 = ?, andmessage_idis 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
messages_fts_map(message → FTS rowid, plus session), filled from the index in one passNotes
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.