Repository navigation
fix(prompts): read the Prompts table's visibility and positions from an index - #1326
Merged
gkhngyk merged 2 commits intoOct 22, 2026
Merged
Conversation
…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]>
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.
|
This was referenced Oct 21, 2026
gkhngyk
approved these changes
Oct 22, 2026
gkhngyk
left a comment
Contributor
There was a problem hiding this comment.
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.
8 of 12 tasks
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]>
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
Measured on prod on 2026-10-21 (read-only, browser, Ansvisor brand): switching Topics → Prompts made
getPromptsPageDatathrow (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) sumsvisibility_score,mention_count,citation_countandmention_position;prompt_position_summaries_json(00153) sumsmention_count,mention_positionandmentioned_entity_count, for the window and the one before it.idx_prompt_results_brand_created_covercarries the columns these reads filter on but not the ones they add up. Aprompt_resultsrow 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.sqlidx_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)plusvisibility_score, mention_count, citation_count, mention_position, mentioned_entity_count.CREATE INDEX CONCURRENTLYfirst.if not existsmakes the migration's statement a no-op there.prompt_visibility_summarieskeeps its signature and rows. It becomes plpgsql withforce_custom_plan, security definer with the organization check made explicit, as 00153 and 00226 are, so it no longer runsprompt_results' row policy per answer. Outsiders get no rows, as before.chore/fold-position-sums, 00146).authenticated, service_role, revoked frompublicandanon.Checked on the dev DB:
EXPLAINshows Index Only Scan usingidx_prompt_results_brand_created_metricsfor both the visibility and the positions aggregates.Please time it on prod:
getPromptsPageDataat 30d/90d on the largest brand;EXPLAIN (ANALYZE, BUFFERS)of both functions;No web or server code changes.
Related issue
None. Reported by the user.
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
create index concurrently if not exists idx_prompt_results_brand_created_metrics …(as in the migration), then apply00232.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
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromweb/) — web not touchedyarn typecheckpasses (run fromweb/) — web not touchedyarn format:checkpasses (run fromweb/) — web not touchedbash supabase/build-schema.shwas run. SQL only.🤖 Generated with Claude Code