Skip to content

perf(citations): skip answers the backfill has already stored - #739

Merged
gkhngyk merged 1 commit into
mainfrom
fix/backfill-skip-done
Aug 19, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
fix/backfill-skip-done

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Summary

The upsert on (prompt_result_id, position) makes re-running the backfill safe, but not cheap. Every answer was still read with its citations jsonb and had every URL resolved against the dictionary before the write was skipped as a duplicate.

Measured on the run currently repairing the 228 answers lost to #738: ~18 answers a second, which is 2 hours 40 minutes to walk 174,466 answers and write 228 of them.

What changed

Two things, both about not paying for work already done.

The page query no longer selects citations. The jsonb is the expensive half of the row — around 12 KB an answer, 2.4 MB a page — and on a repair run nearly every answer in the page is discarded unread.

One id-only lookup per page says which answers already have rows. The jsonb is then fetched only for the answers that still need writing, which on a repair run is close to none.

So a repair run now does three cheap queries per page and almost no work, instead of one heavy query plus a full URL resolution per answer.

--force

Skipping is wrong when the stored rows are present but wrong rather than missing — a changed normalization rule would need every answer rewritten, and the skip would make that impossible. --force turns it off.

That is not hypothetical for this table: extractDomain already changed once during phase 1 review, and if it changes again the rows written under the old rule need replacing.

Reporting

Progress lines carry the skipped count:

  4000 answers · 3998 already stored · 91 citations · 0 with none · 0 failed · 6s · at 2026-05-13T...

Without it, a run racing through covered history reads identically to one that found nothing to do.

Related issue

Refs #732 — phase 1 tooling; the page still reads jsonb until phase 2.

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. Stop the running backfill — it is idempotent, so interrupting costs nothing:
    docker exec ansvisor-server-1 pkill -f backfill-citations
  2. Deploy, then run it again and confirm it finishes in minutes rather than hours, with already stored climbing and failed at 0.
  3. Confirm this returns zero afterwards:
    select count(*) from prompt_results pr
    where jsonb_array_length(coalesce(pr.citations,'[]'::jsonb)) > 0
      and not exists (select 1 from prompt_result_citations c where c.prompt_result_id = pr.id);
  4. Run with --force on a narrow window (CITATIONS_BACKFILL_DAYS=1) and confirm it reprocesses rather than skips.

Checklist

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

Server suite passes: 302 tests. Only the backfill script changes — no migration, no web/ changes, and the shared write path used by the live tracking runs is untouched.

The upsert makes re-running the backfill safe but not cheap. Every answer was
still read with its jsonb and had every URL resolved before the write was
skipped, so a repair run cost as much as the first one: measured at ~18 answers
a second against the full history, roughly two hours and forty minutes to fix
228 answers.

Two changes, both about not paying for work already done. The page query no
longer selects `citations` — the jsonb is the expensive half of the row and on
a repair run nearly every answer in the page is discarded — and one id-only
lookup per page says which answers already have rows. The jsonb is then fetched
only for the answers that still need writing, which on a repair run is close to
none.

`--force` reprocesses everything, for the case where the stored rows are wrong
rather than missing: a changed normalization rule would need that, and the
skip would otherwise make it impossible.

Progress lines now carry the skipped count, so a run that is racing through
already-covered history reads as such rather than as one that found nothing.

Refs #732
@gkhngyk
gkhngyk merged commit 63f8b28 into main Aug 19, 2026
6 checks passed
@gkhngyk
gkhngyk deleted the fix/backfill-skip-done branch August 19, 2026 11:07
gkhngyk added a commit that referenced this pull request Aug 19, 2026
…row cap (#740)

The skip added in #739 read one row per *citation*, not per answer: 200
answers matched 1,591 rows in production, and PostgREST silently caps an
un-paginated select at 1,000. Everything past that first page looked unstored,
was reprocessed, and had its writes discarded as duplicates — so a repair run
reported thousands of citations written while the table did not move at all.
The same trap as #714 and #716, and this file now says so.

The lookup is paginated. A short page is the end-of-results signal, which is
why it reads in exactly PostgREST's own page size.

The reporting that hid it is fixed too. persistCitationRows counted the rows it
attempted rather than the rows that landed, so duplicates counted as writes.
The upsert now returns what it inserted, and the function separates three
outcomes that were previously one number:

  n     rows inserted
  0     nothing new — every row was already there, which is what --force
        produces on every answer it revisits
  null  nothing stored, retry

Conflating 0 with failure would have made --force report every answer as
broken; conflating failure with 0 is what let the original 228 lost answers
look like a healthy run.

Refs #732
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant