Skip to content

Preserve prior search results when an entity refresh fails #1621

Closed

Description

@phernandez

Problem

A failed search refresh can remove a note's last valid full-text projection. This is a correctness bug that becomes visible when the tenant database is slow or a replacement query fails.

In the deployed Core revision 12011fb038572e6b7dcfd004ba249d719259a757, SearchService.index_entity_data reads Markdown content, then calls repository.delete_by_entity_id, then indexes replacement entity, observation, and relation rows. delete_by_entity_id commits its transaction. The replacement work uses later transactions. The search_index_fts_chunks foreign key cascades deletion from search_index. A timeout or other exception during replacement therefore leaves the source entity intact but can leave its search rows absent.

Production evidence

  • Cloud worker release: 116e0ecc3461c712934189faf8fdd03c694ac13e, pinned to the Core revision above.
  • Logfire issue #2932 (c49c14aa-bd72-449a-a1c6-79cd511bdbb4): terminal index_file_batch trace. Four file indexing suboperations time out in postgres_search_repository._replace_fts_chunks; the PGQueuer job exhausts six attempts.
  • Read-only production counts on 2026-09-29: all 6,322 recovered Markdown files had entity rows. At 18:00 UTC, 129 lacked an entity search_index row; at 18:11 UTC, 87 still lacked one. The count fluctuated as retries and relation refreshes ran. This is a real search-availability gap, not just an exception label.
  • The tenant database was under heavy load: approximately 13 GB allocated to search_index_fts_chunks and its indexes/TOAST, versus 1.4 GB in the source tenant despite fewer live chunks. Autovacuum was processing millions of dead TOAST items, and active sessions waited on Neon read I/O. Database load appears to trigger the timeout; it does not make loss of the prior search projection acceptable.

Fix scope

Make replacement of an entity's search rows and its full-text chunks atomic, or stage a complete replacement and switch it into place only after it succeeds. Preserve the previous searchable projection on a replacement failure. Keep entity, observation, relation, and FTS rows consistent across a successful refresh. Do not address this by only increasing asyncpg timeouts, suppressing the exception, or lowering Logfire severity.

Acceptance

  • A regression test forces a failure after replacement begins (including a failure while writing FTS chunks) and confirms the previous entity/observation/relation search rows and FTS chunks remain queryable.
  • A successful refresh replaces stale rows and chunks exactly once, including removal of old observation/relation rows.
  • A first-time indexing failure leaves no partial projection and remains retryable.
  • The production recovery is tracked separately: after database load settles, repair the affected derived indexes and verify coverage reaches the recovered entity count. Do not close this issue solely because retries later restore the rows.

Activity

  1. added
    bug
    production
    on Sep 29, 2026
  2. claude commented on Sep 29, 2026

    @claude
    Contributor

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


    Triage

    • Type: Bug (labeled bug)
    • Component: Cloud (labeled cloud). The code involved is Database/sync: SearchService.index_entity_data, delete_by_entity_id, and the Postgres FTS chunk replacement.
    • Priority: High. A failed refresh can remove a note's last valid search projection. In production, 87 to 129 of 6,322 notes were missing search rows during the incident.
    • Complexity: Medium. The delete and replace steps need to run in one transaction, or the replacement needs to be staged and swapped in. It also needs regression tests for a failure while writing FTS chunks, a successful refresh, and a first-time indexing failure.
    • Duplicates: I searched for related issues and found none.

    The issue is clear and has acceptance criteria, so I don't need reproduction steps. As you noted, production recovery (repairing the derived indexes and verifying coverage) needs separate tracking, and this issue shouldn't be closed on retries alone.

    This was triage only. I made no code changes.

  3. added
    cloud
    on Sep 29, 2026
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

    bugcloudproduction

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions