Repository navigation
perf(citations): keep the domain table under the statement timeout on large brands - #948
Merged
Merged
Conversation
… large brands citations_domains read the brand's results through the created_at index (every brand's rows, filtered afterwards) and then from the heap, where each prompt_results row carries its full answer text; and it resolved every citation row's URL to a domain. On the largest brand the Citations page timed out. Add a covering (brand_id, created_at) index on prompt_results with the columns these reads use, and group citations by URL before resolving each distinct URL's domain once. Output is unchanged. Already applied to the hosted database.
8 of 12 tasks
This was referenced Oct 6, 2026
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
The Citations page failed to open on the largest brand.
citations_domains, which feeds the domain table, ran past the 8 s statement timeout and returned a 500. It did so even on the page's default 24-hour window.Two causes:
idx_prompt_results_created_at. That index holds every brand's results in the window, filtered afterwards. The rows then came from the heap, and eachprompt_resultsrow carries its full answer text, so every row is its own page read.00107_citations_domains_fast.sql:prompt_results (brand_id, created_at desc) include (id, platform, model_used, region, prompt_id), so the brand's results in a window come from the index alone (58 MB). Other brand-and-window reads over these columns can use it too.citations_domainsto group citations by URL first and resolve each distinct URL's domain once (124k lookups instead of 914k).The signature,
security definer, membership guard, settings and grants are unchanged.Already applied to the hosted database. The index was built there with
CREATE INDEX CONCURRENTLYbefore the migration, so the migration'sif not existswas a no-op.Verification on production data
Old and new bodies return identical rows on a brand with ~40k citations in 30 days: 3,612 domains, the same citation counts, result counts and model lists.
On the largest brand, run as an org member with the 8 s timeout:
Follow-up: for 30+ days on the very largest brands, the remaining cost is reading ~914k citation rows. That needs daily citation rollups, like the Insights rollups, or more database memory.
Related issue
None. Reported directly: the Citations page would not open.
Type of change
feat— New featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsHow to test
Open Citations on the largest brand with the default 24-hour window, then 7 days. Both load, and the domain table matches what it showed before for the same window.
Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromweb/) — n/a, migration onlyyarn typecheckpasses (run fromweb/) — n/a, migration onlyyarn format:checkpasses (run fromweb/) — n/a, migration only🤖 Generated with Claude Code