Skip to content

perf(citations): keep the domain table under the statement timeout on large brands - #948

Merged
gkhngyk merged 1 commit into
mainfrom
fix/citations-domains-fast
Oct 6, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
fix/citations-domains-fast

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

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:

  • It read the brand's results through 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 each prompt_results row carries its full answer text, so every row is its own page read.
  • It resolved every citation row's URL to a domain: 914k index lookups for a 30-day window.

00107_citations_domains_fast.sql:

  • adds a covering index on 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.
  • rewrites citations_domains to 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 CONCURRENTLY before the migration, so the migration's if not exists was 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:

Window Before After
24 h (page default) 4.7–8.1 s, one 500 0.22 s
7 days — 1.09 s
30 days 19.7 s 4.5 s warm; can still exceed 8 s on a cold cache

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 feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How 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

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/) — n/a, migration only
  • yarn typecheck passes (run from web/) — n/a, migration only
  • yarn format:check passes (run from web/) — n/a, migration only
  • Changes are focused — one concern per PR

🤖 Generated with Claude Code

… 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.
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 6, 2026
@gkhngyk gkhngyk mentioned this pull request Oct 6, 2026
8 of 12 tasks
@gkhngyk
gkhngyk merged commit d24de29 into main Oct 6, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-migration Adds or edits a migration — apply it to the hosted database

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant