Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.
TypeScript aeoai-agentai-searchanswer-engine-optimizationbrand-monitoringchatgptclaudecopilotdockergeminigeogoogle-ai-overviewsgrokmcpnextjsopen-sourceperplexityseosupabasetypescript
fix(prompts): read the positions column's sums in one call past the 1000-row cap (#1099)
Cihand17e899f4792
diff --git a/supabase/migrations/00153_prompt_position_summaries_json.sql b/supabase/migrations/00153_prompt_position_summaries_json.sqlnew file mode 100644index 000000000..5f20e7338--- /dev/null+++ b/supabase/migrations/00153_prompt_position_summaries_json.sql@@ -0,0 +1,91 @@+-- The Positions column's sums in one value (#1094 review).+--+-- prompt_position_summaries (00123) returns a row per prompt and platform. The+-- largest brand's 30 days come to 4,254 rows, past PostgREST's max_rows of+-- 1000, so the rest were dropped without an error and most of its prompts+-- read "—". Reading it a page at a time re-ran the whole aggregate per page:+-- 3.1 s for 30 days and 4.5 s for 90 days, five times over.+--+-- This returns the same rows as one jsonb array, which max_rows doesn't cut,+-- so the page gets every row in a single call. As 00147, 00148 and 00152 do,+-- it is plpgsql with force_custom_plan, so every call is planned with its real+-- window, and work_mem; and security definer with the organization check made+-- explicit. The rows and their counting rules are 00123's.+--+-- prompt_position_summaries (00123) stays for a web build deployed before this+-- migration, which falls back to it; drop it once no build calls it.++create or replace function public.prompt_position_summaries_json(+ p_brand_id uuid,+ p_date_from timestamptz default null,+ p_date_to timestamptz default null,+ p_region text default null+)+returns jsonb+language plpgsql+stable+security definer+set search_path to 'public'+set work_mem to '64MB'+set plan_cache_mode to 'force_custom_plan'+as $$+declare+ v_result jsonb;+begin+ if auth.role() is distinct from 'service_role' and not exists (+ select 1 from public.brands b+ join public.profiles pf on pf.organization_id = b.organization_id+ where b.id = p_brand_id and pf.id = auth.uid()+ ) then+ return null;+ end if;++ select coalesce(jsonb_agg(jsonb_build_object(+ 'prompt_id', s.prompt_id,+ 'platform', s.platform,+ 'runs', s.runs,+ 'mention_answers', s.mention_answers,+ 'position_sum', s.position_sum,+ 'position_count', s.position_count,+ 'entity_sum', s.entity_sum,+ 'entity_count', s.entity_count)), '[]'::jsonb)+ into v_result+ from (+ select pr.prompt_id,+ pr.platform,+ count(*) as runs,+ count(*) filter (where pr.mention_count > 0) as mention_answers,+ coalesce(sum(pr.mention_position) filter (+ where pr.mention_count > 0 and pr.mention_position > 0), 0)::double precision+ as position_sum,+ count(*) filter (+ where pr.mention_count > 0 and pr.mention_position > 0) as position_count,+ coalesce(sum(pr.mentioned_entity_count) filter (+ where pr.mention_count > 0 and pr.mention_position > 0+ and pr.mentioned_entity_count > 0), 0)::double precision as entity_sum,+ count(*) filter (+ where pr.mention_count > 0 and pr.mention_position > 0+ and pr.mentioned_entity_count > 0) as entity_count+ from public.prompt_results pr+ where pr.brand_id = p_brand_id+ and pr.prompt_id is not null+ and pr.platform <> 'chatgpt-shopping'+ and (p_date_from is null or pr.created_at >= p_date_from)+ and (p_date_to is null or pr.created_at <= p_date_to)+ and (p_region is null or pr.region = p_region)+ group by pr.prompt_id, pr.platform+ ) s;++ return v_result;+end;+$$;++revoke execute on function public.prompt_position_summaries_json(+ uuid, timestamptz, timestamptz, text+) from public, anon;+grant execute on function public.prompt_position_summaries_json(+ uuid, timestamptz, timestamptz, text+) to authenticated, service_role;++comment on function public.prompt_position_summaries_json(uuid, timestamptz, timestamptz, text) is+ 'Tracked Prompts Positions column (#1094): prompt_position_summaries (00123) as one jsonb array, so PostgREST''s max_rows does not cut a large brand''s rows; planned with its arguments, security definer. Null when the caller is not a member of the brand''s organization.';diff --git a/supabase/schema.sql b/supabase/schema.sqlindex 66296caad..24bacb0b4 100644--- a/supabase/schema.sql+++ b/supabase/schema.sql@@ -19197,6 +19197,101 @@ grant execute on function public.insights_topic_competitors_daily(comment on function public.insights_topic_competitors_daily(uuid, date, date, text[], text) is'Insights "Topics vs competitors" (VIS-010, 00137; planned with its arguments, security definer, 00152): per active topic, the brand''s Visibility components over the topic''s prompts and each live competitor''s components over the same answers, from the prompt-grain rollups. complete=false until they are backfilled (00130). Null when the caller is not a member of the brand''s organization.';+-- ─────────────────────────────────────────────────────────────────────────+-- migrations/00153_prompt_position_summaries_json.sql+-- ─────────────────────────────────────────────────────────────────────────+-- The Positions column's sums in one value (#1094 review).+--+-- prompt_position_summaries (00123) returns a row per prompt and platform. The+-- largest brand's 30 days come to 4,254 rows, past PostgREST's max_rows of+-- 1000, so the rest were dropped without an error and most of its prompts+-- read "—". Reading it a page at a time re-ran the whole aggregate per page:+-- 3.1 s for 30 days and 4.5 s for 90 days, five times over.+--+-- This returns the same rows as one jsonb array, which max_rows doesn't cut,+-- so the page gets every row in a single call. As 00147, 00148 and 00152 do,+-- it is plpgsql with force_custom_plan, so every call is planned with its real+-- window, and work_mem; and security definer with the organization check made+-- explicit. The rows and their counting rules are 00123's.+--+-- prompt_position_summaries (00123) stays for a web build deployed before this+-- migration, which falls back to it; drop it once no build calls it.++create or replace function public.prompt_position_summaries_json(+ p_brand_id uuid,+ p_date_from timestamptz default null,+ p_date_to timestamptz default null,+ p_region text default null+)+returns jsonb+language plpgsql+stable+security definer+set search_path to 'public'+set work_mem to '64MB'+set plan_cache_mode to 'force_custom_plan'+as $$+declare+ v_result jsonb;+begin+ if auth.role() is distinct from 'service_role' and not exists (+ select 1 from public.brands b+ join public.profiles pf on pf.organization_id = b.organization_id+ where b.id = p_brand_id and pf.id = auth.uid()+ ) then+ return null;+ end if;++ select coalesce(jsonb_agg(jsonb_build_object(+ 'prompt_id', s.prompt_id,+ 'platform', s.platform,+ 'runs', s.runs,+ 'mention_answers', s.mention_answers,+ 'position_sum', s.position_sum,+ 'position_count', s.position_count,+ 'entity_sum', s.entity_sum,+ 'entity_count', s.entity_count)), '[]'::jsonb)+ into v_result+ from (+ select pr.prompt_id,+ pr.platform,+ count(*) as runs,+ count(*) filter (where pr.mention_count > 0) as mention_answers,+ coalesce(sum(pr.mention_position) filter (+ where pr.mention_count > 0 and pr.mention_position > 0), 0)::double precision+ as position_sum,+ count(*) filter (+ where pr.mention_count > 0 and pr.mention_position > 0) as position_count,+ coalesce(sum(pr.mentioned_entity_count) filter (+ where pr.mention_count > 0 and pr.mention_position > 0+ and pr.mentioned_entity_count > 0), 0)::double precision as entity_sum,+ count(*) filter (+ where pr.mention_count > 0 and pr.mention_position > 0+ and pr.mentioned_entity_count > 0) as entity_count+ from public.prompt_results pr+ where pr.brand_id = p_brand_id+ and pr.prompt_id is not null+ and pr.platform <> 'chatgpt-shopping'+ and (p_date_from is null or pr.created_at >= p_date_from)+ and (p_date_to is null or pr.created_at <= p_date_to)+ and (p_region is null or pr.region = p_region)+ group by pr.prompt_id, pr.platform+ ) s;++ return v_result;+end;+$$;++revoke execute on function public.prompt_position_summaries_json(+ uuid, timestamptz, timestamptz, text+) from public, anon;+grant execute on function public.prompt_position_summaries_json(+ uuid, timestamptz, timestamptz, text+) to authenticated, service_role;++comment on function public.prompt_position_summaries_json(uuid, timestamptz, timestamptz, text) is+ 'Tracked Prompts Positions column (#1094): prompt_position_summaries (00123) as one jsonb array, so PostgREST''s max_rows does not cut a large brand''s rows; planned with its arguments, security definer. Null when the caller is not a member of the brand''s organization.';+-- ─────────────────────────────────────────────────────────────────────────-- migrations/00154_prompt_volume_median.sql-- ─────────────────────────────────────────────────────────────────────────diff --git a/web/src/lib/actions/prompt.test.ts b/web/src/lib/actions/prompt.test.tsindex a93551edb..656a01eb2 100644--- a/web/src/lib/actions/prompt.test.ts+++ b/web/src/lib/actions/prompt.test.ts@@ -18,6 +18,14 @@ let writes: unknown[] = [];let promptSetRows: Record<string, unknown>[] = [];let positionData: Record<string, unknown>[] = [];let positionsError: string | null = null;+/** The window before, for the trend; the current window's rows when null. */+let previousPositionData: Record<string, unknown>[] | null = null;+/** Null as the RPC returns it for a caller outside the brand's organization. */+let positionsNull = false;+/** The web deployed before 00153: PostgREST can't find the jsonb RPC. */+let jsonRpcMissing = false;+/** The position RPC calls, and whether any asked for a slice. */+let positionCalls: { fn: string; ranged: boolean }[] = [];vi.mock('next/cache', () => ({ revalidatePath: vi.fn() }));vi.mock('@/lib/actions/volumes', () => ({ getPromptVolumes: vi.fn() }));@@ -29,15 +37,40 @@ vi.mock('@/lib/guards/plan-guard', () => ({vi.mock('@/lib/supabase/server', () => ({createClient: vi.fn(async () => ({- rpc: async (fn: string) => {- if (fn === 'prompt_position_summaries') {- return positionsError- ? { data: null, error: { message: positionsError } }- : { data: positionData, error: null };- }- return usageFails- ? { data: null, error: { message: 'rpc failed' } }- : { data: [{ brand_id: 'b1', locations: usedLocations }], error: null };+ // The position sums come as one jsonb value (00153), which PostgREST's+ // 1000-row cap doesn't cut; the old row-returning RPC would have been cut.+ rpc: (fn: string, args?: { p_date_to?: string }) => {+ const call = { fn, ranged: false };+ if (fn.startsWith('prompt_position_summaries')) positionCalls.push(call);+ const result = () => {+ if (fn === 'prompt_position_summaries_json' && jsonRpcMissing) {+ return {+ data: null,+ error: { code: 'PGRST202', message: 'Could not find the function' },+ };+ }+ if (fn === 'prompt_position_summaries_json') {+ // Only the window before the current one has an end.+ const rows = (args?.p_date_to && previousPositionData) || positionData;+ if (positionsError) return { data: null, error: { message: positionsError } };+ return { data: positionsNull ? null : rows, error: null };+ }+ if (fn === 'prompt_position_summaries') {+ return { data: positionData.slice(0, 1000), error: null };+ }+ return usageFails+ ? { data: null, error: { message: 'rpc failed' } }+ : { data: [{ brand_id: 'b1', locations: usedLocations }], error: null };+ };+ const chain = {+ order: () => chain,+ range: () => {+ call.ranged = true;+ return chain;+ },+ then: (resolve: (value: unknown) => unknown) => resolve(result()),+ };+ return chain;},from: (table: string) => ({select: () => ({@@ -224,6 +257,10 @@ describe('getPromptsPageData', () => {},];positionsError = null;+ previousPositionData = null;+ positionsNull = false;+ jsonRpcMissing = false;+ positionCalls = [];vi.mocked(getPromptVolumes).mockResolvedValue({ volumes: [], quota: {} as VolumeQuota });vi.mocked(getPromptVisibilitySummaries).mockResolvedValue({});// The location quota fails here (no brand row), which the page also absorbs.@@ -241,6 +278,66 @@ describe('getPromptsPageData', () => {expect(page.positions.p1.dots.map((d) => d.id)).toEqual(['chatgpt-web']);});+ /** 1000 rows of other prompts, which come before p1's in the RPC's order. */+ const others = () =>+ Array.from({ length: 1000 }, (_, i) => ({+ prompt_id: `other-${String(i).padStart(4, '0')}`,+ platform: 'chatgpt-web',+ runs: 1,+ mention_answers: 0,+ position_sum: 0,+ position_count: 0,+ entity_sum: 0,+ entity_count: 0,+ }));++ it("reads every prompt's positions in one call, past PostgREST's 1000-row cap", async () => {+ // A busy brand's 30 days: p1's row comes after 1000 others. Read as rows,+ // the cap dropped it and p1 read —.+ positionData = [...others(), positionData[0]];++ const page = await getPromptsPageData('b1');++ expect(page.positions.p1.avg).toBe(2);+ // One call per window, the current one and the one before it for the+ // trend, neither paged: paging re-ran the whole aggregate for every page.+ expect(positionCalls).toEqual([+ { fn: 'prompt_position_summaries_json', ranged: false },+ { fn: 'prompt_position_summaries_json', ranged: false },+ ]);+ });++ it("reads the trend's previous window past the cap too", async () => {+ // Before: #3 on average (12 / 4); now #2, so the cell shows ↑1.+ previousPositionData = [...others(), { ...positionData[0], position_sum: 12 }];++ const page = await getPromptsPageData('b1');++ expect(page.positions.p1).toMatchObject({ avg: 2, delta: 1 });+ });++ it('falls back to the row RPC when the web deploys before 00153', async () => {+ jsonRpcMissing = true;++ const page = await getPromptsPageData('b1');++ expect(page.positions.p1.avg).toBe(2);+ expect(positionCalls.map((c) => c.fn)).toEqual([+ 'prompt_position_summaries_json',+ 'prompt_position_summaries_json',+ 'prompt_position_summaries',+ 'prompt_position_summaries',+ ]);+ });++ it("shows — when the caller is outside the brand's organization (null sums)", async () => {+ positionsNull = true;++ const page = await getPromptsPageData('b1');++ expect(page.positions.p1).toMatchObject({ avg: null, delta: null });+ });+it('shows — in the Positions column when the position query fails, instead of failing the page', async () => {positionsError = 'canceling statement due to statement timeout';diff --git a/web/src/lib/actions/prompt.ts b/web/src/lib/actions/prompt.tsindex 967797269..4233d6004 100644--- a/web/src/lib/actions/prompt.ts+++ b/web/src/lib/actions/prompt.ts@@ -1059,8 +1059,8 @@ export async function getPromptsPageData(region: opts?.region,}),// The Positions column scans every answer in the window and the one- // before it. A timeout, or the web deploying before 00123 is applied,- // shows — in that column instead of failing the table.+ // before it. A timeout shows — in that column instead of failing the+ // table; the web deploying before 00153 falls back to 00123's rows.getPositionRows(brandId, window, opts?.region).catch((err) => {console.error('[prompts] position summaries fetch failed, rendering without them:', err);return noPositionRows();@@ -1141,22 +1141,55 @@ async function getPositionRows(return { included, current, previous };}+/** A prompt and platform's sums, as prompt_position_summaries_json (00153) returns them. */+interface PositionSummaryRow {+ prompt_id: string;+ platform: string;+ runs: number;+ mention_answers: number;+ position_sum: number;+ position_count: number;+ entity_sum: number;+ entity_count: number;+}+async function positionSums(brandId: string,window: { from: string; to?: string },region?: string,): Promise<Map<string, PlatformPositionRow[]>> {const supabase = await createClient();- const { data, error } = await supabase.rpc('prompt_position_summaries', {+ // One jsonb array (00153). A busy brand's 30 or 90 days come to thousands+ // of prompt × platform rows, past PostgREST's 1000-row cap, which dropped+ // the rest and left most prompts reading —; a single value isn't cut, so+ // one call reads them all at the aggregate's own cost.+ const { data, error } = await supabase.rpc('prompt_position_summaries_json', {p_brand_id: brandId,p_date_from: window.from,p_date_to: window.to ?? undefined,p_region: region ?? undefined,});- if (error) throw new Error(error.message);+ let rows: PositionSummaryRow[];+ if (error?.code === 'PGRST202') {+ // The web deployed before 00153 was applied: 00123's rows, as before,+ // which only a brand past 1000 prompt × platform rows loses any of.+ const legacy = await supabase.rpc('prompt_position_summaries', {+ p_brand_id: brandId,+ p_date_from: window.from,+ p_date_to: window.to ?? undefined,+ p_region: region ?? undefined,+ });+ if (legacy.error) throw new Error(legacy.error.message);+ rows = legacy.data ?? [];+ } else if (error) {+ throw new Error(error.message);+ } else {+ // Null when the caller isn't a member of the brand's organization.+ rows = (data ?? []) as unknown as PositionSummaryRow[];+ }const byPrompt = new Map<string, PlatformPositionRow[]>();- for (const row of data ?? []) {+ for (const row of rows) {const list = byPrompt.get(row.prompt_id) ?? [];list.push({platform: row.platform,diff --git a/web/src/lib/actions/tracking.test.ts b/web/src/lib/actions/tracking.test.tsindex a3a70b772..c43152fae 100644--- a/web/src/lib/actions/tracking.test.ts+++ b/web/src/lib/actions/tracking.test.ts@@ -517,3 +517,52 @@ describe('analyzeNewPrompt', () => {expect(fetchMock).not.toHaveBeenCalled();});});++describe('getPromptVisibilitySummaries', () => {+ afterEach(() => {+ rpcMock = vi.fn(async () => ({ data: insightsAggregatesRow, error: null }));+ });++ it('keeps every prompt of a brand past 1000, reading the summaries a page at a time', async () => {+ const rows = Array.from({ length: 1200 }, (_, i) => ({+ prompt_id: `p-${i}`,+ runs: 5,+ visible_runs: 1,+ mention_answers: 1,+ citation_answers: 0,+ total_mentions: 2,+ total_citations: 0,+ avg_visibility: 10,+ avg_visibility_visible: 50,+ position_factor: 0.5,+ last_run_at: '2026-10-16T00:00:00.000Z',+ }));+ const ranges: [number, number][] = [];+ // Served as PostgREST would: a ranged request gets its slice, never more+ // than 1000 rows.+ rpcMock = vi.fn(() => {+ let slice: [number, number] = [0, 999];+ const chain = {+ order: () => chain,+ range: (from: number, to: number) => {+ slice = [from, to];+ ranges.push([from, to]);+ return chain;+ },+ then: (resolve: (value: unknown) => unknown) =>+ resolve({ data: rows.slice(slice[0], slice[1] + 1), error: null }),+ };+ return chain;+ }) as unknown as typeof rpcMock;+ const { getPromptVisibilitySummaries } = await import('./tracking');++ const summaries = await getPromptVisibilitySummaries('brand-id', { days: 90 });++ expect(Object.keys(summaries)).toHaveLength(1200);+ expect(summaries['p-1199'].totalMentions).toBe(2);+ expect(ranges).toEqual([+ [0, 999],+ [1000, 1999],+ ]);+ });+});diff --git a/web/src/lib/actions/tracking.ts b/web/src/lib/actions/tracking.tsindex 82336d11b..fe6d260df 100644--- a/web/src/lib/actions/tracking.ts+++ b/web/src/lib/actions/tracking.ts@@ -2,6 +2,7 @@import { unstable_cache } from 'next/cache';import { dbClient, withDbClient } from '@/lib/supabase/scoped';+import { readAllRpcRows } from '@/lib/supabase/rpc-pages';import { expandDateToEndOfDay } from '@/lib/dates';import { runDayWindow } from '@/lib/citations/rollup-window';import {@@ -2338,18 +2339,23 @@ export async function getPromptVisibilitySummaries(// 1000 rows (wrong health columns on busy brands — the #430/#464 defect// family) and its transfer cost slowed the Prompts page's first load.// The RPC excludes chatgpt-shopping (#155) like every analytical surface.- const { data, error } = await supabase.rpc('prompt_visibility_summaries', {- p_brand_id: brandId,- p_date_from: since,- p_date_to: opts?.to ?? undefined,- // One tracked location, or every location blended when absent (#691).- p_region: opts?.region ?? undefined,- });-- if (error) throw new Error(error.message);+ // One row per prompt, read a page at a time so a brand past 1000 prompts+ // keeps them all.+ const data = await readAllRpcRows((from, to) =>+ supabase+ .rpc('prompt_visibility_summaries', {+ p_brand_id: brandId,+ p_date_from: since,+ p_date_to: opts?.to ?? undefined,+ // One tracked location, or every location blended when absent (#691).+ p_region: opts?.region ?? undefined,+ })+ .order('prompt_id')+ .range(from, to),+ );const result: Record<string, PromptVisibilitySummary> = {};- for (const row of data ?? []) {+ for (const row of data) {const runs = Number(row.runs ?? 0);const visibleRuns = Number(row.visible_runs ?? 0);const mentionAnswers = Number(row.mention_answers ?? 0);diff --git a/web/src/lib/prompt-position-summary.ts b/web/src/lib/prompt-position-summary.tsindex 8d7fc452f..1a0da6ddb 100644--- a/web/src/lib/prompt-position-summary.ts+++ b/web/src/lib/prompt-position-summary.ts@@ -4,7 +4,7 @@* the same length, and one dot per platform.** Pure on purpose: the server action loads per-platform sums- * (prompt_position_summaries, 00123) and this turns them into the cell. The+ * (prompt_position_summaries_json, 00153) and this turns them into the cell. The* average is the Positions panel's: the mean mention position over the* answers that mention the brand, on the plan-included platforms.*/diff --git a/web/src/lib/supabase/rpc-pages.test.ts b/web/src/lib/supabase/rpc-pages.test.tsnew file mode 100644index 000000000..b2cfbd5b5--- /dev/null+++ b/web/src/lib/supabase/rpc-pages.test.ts@@ -0,0 +1,60 @@+import { describe, expect, it, vi } from 'vitest';+import { RPC_PAGE_SIZE, readAllRpcRows } from './rpc-pages';++/** A set-returning RPC whose result has `total` rows, served as PostgREST would. */+function rpcOf(total: number) {+ const rows = Array.from({ length: total }, (_, i) => ({ n: i }));+ return vi.fn(async (from: number, to: number) => ({+ data: rows.slice(from, Math.min(to + 1, from + RPC_PAGE_SIZE)),+ error: null,+ }));+}++describe('readAllRpcRows', () => {+ it('reads every row past the 1000-row cap, a page at a time', async () => {+ const page = rpcOf(2500);++ const rows = await readAllRpcRows(page);++ expect(rows).toHaveLength(2500);+ expect(rows.at(-1)).toEqual({ n: 2499 });+ expect(page.mock.calls).toEqual([+ [0, 999],+ [1000, 1999],+ [2000, 2999],+ ]);+ });++ it('takes a single request when the result is under a page', async () => {+ const page = rpcOf(709);++ expect(await readAllRpcRows(page)).toHaveLength(709);+ expect(page).toHaveBeenCalledTimes(1);+ });++ it('asks once more when the last page is exactly full', async () => {+ const page = rpcOf(1000);++ expect(await readAllRpcRows(page)).toHaveLength(1000);+ expect(page).toHaveBeenCalledTimes(2);+ });++ it('stops at its page limit and says the result may be incomplete', async () => {+ const page = rpcOf(1_000_000);+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});++ expect(await readAllRpcRows(page)).toHaveLength(100_000);+ expect(warn).toHaveBeenCalledWith(expect.stringContaining('may be incomplete'));+ warn.mockRestore();+ });++ it('throws the error of any page', async () => {+ const page = vi.fn(async (from: number) =>+ from === 0+ ? { data: Array.from({ length: RPC_PAGE_SIZE }, () => ({})), error: null }+ : { data: null, error: { message: 'canceling statement due to statement timeout' } },+ );++ await expect(readAllRpcRows(page)).rejects.toThrow('statement timeout');+ });+});diff --git a/web/src/lib/supabase/rpc-pages.ts b/web/src/lib/supabase/rpc-pages.tsnew file mode 100644index 000000000..bcc205140--- /dev/null+++ b/web/src/lib/supabase/rpc-pages.ts@@ -0,0 +1,36 @@+/** PostgREST's max_rows (supabase/config.toml). A page larger than it would read short and end early. */+export const RPC_PAGE_SIZE = 1000;++/** 100,000 rows: far past any brand's window, and a bound on a runaway loop. */+const MAX_PAGES = 100;++/**+ * Every row a set-returning RPC gives back.+ *+ * PostgREST returns at most 1000 rows per request, and an RPC is no+ * exception: the rest are dropped without an error, the #430/#464 defect+ * family. `page` asks for one slice in a fixed order (each caller orders by+ * its RPC's key), and slices are read until one comes back short. A result+ * under 1000 rows still takes a single request. Each slice is its own query,+ * so rows written in between can shift a later slice by a row: a caller+ * should key rows by id rather than sum them as they come. Every page re-runs+ * the RPC, so an expensive aggregate is better returned as one jsonb value+ * (as prompt_position_summaries_json, 00153, does).+ */+export async function readAllRpcRows<T>(+ page: (+ from: number,+ to: number,+ ) => PromiseLike<{ data: T[] | null; error: { message: string } | null }>,+): Promise<T[]> {+ const rows: T[] = [];+ for (let i = 0; i < MAX_PAGES; i++) {+ const from = i * RPC_PAGE_SIZE;+ const { data, error } = await page(from, from + RPC_PAGE_SIZE - 1);+ if (error) throw new Error(error.message);+ rows.push(...(data ?? []));+ if ((data ?? []).length < RPC_PAGE_SIZE) return rows;+ }+ console.warn(`[rpc-pages] stopped after ${MAX_PAGES} pages; the result may be incomplete`);+ return rows;+}diff --git a/web/src/types/supabase.ts b/web/src/types/supabase.tsindex 1daf9ab52..612cf52a6 100644--- a/web/src/types/supabase.ts+++ b/web/src/types/supabase.ts@@ -3698,6 +3698,15 @@ export type Database = {runs: number}[]}+ prompt_position_summaries_json: {+ Args: {+ p_brand_id: string+ p_date_from?: string+ p_date_to?: string+ p_region?: string+ }+ Returns: Json+ }prompt_usage_summary: {Args: { p_brand_id: string }Returns: {