Repository navigation
perf(citations): read Competitor Gaps from the citation rows - #778
Merged
Merged
Conversation
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
This was referenced Aug 24, 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
Closes #777.
getCitationGapswas 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 ofcitations+ 52 MB ofcompetitor_mentionsacross 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 definerbut carry the org-membershipallowedguard fromreport_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
authenticatedceiling. Two changes brought it to 3.8 s:jsonb_path_existsanswers "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:
citation_urls.domaincitation_gap_domainsvs old-path replica (counts, flags, strength to 9 decimals, name arrays)citation_competitor_sourcesvs old-path replicacompetitor_mentionsshape assumptionsmention_countMigration
00068is already applied to the hosted database.scanFilteredResultsandapplyModelFilterhad no callers left and are deleted.Type of change
fix— Bug fixrefactor— Code change that neither fixes a bug nor adds a featureHow to test
Checklist
yarn lint/yarn typecheck/yarn format:check/yarn buildpass (web)schema.sqlregenerated🤖 Generated with Claude Code