Skip to content

fix(api): stop profile/dashboard track queries from seq-scanning tracks (perf regression from #932)#942

Merged
raymondjacobson merged 1 commit into
mainfrom
claude/collab-profile-perf-fix
Jun 9, 2026
Merged

fix(api): stop profile/dashboard track queries from seq-scanning tracks (perf regression from #932)#942
raymondjacobson merged 1 commit into
mainfrom
claude/collab-profile-perf-fix

Conversation

@raymondjacobson

Copy link
Copy Markdown
Member

Incident hotfix

After #932 deployed, GET /users/:id/tracks (and the dashboard monthly-listens query) started sequential-scanning the entire tracks table on every request, melting the DB on a top-QPS endpoint.

Root cause

#932 merged accepted-collaborator tracks into the profile query as:

WHERE (t.owner_id = $1 OR t.track_id IN (SELECT track_id FROM track_collaborators WHERE collaborator_user_id = $1 AND status = 'accepted'))

That OR across two different access paths (an indexed column on tracks vs. a semi-join subquery) defeats the tracks(owner_id) index. With the subquery's poor row estimate, the planner falls back to a seq scan of tracks (+ the aggregate_* joins, then sort) — per request. Not feature-gated, so it hit 100% of profile loads immediately, despite zero collaborations existing yet (frontend isn't out).

Fix

Fetch the user's accepted-collaboration track ids first — a tiny index-only lookup on the covering (collaborator_user_id, status, track_id) index that returns nothing for ~every user — then:

Same treatment applied to v1_users_listen_counts_monthly.

Correctness

Behavior is unchanged. Passing: TestGetUserTracks (no-collab path), TestAcceptedCollaborationAppearsOnProfile (merge still works), TestPendingCollaborationHiddenFromProfile, TestV1UsersListenCountsMonthly, the collaborator embed/notification tests.

Note

This is the perf risk I'd flagged in the #932 review (the OR … IN pattern). If you need relief before this merges, reverting just v1_users_tracks.go + v1_users_listen_counts_monthly.go to their pre-#932 owner-only queries is a zero-risk instant mitigation — there are no real collaborations yet, so nothing is lost.

🤖 Generated with Claude Code

#932 folded accepted-collaborator tracks into GET /users/:id/tracks (and the
dashboard monthly-listens query) as `OR t.track_id IN (SELECT ... FROM
track_collaborators ...)`. That OR across two access paths defeats the
tracks(owner_id) index, so Postgres sequential-scanned the entire tracks table
on every profile load — a severe regression on a top-QPS endpoint.

Fix: fetch the user's accepted-collaboration track ids first (a tiny index-only
lookup that returns nothing for ~all users), and only widen the query when the
set is non-empty — via an explicit `= ANY($ids)` array (bitmap-OR of the
owner_id index and the track_id PK), not a correlated subquery. For users with
no collaborations the query is byte-identical to the pre-#932 owner-only query
and plan. Same treatment for v1_users_listen_counts_monthly.

Behavior is unchanged (tests for the merge, pending-hidden, and the no-collab
path all pass).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@raymondjacobson raymondjacobson merged commit 49176eb into main Jun 9, 2026
3 of 4 checks passed
@raymondjacobson raymondjacobson deleted the claude/collab-profile-perf-fix branch June 9, 2026 21:28
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.

1 participant