You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Postgres FTS chunk insert writes a whole batch in one statement and times out on large notes #2133
On Postgres, every full-text chunk for an indexing batch goes into one INSERT ... FROM jsonb_to_recordset(:chunks) statement. Statement size grows with note size and with batch size, and nothing caps it.
POSTGRES_FTS_CHUNK_SIZE = 8_000, overlap 2_048, so the step is about 6 KB. A 5.9 MB note is about 1,000 chunks, each also carrying its script n-grams, all serialized into a single JSONB parameter.
_replace_fts_chunks gets every row in the batch (bulk_index_items passes the whole search_index_rows list), so several large notes in one batch stack up in the same statement.
Cloud runs tenant databases with a 30 s statement_timeout (asyncpg command_timeout 35 s), and runs up to 8 batch writes concurrently.
In production one tenant had three large notes (largest about 5.9 MB) that hit this limit. The client-side timeout raises a bare TimeoutError with an empty message. By then the entity row and its checksum were already committed, so the file-checksum checker skipped those notes on every later run and they never became searchable. #1954's restore_missing_search_rows() repairs that after the fact (Cloud wires it in basic-memory-cloud#3225), but the insert itself still times out for large notes.
Where
src/basic_memory/repository/postgres_search_repository.py: _replace_fts_chunks (called from index_item and bulk_index_items)
src/basic_memory/repository/accepted_note_search_repository.py: INSERT_ACCEPTED_NOTE_FTS_CHUNKS_SQL, the same single-statement shape
Proposed fix
Keep the delete as is, and write the chunks in bounded slices inside the same session and transaction:
Slice the chunks list by row count and/or serialized bytes (for example, 200 chunks or about 2 MB of JSON per statement, whichever comes first).
A test that indexes a note large enough to produce more chunks than one slice holds asserts that every chunk row is written and that the insert ran in more than one statement.
Search for text near the end of a multi-MB note returns the note.
No change for normal-size notes (one statement, same rows).
Priority: High. Large notes never become searchable on Cloud tenants. The entity row and checksum are committed before the insert fails, so later checksum runs skip them. fix(core): repair and report entities missing search rows #1954's restore_missing_search_rows() repairs that afterwards, but the insert still times out.
Complexity: Medium. It's a bounded change in two places: _replace_fts_chunks in postgres_search_repository.py and INSERT_ACCEPTED_NOTE_FTS_CHUNKS_SQL in accepted_note_search_repository.py. The slicing needs to stay inside the existing session and transaction so the fix(core): replace an entity's search rows in one transaction #1623 atomicity holds.
The issue already has a clear root cause, a proposed fix and acceptance criteria, so I don't need reproduction steps. One thing to settle at implementation time is the slice limits: row count (about 200) versus serialized bytes (about 2 MB), whichever comes first. The bare empty-message TimeoutError could also use a clearer message, but that's separate from this fix.
In src/basic_memory/repository/postgres_search_repository.py (_replace_fts_chunks) and accepted_note_search_repository.py (INSERT_ACCEPTED_NOTE_FTS_CHUNKS_SQL), all FTS chunks and their n-grams for a batch are serialized into a single INSERT ... FROM jsonb_to_recordset(CAST(:chunks AS JSONB)) statement, causing multi-megabyte JSONB binds to hit statement timeout on large notes. I have a verified patch ready that slices chunk inserts into bounded batches (~200 rows / 2 MB JSONB payload cap) inside the existing session transaction so atomicity holds. Could you assign this issue to me so I can open the PR?
Activity
claude commentedon Nov 19, 2026
Claude finished @phernandez's task in 13s —— View job
Triage
bug,cloud.restore_missing_search_rows()repairs that afterwards, but the insert still times out._replace_fts_chunksinpostgres_search_repository.pyandINSERT_ACCEPTED_NOTE_FTS_CHUNKS_SQLinaccepted_note_search_repository.py. The slicing needs to stay inside the existing session and transaction so the fix(core): replace an entity's search rows in one transaction #1623 atomicity holds.The issue already has a clear root cause, a proposed fix and acceptance criteria, so I don't need reproduction steps. One thing to settle at implementation time is the slice limits: row count (about 200) versus serialized bytes (about 2 MB), whichever comes first. The bare empty-message
TimeoutErrorcould also use a clearer message, but that's separate from this fix.Triage only: I haven't made any code changes.
zaidrain06 commentedon Nov 19, 2026
In
src/basic_memory/repository/postgres_search_repository.py(_replace_fts_chunks) andaccepted_note_search_repository.py(INSERT_ACCEPTED_NOTE_FTS_CHUNKS_SQL), all FTS chunks and their n-grams for a batch are serialized into a singleINSERT ... FROM jsonb_to_recordset(CAST(:chunks AS JSONB))statement, causing multi-megabyte JSONB binds to hit statement timeout on large notes. I have a verified patch ready that slices chunk inserts into bounded batches (~200 rows / 2 MB JSONB payload cap) inside the existing session transaction so atomicity holds. Could you assign this issue to me so I can open the PR?phernandez commentedon Nov 19, 2026
@zaidrain06 thanks go ahead and submit a pr if you'd like.
fix(search): slice Postgres FTS chunk inserts into bounded batches (b…