Repository navigation
perf(topics): aggregate the overview in Postgres instead of downloading it - #726
Merged
Merged
Conversation
…ng it The page fetched the brand's entire 30-day result set to render twenty table rows. On the largest brand that was 33 sequential requests and 73 MB of JSON, measured end to end at 19.3 s. Two costs, separated by a controlled A/B: ~12.3 s was one over-fetched column — competitor_mentions, averaging eleven entries per row, downloaded to derive a single top competitor per topic — and ~7.0 s was the round trips, since .range() becomes LIMIT/OFFSET and page k re-walks k×1000 rows. Buffers grew linearly with offset: ~563k touches to read 32,607 rows once, against 28k for a single pass. topics_overview_aggregates does in Postgres what the client was doing in JavaScript. Only the AI Visibility Score stays here, over ~20 rows, so this page and the Insights headline keep computing it from one implementation. The competitor rollup deliberately re-scans prompt_results rather than reusing the shared CTE. `rows` is referenced several times, so Postgres materialises it, and carrying the jsonb through that materialisation spilled 32k detoasted values to temp files — 4.5 s and 7,500 temp blocks. Reading the column only where it is needed brought the function to 736 ms. Also removes the 50,000-row ceiling. The largest brand was at 32,607 and crossing it would have truncated the scan with no error, understating every figure on the page — the same class of defect as #464, which the constant was added to fix. Aggregating server-side removes the cliff instead of raising it. Verified against the live database before switching the page over: the old client-side aggregation and the new function were run side by side on the largest brand and agree exactly on all seventeen topics across answers, mention and citation answers, position sum and n, total mentions and citations, competitor mentions, active and visible prompts, and last run. Closes #721
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 Topics page fetched the brand's entire 30-day result set to render twenty table rows — 33 sequential requests, 73 MB of JSON, 19.3 s end to end on the largest brand.
topics_overview_aggregatesdoes in Postgres what the client was doing in JavaScript. Only the AI Visibility Score stays on the client, over ~20 rows, so this page and the Insights headline keep computing it from one implementation.Correctness first
The old client-side aggregation and the new function were run side by side against the live database before the page was switched over. They agree exactly on all seventeen topics, across every field: answers, mention answers, citation answers, position sum and n, total mentions, total citations, competitor mentions, active prompts, visible prompts and last run.
Two details that would have broken it silently and are handled explicitly:
AT TIME ZONE 'UTC'), because the client builds the same keys fromDate.toISOString(). Reading them in the session timezone would shift every bucket by the offset and quietly redraw the trend.The 4.5 s detour, and why the competitor rollup re-scans
The first working version was still 4.5 s with 7,500 temp blocks.
rowsis referenced several times, so Postgres materialises it — and carryingcompetitor_mentionsthrough that materialisation spills 32k detoasted jsonb values to disk. It was the same over-fetch as before, just moved inside the database.Reading that column only where it is needed, in its own scan, brought the function to 736 ms. The CTE comment says so, since the shape looks redundant otherwise.
The 50,000-row cliff is gone
TOPIC_RESULTS_MAX_ROWSstopped the scan at 50,000 rows with no error. The largest brand was at 32,607 — 65% of the way there — and crossing it would have understated every figure on the page. Same class of defect as #464, which the constant was added to fix. Aggregating server-side removes the cliff rather than raising it.visibility_scorewas also being selected and never read; it is gone with the scan.Related issue
Closes #721
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
yarn typecheck,yarn lint(6 pre-existing warnings, 0 errors),yarn format:checkandyarn buildpass locally.The types file is regenerated to include the new function — without it
supabase.rpc('topics_overview_aggregates', …)does not type-check.Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromweb/)yarn typecheckpasses (run fromweb/)yarn format:checkpasses (run fromweb/)supabase/schema.sqlregenerated viabash supabase/build-schema.sh