Skip to content

reindex fails on Postgres projects above ~32k entities: unchunked find_by_ids #1442

Closed

Description

@phernandez

Summary

SearchService.reindex_vectors() builds entity_ids from find_all and passes the whole list to sync_entity_vectors_batch, which calls find_by_ids(session, entity_ids) — a plain Entity.id.in_(ids) with no chunking.

On Postgres, asyncpg caps bind parameters at 32,767. A project above that fails bm reindex and bm project index outright.

Pre-existing on main. Found while fixing a sibling defect in PR #1440 (#1414).

Why it matters more than the count suggests

bm project index is the command #1414 makes the CLI advertise as the remedy when a project is unindexed. So on a large hosted project, the tool tells the user to run a command that cannot complete — and large projects are exactly where being unindexed hurts most.

PR #1440 fixed an unchunked marker UPDATE on the same code path by reusing the existing VECTOR_HYDRATION_BATCH_SIZE bound. That removes the second failure on this path; this one fires first, so the path is still broken above the cap.

Scope

find_by_ids is shared by many callers, so this is not a one-line change confined to the reindex path — which is why it was flagged rather than folded into #1440 as a last-minute fix.

Worth deciding: whether find_by_ids should chunk internally (fixing every caller at once, at the cost of changing shared behavior) or whether the reindex path should chunk before calling it (narrower, but leaves the same trap for the next caller). The first is more in keeping with how this codebase has been fixing these — the recurring defect has been a rule stated in one place and missing in another.

Note on SQLite

Not reproducible there: the build in use allows 250,000 variables, so the same list is comfortable. That asymmetry is worth a test that asserts the bound structurally rather than relying on a driver to raise, as #1440 did for the marker update.

Activity

  1. added
    bug
    on Sep 3, 2026
  2. claude commented on Sep 3, 2026

    @claude
    Contributor

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


    Triage

    Type: Bug (code defect)

    Priority: High — find_by_ids (src/basic_memory/repository/repository.py:222) builds a plain Entity.id.in_(ids) query with no chunking, and SearchService.reindex_vectors() (src/basic_memory/services/search_service.py:774-786) passes the full unfiltered entity_ids list straight through sync_entity_vectors_batch into it — confirmed against current main. On Postgres/asyncpg (32,767 bind-parameter cap), any project above ~32k entities fails bm reindex / bm project index outright. This is also the exact remedy the CLI now points users to for unindexed projects (per #1414), so the failure hits precisely the large-project case where it matters most.

    Component: Database/sync (repository layer: find_by_ids is shared by many callers across repository/, indexing/, and index/local_dependencies.py, not just the reindex path). Not cloud-specific in the labeling sense — this reproduces on any Postgres-backed deployment (self-hosted or cloud) once entity count crosses the driver's bind-parameter limit; SQLite is unaffected only because that build allows far more bound variables.

    Complexity: Medium — the fix itself (chunking) is mechanical, but the issue correctly flags a real design decision: chunk inside find_by_ids (fixes every caller, changes shared behavior) vs. chunk only at the reindex call site (narrower, but leaves the same trap for the next caller who reaches this scale). Given this codebase's history of the same class of bug recurring caller-by-caller (per the issue and the related #1440 fix for the marker UPDATE), fixing it in find_by_ids itself seems more durable, but that's a call for whoever picks this up. Worth adding a structural test asserting the chunk-size bound rather than relying on a Postgres/asyncpg error to catch a regression, since SQLite tests won't reproduce this.

    No duplicate issues found via search.

    This is triage only — no fix has been implemented.

  3. added this to the v0.24.0 milestone on Sep 4, 2026
  4. phernandez commented on Sep 5, 2026

    @phernandez
    MemberAuthor

    Verified against current main: the lookup described here is already bounded. Repository.find_by_ids() delegates to select_by_ids(), which splits requests into SELECT_BY_IDS_CHUNK_SIZE = 500 and applies the project filter to each query. EntityRepository does not override that lookup with an unbounded implementation.

    This was implemented in #1057 (commit 7df8f84, merged July 15), fixing #1045. The find_by_ids docstring still says single query, but the implementation delegates to the batched path.

    Validation: uv run pytest tests/repository/test_repository.py -k find_by_ids --no-cov -q — 2 passed, including the regression proving requests are split across queries. This is not a full large-project PostgreSQL reindex reproduction, but the specific unchunked lookup reported here is already fixed. Closing as completed; a remaining reindex failure would need a traceback identifying a different query.

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

    bug

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions