Add opt-in event type index and adaptive EventLoader for sparse projections#4233
Merged
Merged
Conversation
…ctions When projections filter on a small subset of event types with large sequence gaps between matching events, the daemon's query can time out scanning through non-matching events. Two improvements: 1. Opt-in composite index on (type, seq_id): opts.Events.EnableEventTypeIndex = true Allows PostgreSQL to jump directly to matching event types. 2. Adaptive EventLoader with timeout-triggered fallback strategies: - Normal: standard range query with type filter - Skip-ahead: find MIN(seq_id) matching type filter, fetch from there - Window-step: advance in 10K windows until events found The loader automatically escalates through strategies on timeout, then resets to Normal when events are found. Closes #3705 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This was referenced Apr 7, 2026
This was referenced Apr 27, 2026
This was referenced May 11, 2026
This was referenced May 14, 2026
This was referenced May 25, 2026
This was referenced Jun 11, 2026
jeremydmiller
added a commit
that referenced
this pull request
Jun 11, 2026
) The skip-ahead / window-step fallback added in #4233 never engaged: 1. AutoClosingLifetime re-throws command failures through MartenExceptionTransformer, which wraps the timeout NpgsqlException/ PostgresException (SqlState 57014) into a MartenCommandException — not an NpgsqlException. isTimeoutException only inspected the outermost exception, so every real timeout was classified as non-timeout and the fallback was skipped. It now walks the inner-exception chain. 2. The escalation gate also required !EnableEventTypeIndex, but the (type, seq_id) composite index cannot serve a multi-type, globally seq_id-ordered LIMIT query, so the normal query still times out with the index on. The gate is now just _hasTypeFilter; the flag only controls the advisory warning text. Also guard the catch filter with !token.IsCancellationRequested so a deliberate daemon shutdown (OperationCanceledException) propagates cleanly instead of triggering pointless strategy escalation. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
jeremydmiller
added a commit
that referenced
this pull request
Jun 11, 2026
) Backport of the master fix to the 8.0 maintenance branch. The skip-ahead / window-step fallback added in #4233 never engaged: 1. AutoClosingLifetime re-throws command failures through MartenExceptionTransformer, which wraps the timeout NpgsqlException/ PostgresException (SqlState 57014) into a MartenCommandException — not an NpgsqlException. isTimeoutException only inspected the outermost exception, so every real timeout was classified as non-timeout and the fallback was skipped. It now walks the inner-exception chain. 2. The escalation gate also required !EnableEventTypeIndex, but the (type, seq_id) composite index cannot serve a multi-type, globally seq_id-ordered LIMIT query, so the normal query still times out with the index on. The gate is now just _hasTypeFilter; the flag only controls the advisory warning text. Also guard the catch filter with !token.IsCancellationRequested so a deliberate daemon shutdown (OperationCanceledException) propagates cleanly instead of triggering pointless strategy escalation. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When projections filter on a small subset of event types and there are large sequence gaps between matching events, the daemon's event loading query can time out scanning through non-matching events.
Two improvements:
1. Opt-in composite index on (type, seq_id)
Creates
CREATE INDEX idx_mt_events_event_type_seq_id ON mt_events (type, seq_id)— allows PostgreSQL to jump directly to matching event types within a sequence range.2. Adaptive EventLoader with timeout-triggered fallback strategies
When a timeout occurs and the event type index is NOT enabled, the loader automatically escalates:
EnableEventTypeIndexCloses #3705
Test plan
🤖 Generated with Claude Code