Repository navigation
perf(notifications): read a profile's progress items in one query during interest rebuild - #4384
Conversation
…ing interest rebuild rebuildProfileInterest still found the items a profile has progress on by paging ListProgress 500 rows at a time with OFFSET. Every page sorts the profile's whole progress set again, so the rebuild grows with the square of the row count. A profile filled by a history import can hold hundreds of thousands of progress rows, and the rebuild runs at startup and daily. ListProgressItemIDs returns the same items in one read: the full listing's profile scope and hidden-history exclusion, without the sort or the per-row fields the rebuild never used. Both stores build it and their listing select from one shared clause, so the two cannot drift apart. The progress keyset conformance suite now also runs on Postgres and checks the new listing against the full listing and a second profile on the same account. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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:
|
Review summaryReviewed head: f58e994 I found nothing to fix. The new query returns the same items as the old paged loop, and the rebuild no longer re-sorts the whole progress set for every page. What I checked
Validated features: I couldn't check the board automatically. Going by the code, #1234 C1 (new-episode notifications) reads the interest rows this rebuild writes. The set of items that feed those rows is unchanged, so I agree with the PR's User manual: unaffected (internal performance change, no setting or visible behavior). Other clients / jellycompat: N/A. No API or client-visible change. Fit: 9/10 · Readiness: 9/10 — ready
🤖 Claude Code (claude-opus-5-5) |
Problem
Related issue: N/A
Validation tasks: none. #1234 C1 delivers new-episode notifications, which read the interest rows this rebuilds; the set of items that count as watched does not change.
Other clients: N/A (no client-visible change)
The interest rebuild still slows down with the square of a profile's watch progress. #4378 made the rebuild read completed history in one call. It still reads watch progress 500 rows at a time, and each page sorts the profile's entire progress set again. A profile filled by a history import can hold hundreds of thousands of progress rows; one with 300,000 takes about 90 seconds of database time per rebuild, which runs at every server start and daily at 04:30.
This change reads the profile's progress items in one query.
Approach
UserStore.ListProgressItemIDs(internal/userstore/store.go:131) returns the media item of every row the fullListProgresslisting returns, unordered and without the per-row fields.rebuildProfileInterestonly collected those IDs (internal/notifications/system.go:716).progressVisibleFrom(internal/userstore/pgstore/progress.go,internal/userdb/progress.go), so the hidden-history rule cannot drift between them.TestPostgresProgressPage, listed inscripts/ci/db-contracts.txt. It checks the new listing against the full listing, with a second profile on the same account whose progress must not appear.This overlaps #2888 only in
rebuildProfileInterest. #2888 keeps this progress loop, so a rebase of it takes this side of the conflict.Validation
gofmt,go mod tidy -diff, vet,make lint-changedandmake lint-router-recoverypass.make test-go(with umask 022) passes exceptinternal/metadata/nfoTestProvider_DirectoryListingFollowsChanges, which fails locally on every run; this branch does not change that package or anything it imports.userstore,userdbandnotificationstests pass, including the newTestPostgresProgressPage.make test-db-pinsreports no failures; its only problems are backup tests that skip becausepg_dumpandpg_restoreare not installed locally.verify-settings-bindings-allandverify-apiv2-web-typescould not run (nopnpmlocally); no web or settings file changed. The web gate,verify-actionlint,verify-shellcheckandverify-prometheus-ruleswere not run; this change touches none of their inputs.Evidence
Synthetic data on a scratch PostgreSQL 18 (pgvector image) database at this branch's migrations: one profile with 300,000 progress rows (100,000 in progress, 200,000 completed), 500 of them hidden from history after their last write, and a second profile on the same account with 20,000 rows. A PL/pgSQL block ran the old page loop and the new query server-side and compared the item sets. Three runs:
On a real deployment, the largest profile has about 302,000 progress rows, and the same one-query read of its visible progress items took 144 ms.
Server-side plans (EXPLAIN ANALYZE, trimmed)
Old, one page at
OFFSET 150000(each of the 600 pages sorts the whole progress set):New, the whole profile:
Risks
UserStoregains a method. Both implementations add it; test fakes that embed the interface are unaffected.Checklist
AI Disclosure
UserStoreimplementer, cancellation and row handling, large-profile plans, lint and the tests. It found no defects; nothing changed.Note
Add
ListProgressItemIDstouserstore.UserStoreand use it for interest rebuildListProgressloop innotifications.System.rebuildProfileInterestwith a singleListProgressItemIDscall that fetches all visible progress media item IDs in one queryprogressVisibleFromSQL fragments in each store to reuse the profile scope and hidden-history exclusionSILO_TEST_DATABASE_URLListProgressItemIDsis now on theUserStoreinterface, so any out-of-tree implementations must add itMacroscope summarized f58e994.