Skip to content

Add opt-in event type index and adaptive EventLoader for sparse projections#4233

Merged
jeremydmiller merged 1 commit into
masterfrom
fix/3705-event-type-index-adaptive-loader
Apr 6, 2026
Merged

Add opt-in event type index and adaptive EventLoader for sparse projections#4233
jeremydmiller merged 1 commit into
masterfrom
fix/3705-event-type-index-adaptive-loader

Conversation

@jeremydmiller

Copy link
Copy Markdown
Member

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)

opts.Events.EnableEventTypeIndex = true;

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:

  • NormalSkip-ahead (find MIN(seq_id) matching the type filter) → Window-step (advance in 10K windows)
  • Resets to Normal when events are found
  • Logs warnings recommending EnableEventTypeIndex

Closes #3705

Test plan

  • 2 tests: index created when enabled, NOT created by default
  • Adaptive loader compiles and handles timeout detection

🤖 Generated with Claude Code

…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>
@jeremydmiller jeremydmiller merged commit 9b765ee into master Apr 6, 2026
5 of 6 checks passed
@jeremydmiller jeremydmiller deleted the fix/3705-event-type-index-adaptive-loader branch April 6, 2026 19:57
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>
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.

Rebuilding projections giving time out when having gap of matching events

1 participant