Skip to content

fix(citations): answer "has any citations" without counting every row - #707

Merged
gkhngyk merged 1 commit into
mainfrom
fix/706-citations-presence-check
Aug 11, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
fix/706-citations-presence-check

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

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:

Aggregate  (actual time=1773.205..1773.206 rows=1)
  Buffers: shared hit=95371 read=2063
  ->  Bitmap Heap Scan  (actual time=6.285..1767.799 rows=39385)
Execution Time: 1773.544 ms

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 HEAD returning 500 at 16:13:04 and 206 at 16:13:28. Because the call sits in a Promise.all with 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:

  • The order by is 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 by created_at keeps the scan on the brand's own rows via the existing idx_prompt_results_brand_created.
  • prompt_results.citation_count is not a substitute for the citations <> '[]' predicate, despite looking like one. It counts something narrower: on the brand measured, 39,385 rows have a non-empty citations array while only 1,895 have citation_count > 0.

Related issue

Closes #706

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. Open the Citations page for a brand with tens of thousands of results and refresh repeatedly — no failure toast, and the page settles noticeably faster.
  2. Filter down to a window with no citations: the empty state still says "no citations match these filters" rather than "this brand has no citations yet".
  3. On a brand that genuinely has no citations at all, the empty state still reports that correctly.
  4. The Retry button in the failed state still works.

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

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
@gkhngyk gkhngyk self-assigned this Aug 11, 2026
@gkhngyk
gkhngyk merged commit 1e9ed9b into main Aug 11, 2026
5 checks passed
@gkhngyk
gkhngyk deleted the fix/706-citations-presence-check branch August 11, 2026 22:20
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.

bug: Citations page intermittently fails to load — the has-any-citations check times out

1 participant