Skip to content

fix(search): slice Postgres FTS chunk inserts into bounded batches (#2133) - #2135

Open
zaidrain06 wants to merge 1 commit into
basicmachines-co:mainfrom
zaidrain06:fix-postgres-fts-chunk-slice-2133
Open

zaidrain06 wants to merge 1 commit into
basicmachines-co:mainfrom
zaidrain06:fix-postgres-fts-chunk-slice-2133

Conversation

@zaidrain06

Copy link
Copy Markdown

Summary

Fixes #2133

Slices PostgreSQL full-text search chunk writes (INSERT INTO search_index_fts_chunks ... FROM jsonb_to_recordset(CAST(:chunks AS JSONB))) into bounded JSONB batches (POSTGRES_FTS_CHUNK_INSERT_MAX_ROWS = 200 or POSTGRES_FTS_CHUNK_INSERT_MAX_BYTES = 2_000_000, whichever comes first) inside the caller's existing session and transaction.

Changes

  • src/basic_memory/repository/postgres_fts_chunks.py: Added POSTGRES_FTS_CHUNK_INSERT_MAX_ROWS = 200, POSTGRES_FTS_CHUNK_INSERT_MAX_BYTES = 2_000_000, and slice_postgres_fts_chunk_json_batches() to partition serialized chunk payloads by row count and UTF-8 byte size.
  • src/basic_memory/repository/postgres_search_repository.py: Updated _replace_fts_chunks to execute one INSERT ... FROM jsonb_to_recordset per bounded slice inside the same session/transaction.
  • src/basic_memory/repository/accepted_note_search_repository.py: Updated AcceptedNoteSearchRepository.refresh_entity to use the same bounded slicing for INSERT_ACCEPTED_NOTE_FTS_CHUNKS_SQL.
  • tests/repository/test_accepted_note_search_repository.py: Added tests verifying multi-statement slicing on >200-chunk notes, tail token preservation, and byte-cap splitting.

Verification

  • Normal-size notes continue executing a single INSERT statement with identical row payloads.
  • Multi-megabyte notes exceeding 200 chunks or 2 MB of serialized JSONB execute across bounded statements within the same transaction, preventing Postgres statement_timeout while preserving fix(core): replace an entity's search rows in one transaction #1623 transaction atomicity.

@CLAassistant

CLAassistant commented Nov 19, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-19T16:12:19.340175Z 70ced1d PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

…asicmachines-co#2133)

Signed-off-by: Said Yagmur <zaidrain06@users.noreply.github.com>
@zaidrain06
zaidrain06 force-pushed the fix-postgres-fts-chunk-slice-2133 branch from 70ced1d to e04afb0 Compare November 19, 2026 16:11
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.

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

2 participants