Skip to content

perf(api): Keep the active feed plan driven by member roots - #2996

Merged
BYK merged 2 commits into
mainfrom
perf/unsubscribed-root-filter
Nov 9, 2026
Merged

BYK merged 2 commits into
mainfrom
perf/unsubscribed-root-filter

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

In 0.287.0, #2939 made the active feed slower. The feed token SQL in /api/versions went from 19 to 60ms p50 (span group 2de31ef2 became e29dcd55). The feed page query went from 10 to 59ms (59eb1c4a became 4cf98068).

Cause (EXPLAIN-confirmed)
conversationIsNotUnsubscribedRoot used conversation_id NOT IN (subquery). Postgres turns that into a hashed SubPlan filter on the roots scan. Before #2939, the planner probed junior_conversations_pkey once for each of the viewer's member roots. With the filter, it scans every active root, then hash-joins the result to the member roots.

Fix

  • Use NOT EXISTS with the existing memberParticipantRow helper. Postgres plans it as an anti-join, so the member-root plan from before feat(conversations): Let a participant unsubscribe from a conversation #2939 comes back.
  • Migration 0091 adds the partial index junior_conversation_participants_unsubscribed_idx (user_id, root_conversation_id) WHERE unsubscribed_at IS NOT NULL. The anti-join reads it with an index-only scan, and it holds only unsubscribed rows. The migration uses the -- junior:concurrent-index marker, so junior upgrade builds it with CREATE INDEX CONCURRENTLY.
  • Unsubscribe behavior does not change. The existing subscription scenario passes without changes.

Local measurement
Seed: 500k conversations (100k roots, 4 Threads each), 365 users, 200k participant rows. The viewer is a member of 3,032 roots, and 1,849 of them are active. Same seed for each build. Timings are p50 / p95 over 60 runs after 5 warm-up runs, through the real code (readVersions and readConversationFeedFromSql).

Build readVersions List (feed)
Before #2939 (filter removed) 54.9 / 60.1 ms 65.5 / 92.6 ms
main (NOT IN) 61.0 / 76.1 ms 94.4 / 133.8 ms
This PR 57.7 / 100.9 ms 65.7 / 75.0 ms

With 5 unsubscribed roots: main 64.2 ms readVersions / 92.8 ms list; this PR 57.9 / 69.2 ms (p50).

Feed token plan, zero unsubscribed roots
EXPLAIN (ANALYZE, BUFFERS) with bind parameters through node-postgres:

main (NOT IN), 62.2 ms
->  Hash Join  (actual rows=1849)
      ->  Bitmap Heap Scan on junior_conversations  (actual rows=66668)
            Filter: (... AND (archived_at IS NULL) AND (NOT (ANY (conversation_id = (hashed SubPlan 5).col1))))
            Rows Removed by Filter: 33333

this PR (NOT EXISTS), 54.0 ms
->  Nested Loop Anti Join  (actual rows=1849)
      ->  Nested Loop  (actual rows=1849)
            ->  Index Scan using junior_conversations_pkey on junior_conversations  (loops=3032)
      ->  Index Only Scan using junior_conversation_participants_unsubscribed_idx

before #2939, 55.2 ms
->  Nested Loop  (actual rows=1849)
      ->  Index Scan using junior_conversations_pkey on junior_conversations  (loops=3032)

Locally, the full-root scan costs only about 7 ms more on the feed token, much less than the +40 ms in prod. The plan change is the same, so the prod gap likely comes from its larger root table (inference). Check e29dcd55 and 4cf98068 after the deploy.

NOT EXISTS alone already restores the member-root plan. Without the index, the anti-join probes the primary key for each row, about 12 ms more on this seed.

Checks

  • typecheck, pnpm lint and migration-metadata:check pass.
  • After the rebase on main, the tests/integration/api/conversations suite, api/versions and the concurrent index migration tests pass: 26 files, 102 tests.
  • The conversation and MCP API integration suites passed before the rebase: 42 files, 142 tests.
  • diff-quality found no must-fix issues.
  • Server-only change, so there are no screenshots.

via @BYK.

--

View Session in Sentry

The unsubscribe filter from #2939 used NOT IN. Postgres then planned the
feed page by scanning every root instead of probing the viewer's member
roots. Use NOT EXISTS, and add a partial index on unsubscribed
participant rows for the anti-join.

Co-Authored-By: BYK <[email protected]>
@vercel

vercel Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
junior-docs Ready Ready Preview Nov 9, 2026 5:36pm UTC

Request Review

@BYK
BYK enabled auto-merge (squash) November 9, 2026 17:29
…t-filter

# Conflicts:
#	packages/junior/migrations/meta/0090_snapshot.json
#	packages/junior/migrations/meta/_journal.json
@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

6 screenshot changes — 6 changed · 0 added · 0 removed

Review screenshots in Frameshift

Conversation Captured Memories Popover · Desktop
Conversation Captured Memories Popover · Desktop
Changed
Conversation Details Watched Linked Work · Desktop
Conversation Details Watched Linked Work · Desktop
Changed
Conversation Long Transcript Touch · Mobile
Conversation Long Transcript Touch · Mobile
Changed
Conversation Unsent Answers · Desktop
Conversation Unsent Answers · Desktop
Changed
Conversation Unsent Answers · Mobile
Conversation Unsent Answers · Mobile
Changed
Settings Mcp · Desktop
Settings Mcp · Desktop
Changed

@BYK
BYK merged commit 8b3d86c into main Nov 9, 2026
43 checks passed
@BYK
BYK deleted the perf/unsubscribed-root-filter branch November 9, 2026 17:44

This branch was successfully deployed

1 active deployment
Preview – junior-docs — 3aae1285 Deployed Nov 9, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: high PR risk score: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant