Repository navigation
fix(scanner): keep the audiobook duplicate lookup fast before statistics catch up - #4280
Conversation
…ics catch up The lookup that decides whether a new audiobook is a moved copy of an indexed one joined people, credits, and items in an order the planner chose. During the first audiobook scan on a server with a large catalog, planner statistics still say there are no audiobooks, and the chosen plan compares every author credit with every audiobook, so each lookup gets slower as the library fills. Resolve the author and narrator names first, materialize the items credited to both, and read media_items by primary key. The type check moves to the caller so the planner cannot pick the partial books index. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
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:
|
| return nil | ||
| } | ||
| if audiobookDedupeTitlesMatch(candidateTitle, cleanTitle) { | ||
| if candidateType == "audiobook" && audiobookDedupeTitlesMatch(candidateTitle, cleanTitle) { |
There was a problem hiding this comment.
Non-audiobooks can crowd valid duplicates out of the candidate limit
Enough better-ranked non-audiobook rows can hide a moved audiobook, giving it a new identity and losing its listening progress. Apply the audiobook type filter before the 25-row limit while preserving a primary-key-driven lookup plan.
Prompt for LLM
File internal/scanner/audiobook_scan.go, line 1402:
Non-audiobooks can crowd valid duplicates out of the candidate limit
Non-audiobook rows now consume the SQL query's 25-row limit before the Go type check, so enough better-ranked matches can hide a moved audiobook and cause it to be assigned a new identity, losing its associated listening progress. Apply the audiobook type filter before the limit while preserving a primary-key-driven lookup plan.
Talk to Kody by mentioning @kody
Was this suggestion helpful? React with 👍 or 👎 to help Kody learn from this interaction.
There was a problem hiding this comment.
Fixed in 7d0c185. The type test is back in SQL, applied to the materialized candidate rows before the limit, so a non-audiobook item can no longer take a slot. media_items is still read by primary key inside the materialized step, which keeps the plan independent of statistics.
There was a problem hiding this comment.
Thanks for the fix. Applying the audiobook type filter before the limit addresses the reported issue, while the materialized primary-key lookup preserves the intended query plan.
Filter on the materialized candidate rows so a non-audiobook item cannot take one of the 25 slots, while media_items is still read by primary key. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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
Other clients: N/A (no client-visible change)
The first audiobook scan on a server that already holds a large catalog gets slower with every book it adds. For each new book the scanner asks whether it is a moved copy of a book already indexed. That lookup joined people, credits, and items in whatever order the planner chose. PostgreSQL refreshes a table's statistics only after about a tenth of its rows change, so for much of that first scan the statistics still say there are no audiobooks and no author or narrator credits. The planner then starts from every author credit and checks each against every audiobook, normalizing each title with two regular expressions. One lookup took over 20 seconds, every scan worker was waiting on it, and the database host ran at full CPU.
This change makes the lookup's plan independent of statistics.
Approach
The query now resolves the author and narrator names through the
LOWER(name)index first, materializes the items credited to both, and readsmedia_itemsby primary key into a second materialized set. Thetype = 'audiobook'test runs on that set. Tested onmedia_itemsitself, it lets the planner choose a partial books index and scan every audiobook once per candidate, which is the same trap.The result set is unchanged: audiobooks with an author credit and a narrator credit matching the tagged names, filtered by year, duration, and title as before, in the same order and with the same limit.
Other statements that run once per book were not slow in the same scan and are not changed.
Validation
go build ./...,gofmt -l,go vet,make lint-changed,make verify-size-ratchet BASE_REF=origin/main,make verify-local-paths: clean.go test ./internal/scanner/for the audiobook tests against a migrated PostgreSQL 18 with pgvector: pass, including the contract that a renamed folder still finds its item through this lookup.Evidence
Nothing visible changes. Reproduced on a throwaway PostgreSQL 18 with autovacuum off and synthetic rows: a movie catalog with cast credits, analyzed, then audiobooks with one author and one narrator each inserted without a new
ANALYZE, as during a first audiobook scan.EXPLAIN (ANALYZE, BUFFERS)under a generic plan for the same book,mainat1452ea2d10against this branch at7d0c185fca:The same plan and timing difference showed on a real server whose statistics predated its first audiobook scan, where the old and new queries returned identical rows for a sample of indexed books.
Risks
Checklist
AI Disclosure
🤖 Generated with Claude Code
Note
Rewrite
scanner.audiobookDuplicateCandidateSQLwith materialized CTEsMATERIALIZEDCTE withINTERSECTto find content IDs credited to both the matching author and narrator, instead of joiningmedia_items,item_people, andpeopledirectly.MATERIALIZEDCTE joins those IDs back tomedia_itemsand carriescontent_id,title,type, andyearinto the existing downstream filtering and ordering.media_itemsjoin, so multiple matching credit rows no longer produce repeated candidate rows.Macroscope summarized 7d0c185.