Skip to content

fix(prompts): read every page of the position and visibility summaries - #1094

Closed
cihan-gyk wants to merge 1 commit into
ansvisor:mainfrom
cihan-gyk:fix/prompts-position-row-cap
Closed

cihan-gyk wants to merge 1 commit into
ansvisor:mainfrom
cihan-gyk:fix/prompts-position-row-cap

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

At 30d and 90d, the Tracked Prompts table's Positions column read "—" for most prompts. Their positions panel showed an average for the same prompts.

The column's sums come from prompt_position_summaries, which returns one row per prompt and platform. PostgREST returns at most 1000 rows per request (max_rows in supabase/config.toml) and drops the rest without an error. A busy brand's window runs past that.

What I saw on production (read only), for a brand with 709 tracked prompts:

Range Prompts mentioned Positions shown Mentioned but "—"
24h 154 154 0
7d 175 171 4
30d 410 86 324
90d 446 99 347
  • Same prompt, two answers. "best open source AEO tools developers 2026…" read #1.8 at 24h, — at 30d, and #1.5 in its 30d panel.
  • The trend is cut too. Its previous window reads the same RPC with the same cap, so the ↑/↓ change could compare against partial data.

What changes

  • New helper. readAllRpcRows (lib/supabase/rpc-pages.ts) reads a set-returning RPC a page at a time, ordered by its key, until a page comes back short.
    • A result under 1000 rows still takes a single request, as today.
    • It stops after 100 pages (100,000 rows) and logs a warning if it gets there.
  • Positions (positionSums in lib/actions/prompt.ts) reads both windows this way.
    • Each page is its own query, so a row written between two reads can shift the next page by one. A prompt × platform row that two pages both return is counted once.
  • Visibility summaries (getPromptVisibilitySummaries in lib/actions/tracking.ts) return one row per prompt. They are read the same way, so a brand past 1000 prompts keeps its Visibility, Mentions, Citations and Runs.

No migration and no UI change.

Known, left for follow-ups

  • Cost. Every page re-runs the RPC's aggregate. At 90d, the brand above needs about 5 pages per window instead of one request that lost most of the data.
  • Server readers. server/src/lib/pulse/metrics.js and server/src/lib/signals/record.js also read prompt_visibility_summaries unpaged. That matters only past 1000 prompts; it is a separate PR.

Related issue

None. Found while checking the Tracked Prompts numbers against the positions panel on production.

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

  1. Give a brand more than 1000 prompt × platform rows in a window, e.g. 250 prompts answered on 5 platforms within 30 days.
  2. Open Prompts → Tracked Prompts at 30d. Every prompt with a stored position shows its average; none reads "—" because of the row count.
  3. Open a prompt's positions panel. Its Avg. position matches the table's.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/): no errors; the 7 warnings are in files this PR does not touch
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

Also run:

  • web: yarn test passes (727), and yarn build passes. npm run version:check passes.
  • New tests:
    • lib/supabase/rpc-pages.test.ts:
      • every row past 1000;
      • one request under a page;
      • a full last page;
      • the page limit's warning;
      • an error on a later page.
    • lib/actions/prompt.test.ts:
      • a prompt whose row comes on page 2;
      • the trend's previous window past the cap;
      • a row two pages both return, counted once.
    • lib/actions/tracking.test.ts: 1200 prompts' summaries.
  • Before/after: the page-2 test fails without the fix (expected null to be 2, the "—"). The repeated-row test fails without the de-duplication (expected 2.7 to be 3).
  • Dev database: I added 250 prompts with 5 platforms' answers each (1,274 RPC rows at 30d) and removed them afterwards.
    • Without the fix, 54 of them read "—" at 30d.
    • With it, all 250 show the average their answers give.
  • Production, read only: 24h and 7d averages matched each prompt's panel (6 of 6 at 7d). One 24h answer's #10 of 13 matched its ChatGPT answer text under computeMentionPosition's rule. At 30d, prompts the table showed matched their panel, and the "—" ones had a panel average.
  • Review: a code review raised the repeated row across pages, the silent page limit and two test gaps; all are fixed above. The cost and the server readers are named under Known.

🤖 Generated with Claude Code

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]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk could you review this when you have a moment? On production, the Tracked Prompts Positions column read "—" for 324 of the 410 prompts mentioned in the last 30 days: prompt_position_summaries returns a row per prompt and platform, and PostgREST's 1000-row cap dropped the rest. This reads every page. It touches the code from #1017; the one-scan follow-up you asked for there (chore/fold-position-sums) still needs a migration, so I kept it separate.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, the missing positions are real and the paging reads them all. One blocker, measured on production as a signed-in member of the largest brand:

The paging makes that brand's Prompts page 5× slower. prompt_position_summaries takes 3.1 s for 30 days and 4.5 s for 90 days on that brand, and returns 4,254 rows. Each page re-runs the whole aggregate, and the pages come one after another, so the current window takes about 5 × 3–4.5 s. getPromptsPageData waits for it, so the table goes from about 3 s to 15–20 s.

Only this brand is past 1000 rows today (the next largest has 720), so every other brand keeps a single request.

The single-row version you listed under Known would fix both: a jsonb result isn't subject to max_rows, so the page gets every row in one call at today's cost. Could it come in this PR, with its migration? While there, it's worth timing the body as plpgsql with plan_cache_mode = force_custom_plan, as 00147, 00148 and 00152 do. The 30-day body planned with its arguments took about 0.1 s in one run, though the timings were noisy.

cihan-gyk added a commit to cihan-gyk/ansvisor that referenced this pull request Oct 16, 2026
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]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk thanks, done as you suggested, in #1099 (same branch; I couldn't reopen this one as its author). Migration 00153 adds prompt_position_summaries_json: 00123's rows as one jsonb array, plpgsql with force_custom_plan, work_mem and security definer with the explicit org check, like 00152. positionSums makes one call per window with no paging, and falls back to 00123 on PGRST202 if the web deploys before the migration. On the dev DB it returned the same 1,274 rows as 00123 (0 differing), member/stranger checked.

gkhngyk pushed a commit that referenced this pull request Oct 20, 2026
…000-row cap (#1099)

* fix(prompts): read every page of the position and visibility summaries

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]>

* perf(prompts): read the positions column's sums as one jsonb value

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 (#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]>

---------

Co-authored-by: Cihan <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants