Skip to content

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

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

zaidrain06 wants to merge 2 commits 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.

@zaidrain06
zaidrain06 force-pushed the fix-postgres-fts-chunk-slice-2133 branch from 70ced1d to e04afb0 Compare November 19, 2026 16:11

@k0nnect k0nnect left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for picking up #2133. fork prs don't get the full ci here, so i checked out e04afb0 and ran the checks locally (python 3.12, uv sync):

  • uv run ruff check . fails: src/basic_memory/repository/postgres_search_repository.py:4:8: F401 json imported but unused. the only use was the json.dumps(chunks) this pr removes.
  • uv run ruff format --check wants to reformat tests/repository/test_accepted_note_search_repository.py (the all_rows comprehension and the byte_sliced call).
  • pytest tests/repository/test_accepted_note_search_repository.py: 5 passed, 2 skipped, 1 failed. the failure is the new test_postgres_replace_fts_chunks_slices_by_rows_and_byte_cap: TypeError: SearchIndexRow.__init__() missing 2 required positional arguments: 'created_at' and 'updated_at'. it fails while building the row, so _replace_fts_chunks and the row/byte slicing asserts never run.

one gap against the issue: #2133 asks that searching text near the end of a multi-mb note returns the note. both new tests use the recording session and never reach postgres. test_postgres_refresh_entity_chunks_large_script_content in the same file looks like a good model for a postgres-backed one.

the slicing helper itself reads right to me: a single batch serializes the same as the old json.dumps(chunks), and an oversized chunk gets its own batch.

@zaidrain06

zaidrain06 commented Nov 25, 2026 •

Copy link
Copy Markdown
Author

Thanks for checking this locally, @k0nnect! Pushed aa5701b to address all four items and ran them against a live Postgres 16 + pgvector 0.8.0 instance:

  • Removed the unused json import in src/basic_memory/repository/postgres_search_repository.py (uv run ruff check . clean).
  • Formatted tests/repository/test_accepted_note_search_repository.py (uv run ruff format --check . clean).
  • Added the missing created_at / updated_at arguments to SearchIndexRow in test_postgres_replace_fts_chunks_slices_by_rows_and_byte_cap.
  • Added test_postgres_multi_mb_note_slices_fts_chunks_and_finds_tail_text (modeled after test_postgres_refresh_entity_chunks_large_script_content), which indexes >2.5 MB notes (~420 chunks) through both AcceptedNoteSearchRepository.refresh_entity and PostgresSearchRepository.index_item against Postgres, verifies via before_cursor_execute that INSERT INTO search_index_fts_chunks runs across >=3 sliced statements with all chunk rows persisted, and confirms search_repository.search(...) returns the note for a unique token at the very end of the body.

Both uv run pytest tests/repository/test_accepted_note_search_repository.py (SQLite: 6 passed, 3 skipped) and BASIC_MEMORY_TEST_POSTGRES=1 ... uv run pytest tests/repository/test_accepted_note_search_repository.py (Postgres: 9 passed) pass.

@zaidrain06
zaidrain06 force-pushed the fix-postgres-fts-chunk-slice-2133 branch from a0d065a to aa5701b Compare November 25, 2026 12:20

@k0nnect k0nnect left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-ran on aa5701b:

  • uv run ruff check . and uv run ruff format --check . are both clean.
  • sqlite: 6 passed, 3 skipped.
  • postgres (pgvector/pgvector:pg16): 9 passed, including the new multi-mb tail-search test.

all four points are covered. looks good from my side.

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

3 participants