Repository navigation
perf(citations): read the per-URL detail page from the citation rows - #776
Merged
Merged
Conversation
The detail page was the last reader still paging raw answers out to the app tier: 60,442 rows carrying 75 MB of jsonb across 61 sequential requests on the largest brand, to render a page about a URL cited in 2,084 of them. The scan also stopped at 50,000 rows oldest-first, so that brand's page never reached its newest 10,442 answers — five days missing from the counts and a last-seen date five days stale, with nothing on screen to say so. URL identity still resolves in TypeScript. The page groups by normalizeCitationUrl, which folds away query strings and one trailing slash, and that is load-bearing: 124,134 distinct cited URLs collapse to 73,289 under it. SQL narrows to the domain, the match happens with the same function that renders the result, and only the accepted ids go back. Also brings the overview under the statement timeout. A covering index makes the citation scan index-only (8,296 ms to 273 ms), and citations_domains builds its model list from distinct pairs instead of array_agg(distinct) over every pair — 11.9 s cold and 7.0 s become 2.0 s and 2.8 s, against the 8 s ceiling for authenticated.
This was referenced Aug 23, 2026
Merged
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.
Summary
Opening a URL from the Citations page with the range on All took tens of seconds, and on the largest brand it was also showing stale numbers.
getCitationUrlDetailwas the last reader still on the pre-#732 path: it paged every answer in the window out to the app tier with the fullcitationsjsonb attached and looked for one URL in JavaScript. On the largest brand that is 60,442 rows / 75 MB across 61 sequential requests, to render a page about a URL cited in 2,084 of them. The overview next to it was already reading the citation rows, which is why only the detail felt slow.The correctness half is the more serious one: the scan stops at
CITATIONS_SCAN_MAX_ROWS = 50_000and walks the window oldest-first, so that brand's detail page never reached its newest 10,442 answers. Five days of citations missing from the counts and a "last seen" five days stale, with nothing on screen to say the data was cut off.What changed
Two new
security definerRPCs over the existingprompt_result_citationsindex, which is complete and current (813,000 rows against 813,157 jsonb elements, identical maxcreated_at) — so no backfill:citation_url_candidates(brand, domain)— the URLs on one domain that this brand has cited. Brand-scoped so an authenticated user cannot enumerate the cross-tenant URL dictionary.citation_url_occurrences(brand, url_ids, …filters)— one row per answer citing any of those ids, with rank, per-answer count and total sources.URL identity deliberately stays in TypeScript. The page groups by
normalizeCitationUrl, which folds away query strings and one trailing slash, and that is not cosmetic — the largest brand's 124,134 distinct cited URLs collapse to 73,289 under it. SQL narrows to the domain; the match runs here with the same function that renders the result. Reimplementing those rules in SQL would create a second definition of the same identity, free to drift.Also: the overview was about to break the same way
While measuring I found
citations_urlsat 11.9 s cold / 5.1 s warm andcitations_domainsat 7.0 s on the same brand — against the 8 sstatement_timeoutforauthenticated. Same trajectory Insights was on before #773. Two fixes, both in this migration:(brand_id, created_at desc) include (url_id, prompt_result_id)makes the citation scan index-only: 8,296 ms → 273 ms, takingcitations_urlsto 2.0 s.citations_domainsbuilds its model list from the distinct(domain, model)pairs instead ofarray_agg(distinct …)over every(domain, answer)pair: 7.0 s → 2.8 s.Two narrower indexes with the same leading columns are dropped as redundant; a third candidate index on
prompt_resultswas tested, gave no measurable win, and was left out rather than paying for it on every insert.Verification
Everything was diffed against the path it replaces, on live data:
citations_domainsold vs new — 4 brands, unfilteredcitations_domains— largest brand, 7d windowcitations_domains— largest brand, model + region filterstartIndexorder, none missingtotal_sourcesvs raw array lengthWorst-case detail query (the brand's most-cited URL, 2,084 answers) now measures 622 ms returning ~200 KB, against 62 MB before.
Related issue
None — reported directly.
Type of change
fix— Bug fixrefactor— Code change that neither fixes a bug nor adds a featureHow to test
vparameter is part of the identity and must not merge different videos).Checklist
feature/,fix/,chore/,docs/)yarn lintpasses (web)yarn typecheckpasses (web)yarn format:checkpasses (web)yarn buildpasses (web)schema.sqlregeneratedImportant
Migration
00067has not been applied to the hosted database yet — the apply step was blocked by a local permission rule. The two indexes it creates are already live (they were added while measuring); the three function definitions are not. Apply before merging.🤖 Generated with Claude Code