Repository navigation
perf(citations): skip answers the backfill has already stored - #739
Merged
Merged
Conversation
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
6 of 11 tasks
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
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
The upsert on
(prompt_result_id, position)makes re-running the backfill safe, but not cheap. Every answer was still read with itscitationsjsonb 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.
--forceturns it off.That is not hypothetical for this table:
extractDomainalready 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:
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 featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsHow to test
docker exec ansvisor-server-1 pkill -f backfill-citationsalready storedclimbing andfailedat 0.--forceon a narrow window (CITATIONS_BACKFILL_DAYS=1) and confirm it reprocesses rather than skips.Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromserver/)yarn format:checkpasses (run fromserver/)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.