Repository navigation
perf(sections): bind content ID lookups as one array - #2958
Conversation
Home rows looked titles, episodes, favourites and watchlist entries up with an IN list holding one placeholder per ID. Every list length was new SQL, so each one cost a Parse/Describe round trip and a prepared statement on the connection, and a list longer than the 65,535 parameters Postgres binds failed. A personal collection rail without a default sort sends every member, and nothing caps a collection's size. The four lookups now bind the list as one text[] and compare with = ANY, as ListProgressByMediaItems already does.
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:
|
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 34 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
The Suggested replacement: I could not read the mobile validation tasks because GitHub denied access to Automated check: Macroscope check run agent (gpt-6-luna). No validation was performed. Posted via Macroscope — v1 validation impact |
Review summary (head
|
Problem
Related issue: N/A
Validation tasks: none. The changed lookups are reached by #1183 C1 and C2 and #1184 C2 (favourite and watchlist state on list and Home cards, per profile), #1252 C4 and C9 (Continue Watching and Next Up) and #1140 C4, none of which has a passed result on the board yet, and they return the same rows. #1184 C1 isn't reached: item detail reads favourite and watchlist state one item at a time, which this change doesn't touch.
Home rows look titles, episodes, favourites and watchlist entries up by content ID, and four of those lookups wrote the ID list as
IN ($1, $2, ... $n)with one bind parameter per ID. Every list length is different SQL text. With pgx's default statement cache, each new length costs an extra Parse and Describe round trip and leaves another prepared statement on the connection, where they can push other hot queries out of the cache (pgx keeps 512 per connection by default). Card counts vary from render to render, so the lengths keep changing.ListProgressByMediaItemsalready avoids this for the same reason ("Keep one statement shape for every batch size").A list also can't be longer than the 65,535 parameters Postgres binds. A personal collection rail with no default sort sends every member to the title lookup, and nothing caps a collection's size, so a collection with more than 65,535 members fails to load. I haven't seen one that large; the statement churn is the common cost.
This change binds each list as one
text[]parameter and compares with= ANY, so each lookup is one statement whatever the length.Approach
The four queries are
fetchItemsByContentIDsFilteredandfetchEpisodeTargetsByContentIDsin the section fetcher, andListFavoritesByMediaItemsandListWatchlistByMediaItemsin the Postgres user store. The fetcher callers are Continue Watching, Next Up, the Watchlist and Favourites rows (up to 10,000 IDs) and personal collection rails, and the store lookups set the favourite and watchlist state on every card list. None of the queries ordered by the list; callers reorder the rows themselves. The library, access and profile filters are unchanged, and the lookups keep using their content ID indexes (checked withEXPLAINon the prepared statements).I left
buildRecentlyAddedSingleLibraryQueryalone. Its list holds the viewer's library IDs, so its length only varies with the number of libraries.The SQLite user store still builds IN lists for the same two lookups. This change doesn't touch it.
Validation
scripts/ci/db-pins.txt: the title and episode lookups run the same single statement for 2 and 70,002 IDs and return the requested movie and episode, not the second movie and episode in the same library. Onmainthe 70,002-ID call fails withextended protocol limited to 65535 parameters.go testforinternal/sections,internal/userstore/pgstoreandinternal/userdbpasses withSILO_TEST_DATABASE_URLset, andgo vetpasses on the changed packages.355e08fa9(run): every job passed: Repository checks, Go lint, Go test, Go integration, Go DB pins (which runs the new statement-shape test) and Go DB external auth. The web jobs were skipped because no web files changed.Benchmarks
Baseline
db95c13bd(upstreammain), changed355e08fa9. Local Postgres 18.6 on an Apple M5 Pro Mac, one pgx connection over localhost TCP with pgx's default statement cache. Data: 20,000 movies and 5,000 series in one library, and a profile with 6,667 favourites. Two runs per commit gave the same picture; the table shows the first.200 calls with list lengths 1 to 200, once each, then the prepared statements left on the connection:
fetchItemsByContentIDsListFavoritesByMediaItemsLarge lists, median of 9 lists of n to n+8 IDs: the first call with each length, then the same list again.
One case is slower: around 10,000 IDs, repeating a length the connection had already prepared took about 0.5 ms longer with the array (13.5 to 13.9 ms before, 14.3 to 14.4 ms after, across both runs). Every other case was faster. Limitations: a local database with no network latency, so each avoided Parse and Describe round trip would save more on a real network; synthetic data; 9 samples per size. The harness was a temporary test file and is not part of the change.
Evidence
No screen shows anything different: the lookups return the same rows, which the tests above check. The one exception is a personal collection rail with more than 65,535 members, which failed before and now loads; I don't have a collection that large to capture.
Risks
None identified. The queries keep their filters, and each now has one prepared form instead of one per list length.
Checklist
AI Disclosure