perf(server): events stop maintaining two indexes no query needs #19623
main fromrefs/pull/19623/head·opened View on GitHubProblem#
Every event commit updates all 9 indexes on orchestration_events. Two of them serve no query: nothing filters, joins or sorts on correlation_id, and every reader of idx_orch_events_stream_sequence has an equal or cheaper index available. Together they cost about 2.6 WAL pages per event (part of #19105).
Change#
- Migration 063 drops
idx_orch_events_correlation_idandidx_orch_events_stream_sequence. - Three per-thread agent reads are pinned with
INDEXED BY idx_orchestration_events_agent_stream_sequence, like the file's existing pins. Without planner statistics, SQLite otherwise picksapplication_sequencefor them and ranges over every V2 event (160× slower). The pinned queries always include the partial index'sapplication_event_version = 2 AND aggregate_kind = 'thread'terms, so the pin always prepares. idx_orchestration_events_application_sequencestays:latestAgentSequence()and the shellMAX(sequence)need it.
Scope and approval#
#19105 is open and triaged by maintainers as a performance bug, naming "fewer pages per commit" as the remaining lever. This PR only removes index maintenance; it does not change commit grouping or projections.
Verification#
- Migration test: migrate to 62, then 63; both indexes are gone, the other six remain.
- Plan test (25k events, no statistics): the per-thread read, the read with
eventTypeandgetAgentReplayStatsreturn the right rows and planSEARCH ... USING INDEX idx_orchestration_events_agent_stream_sequence. It fails with the migration alone, reproducing the regression the pins prevent. - 79 persistence/migration tests and 435 tests in callers (
ShellStream,ClaudeAdapterV2,ThreadFind, recovery, ingestion) pass. Typecheck and lint clean.
Query plans before/after on a replica of the real schema (200k events), all 33 orchestration_events read shapes, in three stats modes (none / ANALYZE / the server's PRAGMA optimize). Times are ms in no-stats mode:
| Query | Before | After |
|---|---|---|
readAgentEvents(threadId) | stream_sequence, 0.78 | agent_stream_sequence, 0.67 |
readAgentEvents(threadId, eventType) | stream_sequence, 0.67 | agent_stream_sequence, 0.61 |
readAgentEvents(threadId, bounded) | stream_sequence, 0.41 | agent_stream_sequence, 0.41 |
getAgentReplayStats | stream_sequence, 0.65 | agent_stream_sequence, 0.67 |
| Drop only, no pins: the three reads above | — | application_sequence, 161–192 |
LegacyV1 existing-rows read, migrate-dev-db | stream_sequence | stream_version, same cost |
| The other 26 shapes | — | same plan |
WAL per event, one event per commit, 3,000 events:
| Pages | Bytes | Time | |
|---|---|---|---|
| Before | 23.27 | 95.9 KB | 0.286 ms |
| After | 20.68 (−11%) | 85.2 KB | 0.176 ms |
An older server opening a migrated database stays fast: the server runs PRAGMA optimize right after migrating, which gives it stats that steer those reads to the agent index (0.87 ms).
Collisions: #19451 also adds migration 063, and #19407 lists both indexes in plannerStatistics.ts. Whichever lands second renumbers or removes those two rows.
Not checked: plans against a production database with stale statistics.
Written by Claude Opus 5.5 in Claude Code.
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
Note
Written by
claude-opus-5-5on behalf of MariaThanks for this. I'm closing it as part of PR triage. Right now we're only taking focused bug fixes from outside contributors (see Read this first), and this PR is not one.
From the review: Dropping the correlation and stream-sequence indexes and forcing query indexes is performance work. Schema-replica timings and synthetic commits do not establish improvement through an actual provider turn; maintainer agreement is needed.
If you think this was a mistake, message me on Twitter at @maria_rcks and I'll take another look.
maria-rcks closed this
Sign in to join this conversation.
Important
Review skipped#
Review was skipped as selected files did not have any reviewable changes.
⚙️ Run configuration
3091d75f-5432-42ad-b713-fbb8d46d58d5📥 Commits
Reviewing files that changed from the base of the PR and between a396768240699f6cd5b38ec9049afbd2b98f4e01 and b48bfc0abdc7cf69d2c3c230a7927613b8efc975.
You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
8f0f1284-d9e0-4b52-9d84-d28c9712a097📥 Commits
Reviewing files that changed from the base of the PR and between 348f217a853dd4a23d7fa4834dedb05f545c2075 and 07ab2ce24c982ab5996dbe3da9e537a2307780e3.
📒 Files selected for processing: 8
apps/server/src/orchestration-v2/FoundationPersistence.test.tsapps/server/src/persistence/Migrations.tsapps/server/src/persistence/Migrations/055_OrchestrationV2.test.tsapps/server/src/persistence/Migrations/063_DropUnusedEventIndexes.test.tsapps/server/src/persistence/Migrations/063_DropUnusedEventIndexes.tsapps/server/src/persistence/OrchestrationEventStore.sequence.test.tsapps/server/src/persistence/OrchestrationEventStore.tsapps/server/src/persistence/reconcileV2PreviewMigration.test.tsIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.
📝 Walkthrough
The event store now selects indexes for thread-scoped reads and replay statistics. Migration 63 drops two event indexes and is included in the migration registry and migration tests.
Changes: Orchestration event indexes
apps/server/src/persistence/OrchestrationEventStore.ts,apps/server/src/persistence/OrchestrationEventStore.sequence.test.tsapps/server/src/persistence/Migrations/063_DropUnusedEventIndexes.ts,apps/server/src/persistence/Migrations.ts,apps/server/src/persistence/Migrations/063_DropUnusedEventIndexes.test.ts,apps/server/src/persistence/Migrations/055_OrchestrationV2.test.ts,apps/server/src/persistence/reconcileV2PreviewMigration.test.ts,apps/server/src/orchestration-v2/FoundationPersistence.test.tsPriority: ➖ Normal
Estimated code review effort: 3 (Moderate) | ~20 minutes
Change: Bug fix
Suggested reviewers:
juliusmarmingeMerge Risk: ⚪ Minimal · up to
07ab2The change drops two unused event indexes and pins three thread-scoped reads to a retained index. It reduces per-event write cost, and no concrete merge-blocking risk was found.
Security architecture risk: 🔵 Low · up to
07ab2The change preserves event-identity constraints and query filtering while removing two lookup indexes. No introduced security issue was established. Interrupted or concurrent migration execution and application-version rollback remain only partially validated.
Retained concerns No architecture-level concerns identified.
Security review details
Security Blast Radius
Trust Boundaries and Controls
Resilience and Maintainability Implications
Pre-merge checks:
4
✨ Finishing Touches
🧪 Generate unit tests (beta)
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Comment `@coderabbitai help` to get the list of available commands.