Description
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): terminalindex_file_batchtrace. Four file indexing suboperations time out inpostgres_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_indexrow; 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_chunksand 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
claude commentedon Sep 29, 2026
Claude finished @phernandez's task in 17s —— View job
Triage
bug)cloud). The code involved is Database/sync:SearchService.index_entity_data,delete_by_entity_id, and the Postgres FTS chunk replacement.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.