Repository navigation
fix(prompts): read the positions column's sums in one call past the 1000-row cap - #1099
Merged
Merged
Conversation
The Positions column read "—" for most prompts at 30d and 90d. Its sums come from prompt_position_summaries, one row per prompt and platform, and PostgREST returns at most 1000 rows per request: a busy brand's window ran past that and the rest were dropped without an error. On production, 324 of the 410 prompts mentioned in the last 30 days read "—" while their positions panel showed an average. The trend's previous window was cut the same way. readAllRpcRows (lib/supabase/rpc-pages.ts) reads a set-returning RPC a page at a time, in its key's order, until a page comes back short. The position sums read both windows with it and count a row that two pages return once. The visibility summaries, one row per prompt, read the same way, so a brand past 1000 prompts keeps them all. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reading prompt_position_summaries a page at a time got every row past PostgREST's 1000-row cap, but each page re-ran the whole aggregate: on the largest brand (4,254 rows, 3.1 s for 30 days and 4.5 s for 90) the Prompts page went from about 3 s to 15-20 s (ansvisor#1094 review). 00153 adds prompt_position_summaries_json: the same rows as 00123 in one jsonb array, which max_rows doesn't cut, so each window takes one call. As 00147, 00148 and 00152 do, it is plpgsql with force_custom_plan, work_mem and security definer with the organization check made explicit. positionSums calls it once per window, current and previous, without paging. A web build deployed before 00153 is applied falls back to 00123's rows (PGRST202), so the column doesn't go blank in between. The visibility summaries, one row per prompt, keep their paging: every brand today is one request. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Collaborator
Author
This was referenced Oct 16, 2026
…n-row-cap Co-Authored-By: Claude Opus 5.5 <[email protected]>
gkhngyk
approved these changes
Oct 20, 2026
gkhngyk
left a comment
Contributor
There was a problem hiding this comment.
Looks good, thanks.
I measured it on production as a member, on the largest brand. All columns are computed in both cases:
| Window | 00123's rows | 00153's jsonb |
|---|---|---|
| 30 days | 3.3 s | 2.5 s |
| 90 days | 1.2 s | 0.44 s |
- Both return the same 4,304 prompt × platform rows, with 0 mismatches.
- A member of another organization gets null.
Tested on top of current main: web 1,908 tests pass, tsc, lint and Prettier are clean, and schema.sql rebuilds unchanged. 00153 is applied to production.
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
Reopens #1094 with the change its review asked for. The Tracked Prompts Positions column gets every prompt's position in one call per window, instead of a page at a time.
The bug (from #1094):
prompt_position_summariesreturns a row per prompt and platform. PostgREST returns at most 1000 rows per request and drops the rest without an error. On the largest brand, 324 of the 410 prompts mentioned in the last 30 days read "—", while their positions panel showed an average.#1094's review: reading it a page at a time got every row, but each page re-ran the whole aggregate. On that brand (4,254 rows, 3.1 s for 30 days and 4.5 s for 90 days), the page went from about 3 s to 15–20 s. The review asked for a single-row
jsonbversion instead, written as plpgsql withforce_custom_planlike 00147, 00148 and 00152.What changes
00153_prompt_position_summaries_json.sqladdsprompt_position_summaries_json(p_brand_id, p_date_from, p_date_to, p_region). It returns 00123's rows as onejsonbarray, whichmax_rowsdoesn't cut. It follows 00152's pattern:stable;plan_cache_mode = force_custom_planandwork_mem = 64MB;security definerwith the organization check made explicit (the same rule asprompt_results' RLS);nullfor a caller outside the brand's organization;authenticatedandservice_roleonly.positionSums(lib/actions/prompt.ts) calls it once per window, current and previous (the trend), with no paging.PGRST202and falls back to 00123's rows, as onmaintoday. The column doesn't go blank. 00123 stays for that, and the migration notes it can be dropped once no build calls it.getPromptVisibilitySummaries(lib/supabase/rpc-pages.ts) returns one row per prompt, so every brand today is a single request; only a brand past 1000 prompts takes a second one.Related issue
#1094, closed with changes requested. This is the same branch with the review addressed.
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
Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromweb/): no errors; the 7 warnings are in files this PR does not touchyarn typecheckpasses (run fromweb/)yarn format:checkpasses (run fromweb/)Also run:
web:yarn testpasses (861 withmainmerged in), andyarn buildpasses.bash supabase/build-schema.shleavesschema.sqlunchanged.New and changed tests in
lib/actions/prompt.test.ts:PGRST202fallback to 00123;nullsums for a caller outside the organization.lib/supabase/rpc-pages.test.tsandlib/actions/tracking.test.tscover the visibility paging.Dev database:
null. The 90-day call took 16 ms on the dev data.Review: a code review flagged the deploy window (now the fallback), stale comments naming 00123, and the old RPC's purpose. All are fixed.
Commits:
fix(prompts): read every page…(#1094's paging), thenperf(prompts): read the positions column's sums as one jsonb value(this review), withmainmerged in between.🤖 Generated with Claude Code