Skip to content

fix(prompts): read the positions column's sums in one call past the 1000-row cap - #1099

Merged
gkhngyk merged 5 commits into
ansvisor:mainfrom
cihan-gyk:fix/prompts-position-row-cap
Oct 20, 2026
Merged

gkhngyk merged 5 commits into
ansvisor:mainfrom
cihan-gyk:fix/prompts-position-row-cap

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

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_summaries returns 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 jsonb version instead, written as plpgsql with force_custom_plan like 00147, 00148 and 00152.

What changes

  • Migration 00153_prompt_position_summaries_json.sql adds prompt_position_summaries_json(p_brand_id, p_date_from, p_date_to, p_region). It returns 00123's rows as one jsonb array, which max_rows doesn't cut. It follows 00152's pattern:
    • plpgsql, stable;
    • plan_cache_mode = force_custom_plan and work_mem = 64MB;
    • security definer with the organization check made explicit (the same rule as prompt_results' RLS);
    • null for a caller outside the brand's organization;
    • granted to authenticated and service_role only.
  • positionSums (lib/actions/prompt.ts) calls it once per window, current and previous (the trend), with no paging.
  • Deploy window. A web build deployed before 00153 is applied gets PostgREST's PGRST202 and falls back to 00123's rows, as on main today. The column doesn't go blank. 00123 stays for that, and the migration notes it can be dropped once no build calls it.
  • Visibility summaries keep their paging. 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 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. Apply 00153.
  2. On a brand with more than 1000 prompt × platform rows in a window (e.g. 250 prompts answered on 5 platforms in 30 days), open Prompts → Tracked Prompts at 30d and 90d. Every prompt with a stored position shows its average, and the page makes one positions call per window.
  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 (861 with main merged in), and yarn build passes. bash supabase/build-schema.sh leaves schema.sql unchanged.

  • New and changed tests in lib/actions/prompt.test.ts:

    • positions past 1000 rows read in one unranged call per window;
    • the trend's previous window past the cap;
    • the PGRST202 fallback to 00123;
    • null sums for a caller outside the organization.

    lib/supabase/rpc-pages.test.ts and lib/actions/tracking.test.ts cover the visibility paging.

  • Dev database:

    • Dry run in a rolled-back transaction against 00123 over 30 days, with 1,274 rows: the same rows, 0 differing either way. A member of the organization gets all 1,274; a stranger gets null. The 90-day call took 16 ms on the dev data.
    • Applied after that. With 250 test prompts answered on 5 platforms (removed afterwards), all 250 showed their average at 30d and 90d, matching the answers.
  • 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), then perf(prompts): read the positions column's sums as one jsonb value (this review), with main merged in between.

🤖 Generated with Claude Code

cihan-gyk and others added 4 commits October 16, 2026 18:57
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]>
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 16, 2026
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk could you review this when you have a moment? It's #1094 with your review addressed: the positions sums now come from one jsonb call per window (migration 00153, planned with its arguments like 00152) instead of paging.

@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.

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.

@gkhngyk
gkhngyk merged commit d17e899 into ansvisor:main Oct 20, 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.

2 participants