Repository navigation
fix(userstore): delete removed history against the cutoffs just stored - #4152
Conversation
Removing a long show's episodes from history first upserts a removal watermark per episode into user_history_hidden_items, then deleted the history rows by joining that table. In the same transaction the planner still saw the table's old statistics, empty for a profile that never removed history, so it chose a nested loop that re-read the table for every history row. At 10 shows of 5,000 episodes with history on half, the delete took 46 s; later attempts were fast once statistics caught up. raiseHistoryCutoffs now returns each removed item's stored removed_before, and the delete uses those through unnest instead of the join. The same benchmark runs the delete in 0.24 s with a hash join over the profile's history. The rows deleted are the same. TestPostgresRemoveHistoryItemsDeletesTheirPlaysDB counts the stored rows after a 2,000-episode removal; the existing suites check only the listing, which hides removed plays either way. Closes Silo-Server#2997 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:
|
Quick104
left a comment
There was a problem hiding this comment.
I found no defects in the change to removeHistoryItems. It deletes the same rows as before, and I reproduced the slow plan on the base. Two cleanup findings on the new test are below. I'm fixing both in a commit on this branch, so there's no need to change anything yourself.
🤖 Claude Code (claude-opus-5-5)
The check asserts which rows the delete removes, which doesn't depend on the batch size, so 2,000 episodes only added about two seconds to every DB contract run. Also drop the executable bit from the file. 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: The change works and deletes the same rows as before. Plan check. I ran both delete statements on Postgres 18 with the issue's shape: 10 shows of 5,000 episodes, history on half, 200,000 other rows, statistics collected before 5,000 watermarks are inserted in the transaction.
Removing a single item still uses a nested loop over Scope note. #2997 reports the slow statement on bulk unmark ( Addressed findings (fixed in
Tests. Remaining for maintainers. Nobody has timed a "Remove from history" of a long show on a deployment. The evidence is the statement-level plan above plus the store tests. Validated features. No passed case on the v1.0 board covers history removal. Watch-history import reads the same cutoffs, and the upsert that writes them is unchanged apart from User manual. Unaffected; nothing user-facing changes. Other clients. N/A. The API and its responses are unchanged. Jellycompat unmark uses Fit: 9/10 · Readiness: 8/10 — conditional
🤖 Claude Code (claude-opus-5-5) |
Problem
Closes #2997
Validation tasks: none
Other clients: Apple, Android and Roku N/A. The change is in the server's history delete; the API and its responses are unchanged.
Removing a long show's episodes from history first upserts a removal watermark per episode into
user_history_hidden_items, then deleted the history rows with a join to that table. In the same transaction the planner still used the table's old statistics, empty for a profile that never removed history. It chose a nested loop that re-read the table for every history row. A first bulk unmark took tens of seconds; later ones were fast once statistics caught up.This PR has no dependencies and can merge in any order.
Approach
raiseHistoryCutoffs(internal/userstore/pgstore/progress.go) now returns each removed item's storedremoved_before, throughRETURNING.unnest($3::text[], $4::timestamptz[])instead ofuser_history_hidden_items, so the plan doesn't depend on that table's statistics.TestPostgresRemoveHistoryItemsDeletesTheirPlaysDBis added toscripts/ci/db-contracts.txt.Validation
gofmt,go vetand the changed-lines lint are clean.TestPostgresRemoveHistoryItemsDeletesTheirPlaysDBremoves a show's episodes and counts the stored history rows afterwards. The existing suites check only the listing, which hides removed plays either way.1001 plays of the removed episodes are still stored, want 0). It passes onmain's delete too, so it checks the same rows go.go test ./internal/userstore/...passes against Postgres 18.6.DELETE /api/v2/watched/{series_id}on a deployment, as in the issue; the fullmake test-go.Evidence
No visible change. The evidence is the delete's plan from a standalone probe on Postgres 18.6 (WSL2): 10 shows of 5,000 episodes with history on half, 200,000 other rows, statistics collected before 5,000 watermarks are inserted in the transaction, as in the issue.
EXPLAIN (ANALYZE, BUFFERS):mainuser_history_hidden_items2,500 timesRisks
Checklist
AI Disclosure
🤖 Generated with Claude Code
Note
Fix
removeHistoryItemsto delete removed history using stored cutoffspgstore.PostgresUserStore.raiseHistoryCutoffsnow returns a map of media-item IDs to their storedremoved_beforecutoffs, using an upsert with aRETURNINGclause. Unmarks still return nil.pgstore.PostgresUserStore.removeHistoryItemsdeletes history rows by matching item IDs and cutoffs throughunnest, instead of joininguser_history_hidden_itemsin the DELETE. Cleared progress is still deleted afterwards.TestPostgresRemoveHistoryItemsDeletesTheirPlaysDB, in history_remove_db_test.go. It covers rewatches after the removal time, other titles, and a second profile.user_history_hidden_itemsmay be handled differently. CheckremoveHistoryItemsandclearWatchedStatein progress.go.Macroscope summarized 280842e.