Repository navigation
Description
Problem
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, overlap2_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_chunksgets every row in the batch (bulk_index_itemspasses the wholesearch_index_rowslist), so several large notes in one batch stack up in the same statement.- Cloud runs tenant databases with a 30 s
statement_timeout(asyncpgcommand_timeout35 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 fromindex_itemandbulk_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
chunkslist by row count and/or serialized bytes (for example, 200 chunks or about 2 MB of JSON per statement, whichever comes first). - Run one
INSERT ... FROM jsonb_to_recordsetper slice. The transaction stays atomic (fix(core): replace an entity's search rows in one transaction #1623 behavior is unchanged), and each statement stays well under the timeout. - Apply the same slicing to the accepted-note path.
Acceptance
- 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).
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…