Skip to content

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.

perf(db): vacuum and analyze the answers and their citations daily (#1315)

Gökhan Geyikac5a9d1b1150
diff --git a/supabase/migrations/00224_results_autovacuum.sql b/supabase/migrations/00224_results_autovacuum.sql
new file mode 100644
index 000000000..93073b023
--- /dev/null
+++ b/supabase/migrations/00224_results_autovacuum.sql
@@ -0,0 +1,30 @@
+-- ============================================================================
+-- 00224: vacuum and analyze the answers and their citations daily
+-- ============================================================================
+-- prompt_results and prompt_result_citations only grow. At the default
+-- scale factors, autovacuum's insert pass ran on them about every three
+-- weeks, and analyze about every ten days on the citations (on 2026-10-21:
+-- 78k answers and 784k citations inserted since their last vacuum).
+--
+-- The reads by brand and window (`idx_prompt_results_brand_created_cover`,
+-- `prompt_result_citations_brand_created_cover_idx`) are about the latest
+-- days, which were the very pages the visibility map didn't cover yet:
+-- - their index-only scans still went to the heap, about 20k fetches for a
+-- 30-day window of the biggest brand, and a cold cache took seconds;
+-- - the planner's statistics stopped before those days, so it estimated a
+-- window of tens of thousands of citations as a single row.
+--
+-- At 1% of the rows inserted, the insert pass runs about once a day on each
+-- table (about 6k answers and 50k citations a day now). It only reads the
+-- pages that aren't all-visible yet, so a run stays small. Analyze runs at
+-- 2% of the rows changed, about every other day.
+
+alter table public.prompt_results set (
+ autovacuum_vacuum_insert_scale_factor = 0.01,
+ autovacuum_analyze_scale_factor = 0.02
+);
+
+alter table public.prompt_result_citations set (
+ autovacuum_vacuum_insert_scale_factor = 0.01,
+ autovacuum_analyze_scale_factor = 0.02
+);
diff --git a/supabase/schema.sql b/supabase/schema.sql
index 44c8ac4d9..ec6773dbd 100644
--- a/supabase/schema.sql
+++ b/supabase/schema.sql
@@ -31176,3 +31176,37 @@ grant execute on function public.content_plan_pages(uuid, integer) to authentica
comment on function public.content_plan_pages(uuid, integer) is
'Content Editor (CE-014): the brand''s 100 most cited pages over the last N (7, 30 or 90) whole UTC days to yesterday and the N before, from the daily rollups: citations, the brand''s answers in each window, platforms citing at a lower rate, AI visits and how many of its other pages compete for the same prompts; with the earlier window''s first day and the brand''s first tracked day. Security definer with an organization check.';
+-- ─────────────────────────────────────────────────────────────────────────
+-- migrations/00224_results_autovacuum.sql
+-- ─────────────────────────────────────────────────────────────────────────
+-- ============================================================================
+-- 00224: vacuum and analyze the answers and their citations daily
+-- ============================================================================
+-- prompt_results and prompt_result_citations only grow. At the default
+-- scale factors, autovacuum's insert pass ran on them about every three
+-- weeks, and analyze about every ten days on the citations (on 2026-10-21:
+-- 78k answers and 784k citations inserted since their last vacuum).
+--
+-- The reads by brand and window (`idx_prompt_results_brand_created_cover`,
+-- `prompt_result_citations_brand_created_cover_idx`) are about the latest
+-- days, which were the very pages the visibility map didn't cover yet:
+-- - their index-only scans still went to the heap, about 20k fetches for a
+-- 30-day window of the biggest brand, and a cold cache took seconds;
+-- - the planner's statistics stopped before those days, so it estimated a
+-- window of tens of thousands of citations as a single row.
+--
+-- At 1% of the rows inserted, the insert pass runs about once a day on each
+-- table (about 6k answers and 50k citations a day now). It only reads the
+-- pages that aren't all-visible yet, so a run stays small. Analyze runs at
+-- 2% of the rows changed, about every other day.
+
+alter table public.prompt_results set (
+ autovacuum_vacuum_insert_scale_factor = 0.01,
+ autovacuum_analyze_scale_factor = 0.02
+);
+
+alter table public.prompt_result_citations set (
+ autovacuum_vacuum_insert_scale_factor = 0.01,
+ autovacuum_analyze_scale_factor = 0.02
+);
+