Skip to content

perf(citations): read the per-URL detail page from the citation rows - #776

Merged
gkhngyk merged 1 commit into
mainfrom
feature/citations-url-detail-rpcs
Aug 23, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
feature/citations-url-detail-rpcs

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

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.

getCitationUrlDetail was the last reader still on the pre-#732 path: it paged every answer in the window out to the app tier with the full citations jsonb 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_000 and 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 definer RPCs over the existing prompt_result_citations index, which is complete and current (813,000 rows against 813,157 jsonb elements, identical max created_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_urls at 11.9 s cold / 5.1 s warm and citations_domains at 7.0 s on the same brand — against the 8 s statement_timeout for authenticated. Same trajectory Insights was on before #773. Two fixes, both in this migration:

  • A covering index (brand_id, created_at desc) include (url_id, prompt_result_id) makes the citation scan index-only: 8,296 ms → 273 ms, taking citations_urls to 2.0 s.
  • citations_domains builds its model list from the distinct (domain, model) pairs instead of array_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_results was 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:

Check Result
citations_domains old vs new — 4 brands, unfiltered 0 differences (10,884 / 5,396 / 717 / 6,488 rows)
citations_domains — largest brand, 7d window 0 differences (9,341 rows)
citations_domains — largest brand, model + region filter 0 differences (5,061 rows)
Occurrences old vs new — single-variant URL 0 differences across 1,835 answers, incl. rank + total_sources
Occurrences old vs new — URL with 132 raw variants 0 differences across 2,643 answers, 4,012 citations both sides
Rank ordering assumption 20,000 sampled answers, all citations already in startIndex order, none missing
total_sources vs raw array length differs on 1 answer in 19,176 (an entry with no recoverable host)

Worst-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 fix
  • refactor — Code change that neither fixes a bug nor adds a feature

How to test

  • Citations → set the range to All → click any URL. The detail page should open without the long wait, and "Last seen" should match the newest tracking run rather than trailing it.
  • Switch the platform filter and the range on the detail page; counts should stay consistent with the overview row for the same URL and window.
  • Open a URL on a domain with many query-string variants (a YouTube watch link is the interesting case — the v parameter is part of the identity and must not merge different videos).

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/)
  • Commits follow Conventional Commits
  • yarn lint passes (web)
  • yarn typecheck passes (web)
  • yarn format:check passes (web)
  • yarn build passes (web)
  • schema.sql regenerated
  • Changes are focused — one concern per PR

Important

Migration 00067 has 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

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.
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Aug 23, 2026
@gkhngyk
gkhngyk merged commit 0f135c5 into main Aug 23, 2026
5 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