Skip to content

Cache the remaining fixed-shape timeline readers' prepared statements - #8572

Merged
ymichael merged 1 commit into
mainfrom
bb/8497-db-5-fixed-shape-timeline-readers-in-events-thr_zbjr6j6wpq
Dec 2, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/8497-db-5-fixed-shape-timeline-readers-in-events-thr_zbjr6j6wpq

Conversation

@ymichael

@ymichael ymichael commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

#8497 listed five timeline readers in packages/db/src/data/events.ts that rebuilt their Drizzle query and called SQLite prepare() on every call. Two of them, listTodoSnapshotEventRowsForThread and getLatestModelFallbackRowSinceTurnRequest, were already cached on main by #8490. The other three were not: isTimelineCursorSequencePresent, listTimelineInterruptionRows and listTimelineWindowHintsDescending. Each call paid for compiling the SQL again, and SQLite prepared the statement again.

What changed

  • The three readers now build their query once per connection through prepareCachedQuery, with placeholders for their fixed scalar arguments. This is the same pattern their sibling readers use.
  • listTimelineWindowHintsDescending changes from a raw db.all(sql…) to the query builder. It keeps the same INDEXED BY events_thread_type_sequence_idx, the same predicates and the same DESC order and limit.
  • The three readers accept DbQueryConnection, so calls inside a transaction use the same cache.
  • EXPLAIN QUERY PLAN is unchanged. All three still search their original index, and none of them builds a temp B-tree.

How you verified

  • Added the three readers to the case table in packages/db/test/data/events-preparation.test.ts. The table checks correct results for fresh bindings and inside transactions, with zero prepare() calls after warm-up. All three new cases fail on main and pass with this change.

  • pnpm exec turbo run typecheck test --filter=@bb/db: 822 tests pass.

  • pnpm exec turbo run typecheck --filter=@bb/server passes. The server timeline, request-path-statements and thread-derived-cache-equivalence tests pass: 32 files, 298 tests.

  • The issue's repro script reports 0 prepares per call for all five readers. Before this change, three of them reported 1 per call.

  • Real data: the events of a 52,700-event thread copied from a production bb.db, with 2,000 warm calls per reader. Old and new code return identical results.

    Reader main this PR
    isTimelineCursorSequencePresent 46.7 µs 6.4 µs
    listTimelineInterruptionRows 142.1 µs 58.8 µs
    listTimelineWindowHintsDescending 26.5 µs 24.6 µs

Fixes #8497

AGENT GENERATED

🤖 Generated with Claude Code

isTimelineCursorSequencePresent, listTimelineInterruptionRows and
listTimelineWindowHintsDescending rebuilt their Drizzle query and called
SQLite prepare() on every call. They now use prepareCachedQuery with
placeholders, like their sibling readers, and accept DbQueryConnection so
transactions reuse the same cache path.

Fixes #8497

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit a6e0b54 into main Dec 2, 2026
35 checks passed
@ymichael
ymichael deleted the bb/8497-db-5-fixed-shape-timeline-readers-in-events-thr_zbjr6j6wpq branch December 2, 2026 19:30
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.

db: 5 fixed-shape timeline readers in events.ts still recompile Drizzle SQL and re-prepare SQLite statements on every call

1 participant