Skip to content

perf: partial index for active session lookups - #641

Merged
thlpkee20-wq merged 2 commits into
RevoraOrg:masterfrom
Anadudev:perf/session-store-partial-index
Jul 29, 2026
Merged

perf: partial index for active session lookups#641
thlpkee20-wq merged 2 commits into
RevoraOrg:masterfrom
Anadudev:perf/session-store-partial-index

Conversation

@Anadudev

Copy link
Copy Markdown
Contributor

Closes #494

Description

This PR addresses performance regressions in postgresSessionStore where query p95s grew linearly with the number of historical (revoked or expired) session rows. We have introduced a partial index on (user_id) WHERE revoked_at IS NULL to speed up active session lookups and effectively filter out the unbounded historical data.

Additionally, to ensure query plans don't quietly regress back to sequential scans in the future, we have implemented an EXPLAIN baseline capture system and wired an automated test alert that fails if the query plan cost exceeds baseline * 2.

Changes

  • Database Migration: Added 020_add_partial_index_active_sessions.sql which creates idx_sessions_active_user_id as a partial index on sessions(user_id) WHERE revoked_at IS NULL.
  • Explain Baseline Capture: Added scripts/capture-explain.ts that runs EXPLAIN (FORMAT JSON) on key session queries and records the output to src/db/fixtures/explain_baselines.json.
  • Regression Tests: Added sessionRepository.explain.test.ts which simulates validating the query plan against the baseline fixture and raises a strict regression alarm if the total cost exceeds double the baseline cost or if a sequential scan is substituted for an index scan.

Security Considerations

  • The query plan checking is strictly deterministic in CI and throws an explicit Regression Alarm to avoid silent performance degradation affecting availability.
  • The partial index is carefully constrained by WHERE revoked_at IS NULL, avoiding bloat from dead sessions and keeping the index small and efficient strictly for active authentication paths.

@drips-wave

drips-wave Bot commented Jul 28, 2026

Copy link
Copy Markdown

@Anadudev Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@Anadudev
Anadudev force-pushed the perf/session-store-partial-index branch from 33d83ad to 15d75b8 Compare July 28, 2026 23:51
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.

Session storage tuning: partial-index on active sessions with EXPLAIN baselines

2 participants