Skip to content

fix(rollups): serialize concurrent refreshes of a brand's daily rollups #1020

Closed

Description

@gkhngyk

Problem

The daily rollup tables behind Insights and Citations are written by two SQL functions:

  • refresh_insights_daily writes the insights_*_daily tables (00098).
  • refresh_citations_daily writes the citation_*_daily tables (00118).

Each one deletes a brand's rows for a day range, then recomputes and inserts them. The tables have no unique key, and nothing stops two refreshes of the same brand and days from running at once. Several callers can overlap:

  • a completed run refreshes the run's days (refreshForCompletedRun);
  • the nightly sweep refreshes the trailing days of every brand (sweepInsightsRollups), while that night's runs are completing;
  • a topic move rebuilds the brand's whole citation history (rebuildQueuedCitationRollups), which perf(citations): rebuild citation rollups shortly after a topic move #1006 schedules every 5 minutes.

When two refreshes of the same brand overlap, the second one's DELETE waits for the first to commit. Its snapshot was taken before the first one's INSERT, though, so it skips the rows the first one just wrote. It then inserts its own rows. The overlapping days end up with two copies of every row, and the pages show doubled counts for those days until the next refresh of that range.

No duplicates exist in production today, and the next refresh repairs them, so this is a narrow race rather than an observed failure.

Proposal

Take a transaction-scoped advisory lock per brand at the start of both functions, before the deletes. For example:

perform pg_advisory_xact_lock(hashtextextended('refresh_citations_daily:' || p_brand_id::text, 0));

A second refresh of the same brand then waits until the first one commits. Its DELETE sees the first one's rows and removes them. Refreshes of different brands still run in parallel. Each function gets its own key, since they write different tables.

Acceptance criteria

  • Both refresh functions take a per-brand lock before deleting, in a new migration, with supabase/schema.sql regenerated.
  • Two concurrent refreshes of the same brand and days leave one copy of each row. For example, run the function from two sessions at once, then check that this returns nothing:
    select brand_id, day, count(*)
    from citation_result_daily
    group by brand_id, day, model_used, platform, region, topic_id
    having count(*) > 1;
  • A refresh that waits behind a full-history chunk stays within the service role's statement timeout.

Activity

  1. added
    bug
    help wanted
    citations
    needs-migration
    on Oct 13, 2026
  2. added a commit that references this issue on Oct 20, 2026
    c5a5a32
  3. added a commit that references this issue on Oct 20, 2026
    8ee1044
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcitationshelp wantedneeds-migration

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions