Skip to content

fix(search): Bound the event search backfill scan - #3956

Merged
BYK merged 1 commit into
mainfrom
fix/event-search-backfill-bounded-scan
Nov 21, 2026
Merged

BYK merged 1 commit into
mainfrom
fix/event-search-backfill-bounded-scan

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

The heartbeat backfill from #3755 stores nothing in production and loads the database.

Production, 2026-11-21 18:20–19:07 UTC, sentry/junior spans:

  • 28 runs, every one limit_reached, with 0 lexemes and 0 vectors stored.
  • The backlog scan (count(*) filter … GROUP BY … HAVING over every event of the Conversations active in the last week) took p50 141 s and max 256 s. The 10 s budget started before the scan, so each run passed its deadline before the first Conversation.
  • The 60 s claim expired before the scan ended. A new heartbeat then started another scan, so about 1.5 scans ran at all times.
  • The reported backlog grew from 39,710 to 46,691 events without lexemes, and from 41,203 to 47,905 without a vector.

What changes:

  • Scan: a run walks the Conversations with activity in the last week, newest first, 50 at a time from junior_conversations_last_activity_idx. A cursor in state lets each run continue where the last one stopped. For each Conversation, the run stores the missing lexemes with the existing per-Conversation anti-join, limited to the room left in the run. Then it checks for one event that has lexemes but no vector of the model, on the primary keys (NOT EXISTS … LIMIT 1). It embeds only when it finds one. The global count is gone.
  • Statement timeout: the page read and the per-Conversation queries run in a transaction with statement_timeout at 5 s. A Conversation whose query times out is skipped, and the pass goes on. The embedding call runs outside the transaction, so it holds no connection while it waits for the model.
  • Claim: a run holds its claim until it ends and deletes it in finally. The TTL, now 5 minutes, only frees the claim of a run that died. The 10 s budget starts after the claim and covers the page reads too.
  • Outcome: completed now means a whole pass stored and skipped nothing. limit_reached means the run stopped at its budget with Conversations left in the pass. pass_ended means a pass reached the end but stored or skipped rows, so the next run starts a new pass. Only completed sets the 10-minute done marker.

Telemetry: the backlog.* attributes are gone, because a bounded run cannot count the whole backlog. New attributes on event_search.backfill: conversation.count, skipped.count, pass.conversation.count and pass.event.count. lexeme.count, vector.count and outcome stay. The TELEMETRY.md row already covers app.event_search.backfill.*.

Query plans: local Postgres 17 seeded with 50,800 Conversations (600 active), 403k events, 356k lexeme rows and 198k vectors:

Query Plan Time
Page of active Conversations, after a cursor Bitmap Index Scan on junior_conversations_last_activity_idx, then a top-N sort of the 563 active rows 1.1 ms
Missing-vector probe, 4,000-event Conversation Bitmap Index Scan on junior_conversation_event_lexemes_pk, Nested Loop Anti Join on junior_conversation_event_embeddings_pk 0.4 ms
Missing-vector probe, fully embedded Conversation (no match) Index Scan on junior_conversation_event_lexemes_pk, anti join on junior_conversation_event_embeddings_pk 1.0 ms
Missing lexemes, 4,000-event Conversation (unchanged query) Bitmap Index Scan on junior_conversation_events_type_idx, Nested Loop Anti Join on junior_conversation_event_lexemes_pk 9.5 ms
Old global backlog scan, for comparison Parallel Seq Scan on junior_conversation_events, two nested-loop left joins, GroupAggregate 242 ms

On the same data, with a fake embedder, 15 runs (p50 455 ms, max 2.2 s) drained the seeded backlog of about 8,000 rows. The last run, a clean pass over all 600 active Conversations, returned completed.

Voyage cost: unchanged from #3755. A run embeds at most 256 events of at most about 2,000 tokens each.

via @BYK.

--

View Session [Sentry]

The heartbeat backfill counted the whole backlog in one query that joined
every event of the Conversations active in the last week. In production it
ran for 1.5 to 4 minutes, past the 10 s run budget, so each run stored
nothing. Its 60 s claim expired before the query ended, so runs overlapped.

A run now walks the active Conversations newest first, one page at a time
from the activity index, with a cursor in state. Each Conversation stores
its missing lexemes and probes for a missing vector on the primary keys,
under a 5 s statement timeout. A run holds its claim until it ends.
`completed` now means a whole pass stored nothing.

Co-Authored-By: BYK <[email protected]>
@sentry-junior
sentry-junior Bot requested a review from BYK November 21, 2026 19:29
@vercel

vercel Bot commented Nov 21, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
junior-docs Ready Ready Preview Nov 21, 2026 7:29pm UTC

Request Review

@github-actions github-actions Bot added the risk: high PR risk score: high label Nov 21, 2026
@BYK
BYK enabled auto-merge (squash) November 21, 2026 19:31
@github-actions

Copy link
Copy Markdown
Contributor

3 screenshot changes — 3 changed · 0 added · 0 removed

Review screenshots in Frameshift

Automations Runs Filters · Desktop
Automations Runs Filters · Desktop
Changed
Conversation Unsent Answers · Desktop
Conversation Unsent Answers · Desktop
Changed
System Cache Warming · Desktop
System Cache Warming · Desktop
Changed

@BYK
BYK merged commit 64cf599 into main Nov 21, 2026
58 checks passed
@BYK
BYK deleted the fix/event-search-backfill-bounded-scan branch November 21, 2026 19:41

This branch was successfully deployed

1 active deployment
Preview – junior-docs — d05e349a Deployed Nov 21, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: high PR risk score: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant