Skip to content

perf(catalog): read a page's episodes in one query for Jellyfin episode details - #3954

Merged
Quick104 merged 2 commits into
Silo-Server:mainfrom
blurbery:perf/jellycompat-episode-detail-batch
Nov 11, 2026
Merged

Quick104 merged 2 commits into
Silo-Server:mainfrom
blurbery:perf/jellycompat-episode-detail-batch

Conversation

@blurbery

@blurbery blurbery commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

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, Chapters or People), GetEpisodeDetailsForSeries builds every episode's detail in one batch, but it still read each episode's row with its own query. Infuse sends no Limit on /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

GetEpisodeDetailsForSeries reads the page with one EpisodeRepository.GetByIDs call (the same columns GetByID reads) 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 (in scripts/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.
  • gofmt and go vet ./internal/catalog/: clean.
  • CI on this branch: see Checks.
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.

GetEpisodeDetailsForSeries on its own (the catalog test fixture):

Episodes Statements before Statements after Time before Time after
1 5 5 2 ms 1 ms
4 8 5 1 ms 1 ms
1,000 1,004 5 45-56 ms 12-16 ms

The whole /Shows/{id}/Episodes handler for a 1,000-episode series with no Limit and Fields=MediaSources,MediaStreams,Chapters,People,Overview,Path (the jellycompat Latest test fixture, with file sizes set as the scanner writes them):

Statements Time
Before 1,019 87-112 ms
After 20 51-63 ms

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 DateCreated and the series image tags. Those come from row insert times, which the fixture sets with now(), 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}/Episodes request, 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

  • I read and can explain the complete diff.
  • This pull request addresses one concern.
  • Each test this pull request adds meets "Decide what to test" in CONTRIBUTING.md.
  • The Evidence section shows every change a user can see, or says there is none.

AI Disclosure

  • Harness: Claude Code (desktop app)
  • Tool(s): Claude Code
  • Model(s): claude-opus-5-5, claude-fable-5-2
  • Involvement: AI-assisted
  • Adversarial review: a separate claude-fable-5-2 agent 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.GetEpisodeDetailsForSeries

Replaces per-episode repository reads with a single GetByIDs batch 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. Extends TestGetEpisodeDetailsForSeriesResolvesPreferencesOncePerSeries in detail_versions_test.go to assert SQL statement counts stay flat as episode counts grow.

Macroscope summarized 7a6af62.

…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.
@coderabbitai

coderabbitai Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Episode detail construction now loads requested episodes with one batch read and builds results in request order. Missing episodes are skipped, and a batch-read error fails the call. The updated test compares query counts for one and four episodes.

Changes: Episode detail reads

Layer / File(s) Summary
Batch episode reads and count validation
internal/catalog/detail.go, internal/catalog/detail_versions_test.go, scripts/ci/db-pins.txt
GetEpisodeDetailsForSeries uses one GetByIDs call, indexes returned episodes by content ID, and skips requested IDs that are absent. The test compares user-store lookup and SQL statement counts for one and four episodes. The CI comment documents the once-per-series expectation for both lookups and episode reads.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Refactor

Possibly related PRs

Suggested labels: go, impact: perf

Suggested reviewers: quick104

Merge Risk: Minimal · up to 7a6af

No actionable issue remains in the episode-detail batch change; it is ready to merge after normal checks.

Pre-merge checks: Passed 5

Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: replacing per-episode reads with one query for Jellyfin episode details.
Description check Passed The description is directly related to the changeset. It explains the performance problem, batching approach, validation, benchmarks, and expected unchanged response.
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 1 functions across 2 files. (1 skipped: 1 unsupported.)

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 175a0a52-a78e-4e88-aae6-8d0d6d47bf5e
Commits

Reviewing files that changed from the base of the PR and between 75e10d3 and 7a6af62.

Files selected for processing: 3

  • internal/catalog/detail.go
  • internal/catalog/detail_versions_test.go
  • scripts/ci/db-pins.txt

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 1 remain after this review.

@silo-triage silo-triage Bot added the size/S 0–49 added + deleted lines (all files); change volume, not priority. label Nov 11, 2026
@silo-kody

silo-kody Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Silo Kody — review complete

Review finished. Check the inline comments for findings and verify each suggestion against the code and tests.

Reviewing changes in Silo
  • Include the related issue, expected behavior, and validation steps in the PR description.
  • For API changes, describe the effect on Apple and Android clients and Jellyfin compatibility.
  • For plugin changes, identify the affected SDK contract, plugin, and catalog entry.
  • Follow this repository's AGENTS.md and CONTRIBUTING.md.
  • Request another review with @kody start-review in a PR comment.
  • React with 👍 or 👎 to give feedback on individual suggestions.
Review settings
Review Options

The following review options are enabled or disabled:

Options Enabled
Bug ✅
Performance ✅
Security ✅
Business Logic ❌

⚠️ Note: some checks couldn't be completed in this run, so auto-approval (when enabled) was skipped. Check the details at https://kodus.siloserver.org/pull-requests and then comment @kody review on this PR to retry once the issue is resolved.

@blurbery
blurbery marked this pull request as ready for review November 11, 2026 03:21
@coderabbitai coderabbitai Bot added go Pull requests that update go code impact: perf Unusable slowness on normal hardware labels Nov 11, 2026
@Quick104
Quick104 merged commit b4807da into Silo-Server:main Nov 11, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

go Pull requests that update go code impact: perf Unusable slowness on normal hardware size/S 0–49 added + deleted lines (all files); change volume, not priority.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants