Skip to content

fix(mentions): refresh the Mentions rollups one at a time, and rebuild Insights days of a deleted prompt - #1227

Merged
gkhngyk merged 2 commits into
ansvisor:mainfrom
cihan-gyk:fix/rollup-integrity
Oct 20, 2026
Merged

gkhngyk merged 2 commits into
ansvisor:mainfrom
cihan-gyk:fix/rollup-integrity

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

This follows #1226. Once both pages count mentions and net sentiment the same way, two data paths can still make Insights and Mentions disagree for the same days.

1. Overlapping Mentions refreshes

  • refresh_mentions_daily (00157) deletes and re-inserts a day range with no lock.
  • A run's refresh overlapping the nightly sweep (or a backfill) for the same brand could therefore write the shared days twice. This is the gap fix(rollups): serialize concurrent refreshes of a brand's daily rollups #1020 fixed for refresh_insights_daily in 00164.
  • Migration 00192:
    • gives it the same per-brand pg_advisory_xact_lock;
    • drops rows written twice, keeping the copy with the latest answer (the next refresh of those days rewrites them anyway);
    • adds unique keys, nulls not distinct (PG15+), on mentions_prompt_daily (brand_id, day, prompt_id, platform, region) and mentions_competitor_daily (… , competitor_id).
  • On large tables, build those with create unique index concurrently first, as 00158/00164 note.

2. Deleted prompts

  • Deleting a prompt cascades its prompt_results.
  • The Mentions page reads existing prompts only, so it drops them at once. insights_brand_daily keeps them until those days are refreshed, and the sweep covers only the last 3 days.
  • New pieces:
    • Trigger: prompts_queue_insights_rollup_rebuild (BEFORE DELETE, row level) reads the deleted prompt's first and last day in insights_prompt_daily before the delete. It queues the brand with that range in insights_rollup_rebuilds (one row per brand; ranges widen with least/greatest). As with citation_rollup_rebuilds (00118), there is no foreign key and nothing is queued during a brand delete.
    • Server: rebuildQueuedInsightsRollups (lib/insights-rollups.js) refreshes each queued range in 31-day chunks, like the backfill. It clears an entry only if nothing re-queued the brand meanwhile, and never runs twice at once. It runs on REBUILD_QUEUE_SCHEDULE (every 5 minutes) beside the citation rebuilds, and at the end of the nightly sweep.

Deploy:

  • Apply 00192, then deploy the server.
  • Until the server deploys, queued entries simply wait.

Numbering: 00188 is #1210's, 00189 #1223's, 00190 #1225's and 00191 #1226's. schema.sql is built without them; whichever merges later needs build-schema.sh again.

Related issue

Follows #1226 (Insights vs Mentions numbers reported by the product owner).

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

I applied 00192 on the dev DB. On the demo brand, in a transaction that was rolled back:

Step Insights 30d mentions Mentions 30d mentions
Before deleting the prompt with the most mentions 299 299
Prompt deleted; queued 2026-09-16 .. 2026-09-26 299 (stale) 224
After refreshing the queued range 224 224

After the refresh, named answers (74), positive (29) and negative (15) match too. The rollback restored the prompt and left the queue empty.

Concurrency: I ran two refresh_mentions_daily calls for the same brand and range at once. Both succeeded, with no duplicate keys, and the row count was unchanged.

Unit tests (insights-rollups.test.js):

  • chunks of a queued range;
  • the entry cleared only with requested_at <= the one read;
  • a failed refresh keeping its entry;
  • no overlapping drains;
  • the drain at the end of the sweep.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/): web not touched
  • yarn typecheck passes (run from web/): web not touched
  • yarn format:check passes (run from web/): web not touched
  • Changes are focused — one concern per PR

Also run:

  • server: npm run format, npm run lint, npm test (1822 passed);
  • bash supabase/build-schema.sh.

🤖 Generated with Claude Code

…d Insights days of a deleted prompt

Two ways Insights and Mentions could still disagree for the same days.

refresh_mentions_daily deleted and re-inserted a day range with no lock,
so a run's refresh overlapping the nightly sweep (or a backfill) could
write shared days twice; refresh_insights_daily had the same gap until
00164 (ansvisor#1020). 00192 gives it the same per-brand advisory lock, drops rows
written twice, and adds unique keys (nulls not distinct) to both Mentions
rollups.

Deleting a prompt cascades its answers. The Mentions page reads existing
prompts only, so it dropped them at once, but insights_brand_daily kept
them until those days were refreshed, and the sweep covers only the last 3.
A BEFORE DELETE trigger on prompts now queues the brand with the prompt's
day range (from insights_prompt_daily) in insights_rollup_rebuilds, and the
server refreshes that range in 31-day chunks on the citation rebuild
schedule and at the end of the nightly sweep, clearing an entry only if
nothing re-queued it meanwhile.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk This is the follow-up promised on #1226. It covers two things:

  • The lock: refresh_mentions_daily now takes the same per-brand advisory lock as 00164, and the Mentions rollups get unique keys.
  • Deleted prompts: a BEFORE DELETE trigger queues the prompt's days, and the server rebuilds those Insights days (the 00118 pattern).

On the dev DB (rolled back), deleting the demo's top prompt left Insights at 299 against Mentions' 224 until the rebuild, then both read 224. I think that is the likeliest cause of the gap on prod.

Migration 00192 is applied on the dev DB. On prod the unique indexes may want a concurrently build first.

gkhngyk
gkhngyk previously approved these changes Oct 20, 2026

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lock matches 00164's pattern, and only override_mention_sentiment takes two locks, always in the same order, so there is no deadlock. The queue table follows citation_rollup_rebuilds. On prod, mentions_prompt_daily has 573k rows and mentions_competitor_daily 551k, with no duplicate keys, so the dedup deletes nothing and the unique indexes build quickly inside the migration.

For a follow-up:

  • Drain chunks. The drain refreshes 31-day chunks back to back, holding the brand's insights lock, over a deleted prompt's whole history. override_mention_sentiment (8 s) can time out behind it. 7-day chunks would keep each hold short.
  • The trigger's timestamp. It sets requested_at = now(), the transaction's start time. A long delete can write an older time than the drain read. clock_timestamp() avoids that.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-approving after the merge commit with main (schema.sql rebuilt). 00192 is applied on prod, and both unique indexes are valid.

@gkhngyk
gkhngyk merged commit 1306b45 into ansvisor:main Oct 20, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-migration Adds or edits a migration — apply it to the hosted database

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants