Skip to content

Postgres FTS chunk insert writes a whole batch in one statement and times out on large notes #2133

Open

Description

@phernandez

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, 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).
  • Run one INSERT ... FROM jsonb_to_recordset per 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

  1. added
    bugSomething isn't working
    cloudBasic Memory Cloud
    productionObserved in production
    on Nov 19, 2026
  2. claude commented on Nov 19, 2026

    @claude
    Contributor

    Claude finished @phernandez's task in 13s —— View job


    Triage

    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.

    Triage only: I haven't made any code changes.

  3. zaidrain06 commented on Nov 19, 2026

    @zaidrain06

    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?

  4. phernandez commented on Nov 19, 2026

    @phernandez
    MemberAuthor

    @zaidrain06 thanks go ahead and submit a pr if you'd like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingcloudBasic Memory CloudproductionObserved in production

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions