Skip to content

perf(db): index playlist trending order#906

Merged
raymondjacobson merged 1 commit into
mainfrom
codex/db-audit-playlist-trending-index
Jun 8, 2026
Merged

perf(db): index playlist trending order#906
raymondjacobson merged 1 commit into
mainfrom
codex/db-audit-playlist-trending-index

Conversation

@raymondjacobson

Copy link
Copy Markdown
Member

Summary

  • adds a concurrent index on playlist_trending_scores(type, version, time_range, score DESC, playlist_id DESC)
  • covers /v1/playlists/trending and For You playlist candidate sources without changing API behavior

Production evidence

  • the database audit found playlist trending plans doing a parallel scan and sort for the hot ORDER BY score DESC, playlist_id DESC LIMIT ... pattern
  • the production-only idx_playlist_trending_scores_filtered puts playlist_id before score, which cannot satisfy the requested ordering
  • callers filter by type, version, and time_range, then page by score/tie-breaker; this index follows that shape directly

Verification

  • migration review only; SQL is CREATE INDEX CONCURRENTLY IF NOT EXISTS plus an index comment
  • no application code changed

@raymondjacobson raymondjacobson merged commit 56375ab into main Jun 8, 2026
5 checks passed
@raymondjacobson raymondjacobson deleted the codex/db-audit-playlist-trending-index branch June 8, 2026 20:29
raymondjacobson added a commit that referenced this pull request Jun 8, 2026
## Summary
- clamps the For You recency-decay exponent before calling `exp(...)`
- keeps very old candidates ranked as effectively zero-recency instead
of returning a 500
- adds a regression test with a year-1900 trending candidate

## Production evidence
- during the #905/#906 release watch, `/v1/users/:userId/feed/for-you`
returned 500s with `SQLSTATE 22003` underflow for several users
- a read-only prod probe reproduced the raw failure with
`exp((-1000000)::double precision)`
- the exact clamped expression against a year-1900 timestamp returned a
finite `9.85967654375977e-305` score

## Verification
- `go test ./api -run 'TestV1FeedForYou' -count=1 -timeout=3m`\n- `git
diff --check`
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