Repository navigation
fix(citations): answer "has any citations" without counting every row - #707
Merged
Merged
Conversation
The Citations page failed intermittently with "Failed to load citation data", most visibly right after a refresh. The failing request was the unfiltered has-any-citations lookup, which ran an exact count with a `citations <> '[]'` predicate. That predicate can't use an index, so the count made Postgres read every result the brand had ever produced and detoast each citation payload — 1.8s and ~780 MB of buffers on a large brand with a warm cache, and past the 8s statement timeout once the nightly tracking run had churned it. The call sits in a Promise.all with the real data load, so its 500 took down a page whose actual query had succeeded. The count was never displayed; its only use was `total > 0`. Return the boolean instead and stop at the first match: ~0.1ms on the same brand. The ordering is load-bearing — without it the planner picks a sequential scan over the whole table, unbounded for a brand whose answers never cite anything. Ordering by created_at keeps the scan on the brand's own rows via the existing index. Closes #706
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 Citations page failed intermittently with a "Failed to load citation data" toast, most visibly right after a refresh — the same page then loaded fine seconds later.
The culprit was the unfiltered has-any-citations lookup, which ran an exact count with a
citations <> '[]'predicate. That predicate can't use an index, so the count made Postgres read every result the brand had ever produced and detoast each citation payload:1.8s and ~780 MB of buffers with a warm cache — and past PostgREST's 8s statement timeout once the nightly tracking run had churned that cache, which is why it looked random. The API log caught it plainly: the same
HEADreturning 500 at 16:13:04 and 206 at 16:13:28. Because the call sits in aPromise.allwith the real data load, its failure took down a page whose actual query had already succeeded.The count was never rendered anywhere — its only consumer was
total > 0, feeding the empty-state copy. So the action now returns that boolean and stops at the first match: 0.102 ms, 4 buffers, on the same brand.Two things worth knowing for review:
order byis load-bearing. Without it the planner picks a sequential scan over the whole table — 22 ms when a match turns up early, but unbounded for a brand whose answers never cite anything. Ordering bycreated_atkeeps the scan on the brand's own rows via the existingidx_prompt_results_brand_created.prompt_results.citation_countis not a substitute for thecitations <> '[]'predicate, despite looking like one. It counts something narrower: on the brand measured, 39,385 rows have a non-emptycitationsarray while only 1,895 havecitation_count > 0.Related issue
Closes #706
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
Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromweb/)yarn typecheckpasses (run fromweb/)yarn format:checkpasses (run fromweb/)