Repository navigation
Conversation
GetMetrics counted the five attempt buckets with five SELECTs joined by UNION ALL, so every metrics read scanned metadata_refresh_debt five times. One SELECT with a filtered COUNT per bucket gives the same labels, order and counts from a single scan.
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 43 seconds. View limit detailsLimit details: You’ve used all 4 included reviews currently available. Review configuration: ⚙️ Run configuration
📒 Files selected for processing (2)
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Related issue: N/A
Validation tasks: none
Every read of the metadata refresh metrics scans the whole
metadata_refresh_debttable five times just to count its attempt buckets.GetMetricsbuilds the five counts (0, 1, 2-3, 4-7, 8+) from five SELECTs joined byUNION ALL, and each SELECT reads the full table. The cost grows with the debt backlog, which has one row per item, season and episode with refresh debt.This change counts all five buckets in one scan.
Approach
One SELECT with a
COUNT(*) FILTERper bucket replaces the union. The labels, their order and the counts in the response are unchanged: the old query'sORDER BY labelsorted the same five labels into the same order the new code builds. Only the attempt-bucket query changes;GetMetrics' other queries are untouched.Validation
go test ./internal/metadata/ -count=1: pass, with and without a migrated local PostgreSQL 18 database.TestBuildMetadataRefreshAttemptBucketsPreservesLabelsAndCountschecks the label order and counts.TestMetadataRefreshAttemptBucketCountsSQLUsesOneAggregateScanchecks that the query readsmetadata_refresh_debtonce, withoutUNION, and with one filter per bucket in label order.go vet ./internal/metadata/andmake lint-changed: clean.836b3f9cb: passed.Benchmarks
Before is the attempt-bucket query on
main(52096ba88); after is this branch's query. Old and new returned identical counts at every size.Local runs: PostgreSQL 18.6 (Homebrew) on an Apple silicon Mac, in a disposable migrated database with
fsyncoff and 256 MB of shared buffers. Each run seededmetadata_refresh_debtwith N rows,attempt_countspread over 0 to 12, then ranVACUUM ANALYZEand three warm-up runs. The timings are 15 alternatingEXPLAIN (ANALYZE, BUFFERS)runs of each query.My own server: PostgreSQL 18, 3,742 debt rows in 624 pages. Read-only
EXPLAIN (ANALYZE, BUFFERS)inside a read-only transaction, 7 runs of each query:Limitations: these time the query alone, not the metrics endpoint, which runs other queries as well. Local numbers come from a laptop with
fsyncoff; they show the scan count, not production latency.Evidence
No user-visible change. The metrics response has the same buckets, labels, order and counts.
Evidence: https://evidence.siloserver.org/r/silo-server/pr-2083/
Risks
None identified. Same result set from one statement, no migration.
Checklist
AI Disclosure
AI-assisted with Claude Opus. I directed the task and designed the work.