Repository navigation
perf(blobgc): stop each deleted media file from reading the whole cleanup queue - #3934
Conversation
…anup queue queue_deleted_media_file_blobs() asks blob_gc_queue for the latest not_before among each deleted file's queued chapter images. blob_gc_queue_chapter_file_idx finds a file's rows but does not order them, and PostgreSQL does not use a partial index's statistics for estimates, so the planner guessed 0.5% of the queue per file id and walked blob_gc_queue_due_idx backwards, reading every row for a file with nothing queued. On a queue of 949,319 chapter images each lookup took about 550ms. Replace the index with one on (file id, not_before), built and dropped concurrently in a Go migration so that a retried run keeps the index it built. The lookup then seeks to the file and reads one entry, whatever the planner estimates. The build-then-drop step the push attempt key migration used moves into a shared replaceIndexConcurrently. A DB pin checks that the lookup uses the new index. Co-Authored-By: Claude Opus 5.5 <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:
|
Quick104
left a comment
There was a problem hiding this comment.
🤖 Claude Code in T3 Code (claude-opus-5-5)
One finding, and it needs a maintainer decision rather than a change on this branch. The summary comment has the rest of the review.
|
🤖 Claude Code in T3 Code ( Review summaryReviewed head: Rechecked on Nov 14 against main at The defect is real on main. I tested on a local PostgreSQL 18 with main's schema and a queue of 949,319 chapter images across about 20,000 files. For a file with nothing queued, the trigger's This branch fixes it. On the same queue, the migration built the new index in 0.83 s. In the trigger's own plan (captured with Migration. The migration follows DEVELOPMENT.md's rule for replacing an index (a Go migration that builds through Tests. Merge order: #4205, which fixes #4071, merged first. I then merged main into this branch ( Validated features: No passed case regresses. #1155 C1/C2 (chapter thumbnails) and #1231 C2 (autoscan missing files) share the delete trigger and queue with this change, and they behave the same. User manual: unaffected. No setting, default, or described behavior changes. Other clients: N/A. This is a server-only database change with no client-visible behavior. Cross-provider review: One round on this head (Codex Fit: 9/10 · Readiness: 9/10 — ready (assuming CI is green)
|
|
Thanks for the pull request. Every Silo pull request needs evidence that shows the change working, and this one still has none 24 hours after it opened, so it is closed without review. That isn't a judgment of the work. The Evidence check's Details link explains how to publish screenshots, a recording, or test results. Publish them, then open a new pull request or ask a maintainer to reopen this one. The contributing guide has the details. |
Problem
Related issue: N/A
Validation tasks: none
Deleting media files gets slower as
blob_gc_queuegrows, because every deleted file reads the whole queue. On a server whose queue held 949,319 chapter images, deleting two libraries with 540,000 files between them ran at one 5,000-file batch every 45 minutes. That is about three and a half days in all, and a library deletion job stops after two hours.For each deleted file,
queue_deleted_media_file_blobs()asks the queue for the latestnot_beforeamong that file's chapter images:max(not_before)over rows whereprefix LIKE 'chapter-images/%'andsplit_part(prefix, '/', 2)is the file id.blob_gc_queue_chapter_file_idxfinds those rows but does not order them bynot_before, and PostgreSQL does not use a partial index's statistics to estimate a clause. The planner falls back to its fixed 0.5% guess (4,747 rows for one file id on that queue, against about 17), expects a match within the first few rows, and answersmax()by walkingblob_gc_queue_due_idxbackwards. Most deleted files have nothing queued, so the walk reads every row: about 550 ms per file.Approach
A Go migration replaces
blob_gc_queue_chapter_file_idxwithblob_gc_queue_chapter_file_not_before_idxon(split_part(prefix, '/', 2), not_before), with the sameWHERE. That index answersmax(not_before)for one file directly: the lookup seeks to the file and reads one entry, whatever the planner estimates. No other query uses the old index.As DEVELOPMENT.md asks for replacing an index, the migration builds the new index concurrently before it drops the old one concurrently, so a retried run keeps what it built. The push attempt key migration already did exactly that; the step is now a shared
replaceIndexConcurrentlythat both use.Extended statistics on the expression, as
20260904144457_continue_watching_planner_stats.sqladds for watch progress, also fixed the queue above. On a queue of 2,000 files with 300 images each, the planner still chose the backward walk, so this change uses the index.Validation
--migrate-onlyand--migrate-down-to: Down restores the old index's definition exactly. The new DB pin passes after each Up and fails after the Down; on a freshly vacuumed queue without the new index, the lookup walksblob_gc_queue_due_idx. The push attempt key migration's retry test passes on the shared helper.make test-db-pins: all 784 pins pass.make test-go: passes except two tests that fail only in the full run on the shared test database and pass alone, on this branch and onmain:TestDirectoriesReadsOnlyTheScannedSubtreeDBininternal/themesongs, a generic-plan test onmedia_files, andTestPostgresConcurrentProfileAndCollectionDeletesininternal/userstore/pgstore, which hit a blocking trigger another package's test had installed.go build ./...,gofmt,go mod tidy -diff,go vet ./...,make lint-changed,make lint-router-recovery,make migrate-validate,make verify-migrations,make verify-migration-ledger,make verify-local-pathsandmake verify-case-collisionspass.Evidence
No user-visible change. The evidence page has the PR's test failing on main and passing on this head, and the delete trigger's lookup on a 900,000-row queue: 837 ms per file on main, 0.085 ms here.
Evidence: https://evidence.siloserver.org/r/silo-server/pr-3934/
Risks
The concurrent build and drop do not block queue writes, but each waits for transactions older than it, such as the blob collector's batch while it deletes from storage. A long-running transaction elsewhere delays startup on the replica that applies the migration until it ends. The new index is larger than the old one (29 MB against 7.6 MB on the queue above), because distinct (file id, not_before) pairs leave B-tree deduplication nothing to merge. Down restores the old index.
Checklist
AI Disclosure
claude-opus-5-5.claude-opus-5-5) reviewed the diff in three passes on scratch databases seeded like the queue above, and reproduced the slow lookup through the real trigger function. Its findings changed the fix twice. Extended statistics still produced the walk on a queue of few files with many images each, so this uses the composite index it suggested. A plain build-then-drop in one transaction stalledmedia_fileswrites and deadlocked against the blob collector, so the migration now builds and drops concurrently in Go. The last pass tested interrupted builds and drops, retries and the collector race and found no blockers; its two nits are fixed.Note
Add composite partial index on
blob_gc_queueto speed up media file cleanupsplit_part(prefix, '/', 2)andnot_before, built withCREATE INDEX CONCURRENTLYin blob_gc_queue_chapter_file_index_migration.go.database.replaceIndexConcurrentlyin invalid_index_rebuild.go; the push-attempt key migration now uses this helper.not_beforelookup does not use the new index.blob_gc_queue_chapter_file_idx-style single-expression index; reverse migration rebuilds it. Existing deployments run the index rebuild concurrently during migration.Macroscope summarized ee87fce.