Skip to content

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

Closed

Description

@gkhngyk

Description

The Citations page intermittently fails with a "Failed to load citation data" toast, most often right after a page refresh. It is not deterministic — the same page loads fine seconds later.

The failing request is the "does this brand have any citations at all" lookup, visible in the API log as an exact-count HEAD that returns 500 and then succeeds on a retry moments later:

16:13:04  HEAD | 500 | /rest/v1/prompt_results?select=id&brand_id=eq.<id>&platform=neq.chatgpt-shopping&citations=neq.[]
16:13:28  HEAD | 206 | (identical request)

That is getCitationsTotal in web/src/lib/actions/citations.ts:

.select('id', { count: 'exact', head: true })
.eq('brand_id', brandId)
.neq('platform', 'chatgpt-shopping')
.neq('citations', '[]')

citations is a jsonb column and <> '[]' cannot use an index, so an exact count forces Postgres to read every row for the brand and detoast every citation payload. On a brand with ~43k results:

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)
        Filter: ((platform <> 'chatgpt-shopping') AND (citations <> '[]'::jsonb))
Execution Time: 1773.544 ms

1.8s with a warm cache, roughly 780 MB of buffers touched. After the nightly tracking run rewrites the table — or under any concurrent load — the same query crosses PostgREST's 8s statement_timeout and returns 500. The page catches it and toasts.

The query also ignores the date filter entirely, so it scans all history even when the user is looking at the last 24 hours. It runs in Promise.all alongside the actual data load, so it fails the whole load even though the real query succeeded.

The count is never displayed

Its only consumer is a boolean:

setHasAnyCitations(total > 0);

hasAnyCitations decides which empty state to show — "this brand has no citations yet" versus "no citations match these filters". Nothing renders the number. The page pays a full scan and detoast of every citation payload to answer a yes/no question.

Steps to reproduce

  1. Open the Citations page for a brand with tens of thousands of results.
  2. Refresh repeatedly, ideally shortly after a daily tracking run has written new rows.
  3. The toast appears on some loads and not others.

Expected behavior

The presence check answers in milliseconds and never fails a page load.

Actual behavior

An exact count over the whole table times out intermittently and takes the rest of the page down with it.

Deployment mode

Cloud (ansvisor.com)

Environment

Server action on the Citations page; hosted Postgres with an 8s statement timeout.

Proposed fix

Replace the exact count with an existence check that stops at the first match, and have the action return the boolean the caller actually wants:

.select('id')
.eq('brand_id', brandId)
.neq('platform', 'chatgpt-shopping')
.neq('citations', '[]')
.order('created_at', { ascending: false })
.limit(1)

Measured on the same brand: 0.102 ms, 4 buffers, using the existing idx_prompt_results_brand_created index.

The order by matters. 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 inside the brand's own rows via the existing index.

Note that prompt_results.citation_count is not a substitute for the citations <> '[]' predicate — it counts something narrower. On the brand above, 39,385 rows have a non-empty citations array while only 1,895 have citation_count > 0.

A partial index (ON prompt_results (brand_id) WHERE citations <> '[]'::jsonb) would make even the zero-citation case instant, but it is not needed for this fix and can be a follow-up if the pattern shows up elsewhere.

Activity

  1. added
    bug
    on Aug 11, 2026
  2. self-assigned this
    on Aug 11, 2026
  3. added a commit that references this issue on Aug 12, 2026
    1e9ed9b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions