Repository navigation
perf(catalog): read a page's episodes in one query for Jellyfin episode details - #3954
Conversation
…tail The Jellyfin episode list builds every episode's detail in one batch when a client asks for media sources, streams, chapters or people. The batch still read each episode's row on its own, so a 1000-episode page (the size Infuse gets when it sends no Limit) cost 1000 extra round trips. Read the page's episodes with one query and look each one up by ID. Episodes that no longer exist are skipped as before, and the response is unchanged.
A Jellyfin season page's episode detail batch must not issue a statement per episode. The pin compares a one-episode batch with a four-episode one.
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Silo Kody — review completeReview finished. Check the inline comments for findings and verify each suggestion against the code and tests. Reviewing changes in Silo
Review settingsReview OptionsThe following review options are enabled or disabled:
|
Problem
Related issue: N/A
Validation tasks: none (reaches #1246 C1 Jellyfin episode lists without changing the response; no case has a result yet)
When a Jellyfin client lists a series' episodes and asks for detail fields (
MediaSources,MediaStreams,ChaptersorPeople),GetEpisodeDetailsForSeriesbuilds every episode's detail in one batch, but it still read each episode's row with its own query. Infuse sends noLimiton/Shows/{id}/Episodes, so it gets every episode, up to 1,000, and paid up to 1,000 extra round trips to PostgreSQL for one request. This change reads the page's episodes in one query.This is a latency and query-count change. It doesn't change memory use.
Approach
GetEpisodeDetailsForSeriesreads the page with oneEpisodeRepository.GetByIDscall (the same columnsGetByIDreads) and looks each episode up by ID. Episodes that no longer exist are skipped as before, and a database error still fails the call. The response is unchanged.The existing pin
TestGetEpisodeDetailsForSeriesResolvesPreferencesOncePerSeries(inscripts/ci/db-pins.txt) now also checks that a four-episode batch issues as many statements as a one-episode batch. It fails on the base commit (8 statements against 5).Validation
go test ./internal/catalog/ -run 'TestGetEpisodeDetailsForSeriesResolvesPreferencesOncePerSeries$|TestTrickplayAvailabilityBatchesEpisodeDetails$|TestEpisodeDetailsFallBackAfterBatchFileFailureDB$'against a freshly migrated PostgreSQL 18.6: passes.gofmtandgo vet ./internal/catalog/: clean.Benchmarks
Measured with throwaway tests against a local, freshly migrated PostgreSQL 18.6 (loopback, Go 1.27.1, arm64 macOS), base 51b1e0a against this branch. Statement counts come from a pgx query tracer. Five runs each after one warm-up; times are wall clock for the call.
GetEpisodeDetailsForSerieson its own (the catalog test fixture):The whole
/Shows/{id}/Episodeshandler for a 1,000-episode series with noLimitandFields=MediaSources,MediaStreams,Chapters,People,Overview,Path(the jellycompat Latest test fixture, with file sizes set as the scanner writes them):Over loopback each round trip is a fraction of a millisecond. Against a database on another host or container network, the 999 saved round trips cost proportionally more.
The two response bodies (1,000 items each) are identical apart from
DateCreatedand the series image tags. Those come from row insert times, which the fixture sets withnow(), and they also differ between two runs of the same build.Evidence
Evidence: https://evidence.siloserver.org/r/silo-server/pr-3954/
No user-visible change: the response is the same. The page holds the statement counts, before and after excerpts of the same
/Shows/{id}/Episodesrequest, the full-body comparison, and the throwaway tests.Risks
None identified. A database error on the batched read fails the call, as an error on any one per-episode read did before.
Checklist
AI Disclosure
claude-opus-5-5,claude-fable-5-2claude-fable-5-2agent reviewed the diff for behavioural equivalence (errors, duplicate and missing IDs, columns and scanning, large ID arrays), the other callers, the pin's robustness and changed-line lint. It found no behaviour change. It pointed out that the pin's comment claimed a flat statement count in general, while a presentation library or translated episodes still add per-episode reads, so the comment now says it pins the episode row reads; and that the pin compared a first call with a second, so it now warms up before measuring.Note
Batch episode reads into one query in
DetailService.GetEpisodeDetailsForSeriesReplaces per-episode repository reads with a single
GetByIDsbatch call. Episodes are indexed by content ID, and requested IDs missing from the results are skipped. A batch-fetch error is returned before per-episode validation and detail construction. ExtendsTestGetEpisodeDetailsForSeriesResolvesPreferencesOncePerSeriesin detail_versions_test.go to assert SQL statement counts stay flat as episode counts grow.Macroscope summarized 7a6af62.