Skip to content

fix(prompts): read the Prompts table's visibility and positions from an index - #1326

Merged
gkhngyk merged 2 commits into
ansvisor:mainfrom
cihan-gyk:fix/prompts-visibility-index-read
Oct 22, 2026
Merged

gkhngyk merged 2 commits into
ansvisor:mainfrom
cihan-gyk:fix/prompts-visibility-index-read

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Measured on prod on 2026-10-21 (read-only, browser, Ansvisor brand): switching Topics → Prompts made getPromptsPageData throw (React #441), with a request at 8.6 s, the authenticated statement timeout. The table then showed "No prompts yet". #1324 makes that state say the read failed; this PR makes the read itself cheap.

The Prompts table runs two window reads over the brand's answers, and both go to the heap for every answer:

  • prompt_visibility_summaries (00072) sums visibility_score, mention_count, citation_count and mention_position;
  • prompt_position_summaries_json (00153) sums mention_count, mention_position and mentioned_entity_count, for the window and the one before it.

idx_prompt_results_brand_created_cover carries the columns these reads filter on but not the ones they add up. A prompt_results row holds the whole answer text, so each answer is its own page read, which the cover index's own migration measured at 8–25 s cold for a 30-day window.

Migration 00232_prompt_results_metrics_cover.sql

  1. idx_prompt_results_brand_created_metrics. It uses the cover index's key (brand_id, created_at desc) and carries its included columns (id, platform, model_used, region, prompt_id) plus visibility_score, mention_count, citation_count, mention_position, mentioned_entity_count.
    • Both reads, and everything the cover index serves, can then be index-only scans.
    • Please build it on prod with CREATE INDEX CONCURRENTLY first. if not exists makes the migration's statement a no-op there.
    • The cover index becomes redundant. Drop it concurrently once the plans show the new one in use; I left that to you.
  2. prompt_visibility_summaries keeps its signature and rows. It becomes plpgsql with force_custom_plan, security definer with the organization check made explicit, as 00153 and 00226 are, so it no longer runs prompt_results' row policy per answer. Outsiders get no rows, as before.
    • It is dropped and recreated rather than replaced, because the dev DB had another branch's body with an extra column (the unopened chore/fold-position-sums, 00146).
    • Grants are now authenticated, service_role, revoked from public and anon.

Checked on the dev DB:

  • Same rows. The function's rows are identical before and after for 30 days, 7 days, all time and a region (DE). An outsider still gets 0 rows.
  • Index-only plans. EXPLAIN shows Index Only Scan using idx_prompt_results_brand_created_metrics for both the visibility and the positions aggregates.
  • Page works. The Prompts page reads the visibility columns as before (90d).

Please time it on prod:

  • getPromptsPageData at 30d/90d on the largest brand;
  • EXPLAIN (ANALYZE, BUFFERS) of both functions;
  • the index's build time and size.

No web or server code changes.

Related issue

None. Reported by the user.

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. On prod: create index concurrently if not exists idx_prompt_results_brand_created_metrics … (as in the migration), then apply 00232.
  2. Open Prompts → Tracked Prompts at 30d and 90d on a large brand. The visibility and positions columns should fill as before, and well under 8 s.
  3. Run EXPLAIN (ANALYZE, BUFFERS) select * from prompt_visibility_summaries('<brand>', now() - interval '90 days') as a member. It should show an index-only scan on the new index, with few heap fetches once the visibility map is current.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/) — web not touched
  • yarn typecheck passes (run from web/) — web not touched
  • yarn format:check passes (run from web/) — web not touched
  • Changes are focused — one concern per PR

bash supabase/build-schema.sh was run. SQL only.

🤖 Generated with Claude Code

…an index

