fix(playlists): unblock Discover by removing SELECT DISTINCT + ORDER BY plan error

The cross-user bucket query combined SELECT DISTINCT with ORDER BY md5(...),
which Postgres rejects at plan time (SQLSTATE 42P10). buildDiscoverCandidates
returned that error on first bucket failure, so the whole Discover playlist
was skipped every nightly run — even though the random bucket pool was
healthy. Switched to GROUP BY so the md5 ordering expression no longer needs
to appear in the select list, and hardened the function so future single-
bucket failures degrade gracefully via slot redistribution instead of taking
out the whole playlist.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-12 09:50:01 -04:00
parent 5fc04f14b7
commit 1f0f7eee1a
4 changed files with 33 additions and 8 deletions
+15 -5
View File
@@ -2,7 +2,7 @@ package playlists
import (
"context"
"fmt"
"log/slog"
"github.com/jackc/pgx/v5/pgtype"
@@ -34,24 +34,34 @@ type discoverTrack struct {
// Returns up to discoverTotalSlots track IDs in the order they should
// appear in the playlist (round-robin interleaved across buckets).
// Empty slice when the library has no eligible tracks at all.
func buildDiscoverCandidates(ctx context.Context, q *dbq.Queries, userID pgtype.UUID, dateStr string) ([]rankedCandidate, error) {
//
// Individual bucket query failures are logged and treated as empty —
// the redistribution algorithm rolls the deficit into the surviving
// buckets so one broken bucket can't silently kill the whole playlist.
func buildDiscoverCandidates(ctx context.Context, q *dbq.Queries, logger *slog.Logger, userID pgtype.UUID, dateStr string) ([]rankedCandidate, error) {
dormantRows, err := q.ListDormantArtistTracksForDiscover(ctx, dbq.ListDormantArtistTracksForDiscoverParams{
UserID: userID, Column2: dateStr,
})
if err != nil {
return nil, fmt.Errorf("dormant bucket: %w", err)
logger.Warn("discover: dormant bucket failed; continuing with empty pool",
"user_id", uuidStringPL(userID), "err", err)
dormantRows = nil
}
crossUserRows, err := q.ListCrossUserLikedTracksForDiscover(ctx, dbq.ListCrossUserLikedTracksForDiscoverParams{
UserID: userID, Column2: dateStr,
})
if err != nil {
return nil, fmt.Errorf("cross-user bucket: %w", err)
logger.Warn("discover: cross-user bucket failed; continuing with empty pool",
"user_id", uuidStringPL(userID), "err", err)
crossUserRows = nil
}
randomRows, err := q.ListRandomUnheardTracksForDiscover(ctx, dbq.ListRandomUnheardTracksForDiscoverParams{
UserID: userID, Column2: dateStr,
})
if err != nil {
return nil, fmt.Errorf("random bucket: %w", err)
logger.Warn("discover: random bucket failed; continuing with empty pool",
"user_id", uuidStringPL(userID), "err", err)
randomRows = nil
}
// Adapt sqlc-generated rows into the internal struct, then apply
+4 -1
View File
@@ -294,7 +294,10 @@ func BuildSystemPlaylists(ctx context.Context, pool *pgxpool.Pool, logger *slog.
}
// 4. Discover: surface unheard tracks, biased toward dormant artists.
discoverTracks, derr := buildDiscoverCandidates(ctx, q, userID, dateStr)
// Individual bucket failures are logged inside buildDiscoverCandidates
// and redistribute as deficit; the function only returns an error for
// unrecoverable conditions, so a remaining derr here is genuine.
discoverTracks, derr := buildDiscoverCandidates(ctx, q, logger, userID, dateStr)
if derr != nil {
logger.Warn("system playlist: discover candidates load failed; skipping",
"user_id", uuidStringPL(userID), "err", derr)