Skip to content

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

Merged
gkhngyk merged 2 commits into
mainfrom
fix/results-autovacuum
Oct 21, 2026
Merged

gkhngyk merged 2 commits into
mainfrom
fix/results-autovacuum

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Summary

prompt_results (578k rows, 3.4 GB) and prompt_result_citations (4.8M rows) only grow. At the default scale factors, autovacuum's insert pass reached them about every three weeks, and analyze ran on the citations about every ten days. As of today they had 78k answers and 784k citations inserted since their last vacuum.

The reads by brand and window use idx_prompt_results_brand_created_cover and prompt_result_citations_brand_created_cover_idx, and they're about the latest days: exactly the pages the visibility map didn't cover yet. Measured during the Content stack reviews:

  • Heap fetches: the index-only scans still went to the heap, about 20k fetches for a 30-day window of the biggest brand, so a cold cache took seconds.
  • Estimates: the planner's statistics stopped before those days. It estimated a window of 11k–70k citations as a single row and picked nested loops.

Migration 00224 sets, on both tables:

  • autovacuum_vacuum_insert_scale_factor = 0.01: the insert pass runs about once a day. It only reads pages that aren't all-visible yet, so each run stays small.
  • autovacuum_analyze_scale_factor = 0.02: about every other day.

It's applied on the hosted DB already. The first runs start on their own, since both tables are past the new thresholds.

Related issue

None. Found in the reviews of #1287, #1290, #1292 and #1308.

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. select relname, reloptions from pg_class where relname in ('prompt_results', 'prompt_result_citations'); shows both settings.
  2. After the next autovacuum, pg_stat_user_tables.last_autovacuum for both tables is today's. An explain (analyze, buffers) of a 30-day read on the cover index then shows few heap fetches.

Checklist

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

No web or server code changes. schema.sql is rebuilt with build-schema.sh.

🤖 Generated with Claude Code

https://claude.ai/code/session_0121amRjX7UFrgmGELAtkc7A

prompt_results and prompt_result_citations only grow, and at the default
scale factors autovacuum's insert pass reached them about every three
weeks. The reads by brand and window are about the latest days, the pages
the visibility map didn't cover yet, so their index-only scans went to the
heap, and the planner's statistics stopped before those days.

The insert pass now runs at 1% of the rows inserted (about daily on each
table) and analyze at 2% of the rows changed. Applied on the hosted DB.

Claude-Session: https://claude.ai/code/session_0121amRjX7UFrgmGELAtkc7A
@gkhngyk
gkhngyk merged commit ac5a9d1 into main Oct 21, 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.

1 participant