Skip to content

perf(citations): read Competitor Gaps from the citation rows - #778

Merged
gkhngyk merged 1 commit into
mainfrom
feature/citation-gaps-rpcs
Aug 23, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
feature/citation-gaps-rpcs

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

Summary

Closes #777.

getCitationGaps was the last reader on the pre-#732 path. It paged every answer in the window out to the app tier with both jsonb columns attached — 77 MB of citations + 52 MB of competitor_mentions across 62,285 rows on the largest brand — and did all co-occurrence counting in JavaScript. Like the detail page before #776, the scan stopped at 50,000 rows oldest-first, so on any brand past the ceiling the newest answers were silently missing from the gap analysis.

What changed

Two new RPCs over prompt_result_citations + citation_urls:

  • citation_gap_domains — one row per third-party domain with the gap counters (competitor answers, appears-in-ours, weighted strength, competitor names), plus a null-domain summary row carrying the answer totals so they survive empty windows.
  • citation_competitor_sources — (competitor, domain) rows for the per-competitor source map, keyed by the competitor id recorded in the mention so since-deleted competitors keep counting, exactly as today.

Aggregation semantics are unchanged: 1/distinct-domains weighting, "we are present" = brand mentioned OR own domain cited, gap counters only from answers where a competitor is present and we are absent, third-party domains only.

What crosses the boundary and why. The you/competitor split depends on the brand's current domain lists, so the caller passes those in and the two-line suffix rule runs in SQL, once per distinct domain. Display categories (forum/social/editorial/…) stay in the app tier with the same classifier as the rest of the page. Domain identity itself comes from citation_urls.domain, which is written by the same hostname extraction the page used to run per read.

Security. Both functions are security definer but carry the org-membership allowed guard from report_citation_evidence — they do not extend the unguarded pattern of the older five citation RPCs (that retrofit is a separate discussion). Verified: without a JWT the functions return only the zero-valued summary row.

Performance. The naive translation measured 8.6 s on the largest brand's all-time window — over the 8 s authenticated ceiling. Two changes brought it to 3.8 s: jsonb_path_exists answers "does this answer mention any competitor" without expanding 52 MB of arrays (full expansion now runs only where entries are needed), and the domain suffix-match runs once per distinct domain (21k) instead of once per (answer, domain) pair (581k).

Verification

All diffs on live data, full brand, zero tolerance:

Check Result
Base layer: (answer, domain) pairs, jsonb hosts vs citation_urls.domain 135,040 vs 135,040, 0 different
citation_gap_domains vs old-path replica (counts, flags, strength to 9 decimals, name arrays) 2,440 vs 2,440 rows, 0 different
citation_competitor_sources vs old-path replica 3,397 vs 3,397 rows, 0 different
competitor_mentions shape assumptions 217,586 rows: all arrays, zero string-typed mention_count
Membership guard, no JWT only the zero summary row

Migration 00068 is already applied to the hosted database. scanFilteredResults and applyModelFilter had no callers left and are deleted.

Type of change

  • fix — Bug fix
  • refactor — Code change that neither fixes a bug nor adds a feature

How to test

  • Citations → Competitor Gaps tab, range on All: should load in a few seconds and, on large brands, now includes the newest answers.
  • Compare a smaller brand's gap list before/after deploy — rows and ordering should be unchanged.
  • Platform/region/topic filters on the tab should behave exactly as before.

Checklist

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

🤖 Generated with Claude Code

getCitationGaps was the last reader paging raw answers to the app tier —
citations plus competitor_mentions jsonb, 129 MB across 62,285 rows on the
largest brand — and, like the detail page before it, stopped at 50,000 rows
oldest-first, so the newest answers were silently missing from the analysis.

Two new RPCs aggregate the co-occurrence counts over the citation rows. The
you/competitor split depends on the brand's current domain lists, so the
caller passes those in; the rest of classification stays in the app tier
where it is display-only. Both functions carry the org-membership guard from
report_citation_evidence rather than the unguarded security-definer pattern
of the older citation functions.

Verified against the path it replaces on a full brand: 2,440 gap-domain rows
and 3,397 per-competitor rows computed both ways, zero different — counts,
flags, names, and strength to nine decimals. The scan helper had no callers
left and is deleted.

Closes #777
@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 3c8ad0b 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.

Competitor Gaps still scans raw answers and silently truncates at 50,000

1 participant