Both window reads behind the Prompts table summed answer figures that
idx_prompt_results_brand_created_cover doesn't carry, so every answer
was a heap read (each row holds the answer text); on prod the page's read
hit the 8 s timeout. Migration 00232 adds
idx_prompt_results_brand_created_metrics (the cover index's key and
columns plus visibility_score, mention_count, citation_count,
mention_position and mentioned_entity_count; build it concurrently on
prod first) and makes prompt_visibility_summaries plpgsql planned per
call, security definer with the organization check, same rows.

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 21, 2026
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk This is the read behind #1324's Prompts timeout. Both Prompts-table window reads sum answer figures that the cover index doesn't carry, so every answer is a heap page.

  • Migration 00232 (after perf(mentions): read the Perception tab's 30 and 90 days from daily rollups #1325's 00231) adds idx_prompt_results_brand_created_metrics: the cover index's key and columns plus the five figures. Please build it with CREATE INDEX CONCURRENTLY before applying. Once you see it in the plans, the old cover index can be dropped concurrently.
  • prompt_visibility_summaries returns the same rows as before. It is now plpgsql planned per call, security definer with the org check, and no longer evaluates the row policy per answer. It is dropped and recreated because the dev DB carried the unopened fold-sums body (an extra column). On prod it's main's 00072.
  • Checked on dev:
    • rows are identical across four windows;
    • an outsider gets 0 rows;
    • EXPLAIN shows Index Only Scan on the new index for both aggregates.
  • Please time on prod: EXPLAIN (ANALYZE, BUFFERS) at 30 and 90 days on the largest brand, plus the index's build time and size.

@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. On production, for the largest brand, timed as a member:

Read Before After
Visibility, 30 days 15.5 s cold and warm 0.04 s
Visibility, 90 days — 0.10 s
Positions, 30 days — 0.12 s

The index alone brought the old function to 0.18 s.

  • Same rows. The function's rows are identical before and after for four brand and window cases.
  • Outsiders. A member of another organization gets 0 rows.

On production: I built the index concurrently (78 MB) and applied 00232. I merged main in, rebuilding schema.sql after 00235. Tested on top of main with #1324: web and server tests, tsc, lint and Prettier pass.

idx_prompt_results_brand_created_cover is left in place for now. It can be dropped concurrently later.

@gkhngyk
gkhngyk merged commit 0ef395b into ansvisor:main Oct 22, 2026
4 of 5 checks passed
gkhngyk added a commit that referenced this pull request Oct 22, 2026
…#1338)

* fix(prompts): read the Prompts table's visibility and positions from an index

Both window reads behind the Prompts table summed answer figures that
idx_prompt_results_brand_created_cover doesn't carry, so every answer
was a heap read (each row holds the answer text); on prod the page's read
hit the 8 s timeout. Migration 00232 adds
idx_prompt_results_brand_created_metrics (the cover index's key and
columns plus visibility_score, mention_count, citation_count,
mention_position and mentioned_entity_count; build it concurrently on
prod first) and makes prompt_visibility_summaries plpgsql planned per
call, security definer with the organization check, same rows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(prompts): open the Topics tab's 24 hours and long windows in time

Measured on production on 2026-10-22, the Topics tab's default 24 hours
failed every time at the 8 s statement timeout, and 90 days took 5.9 s.

Migration 00236:
- topics_overview_window's 24-hour branch reads both windows' counts
  from idx_prompt_results_brand_created_metrics (#1326) alone, and
  reads competitor_mentions for the window's answers only. On the dev
  DB it returns the same rows as 00170 over 7 windows.
- Covering indexes on insights_prompt_daily and
  insights_competitor_prompt_daily (brand_id, day) make the long
  windows' rollup reads index-only. They replace the plain (brand_id,
  day) indexes, and both tables are autovacuumed about daily, as 00224
  does for the answers.
- health_probe gains 'topics_overview_window'.

The morning health report now times the tab's own reads (24 hours and
90 days) instead of topics_overview_aggregates, which the page stopped
reading at 00156; that fixed function's 20 s+ was the report's, not a
user's.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Cihan <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Gökhan Geyik <[email protected]>
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