Repository navigation
fix(mentions): refresh the Mentions rollups one at a time, and rebuild Insights days of a deleted prompt - #1227
Conversation
…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]>
|
@gkhngyk This is the follow-up promised on #1226. It covers two things:
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 |
gkhngyk
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
Re-approving after the merge commit with main (schema.sql rebuilt). 00192 is applied on prod, and both unique indexes are valid.
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.refresh_insights_dailyin 00164.pg_advisory_xact_lock;nulls not distinct(PG15+), onmentions_prompt_daily (brand_id, day, prompt_id, platform, region)andmentions_competitor_daily (… , competitor_id).create unique index concurrentlyfirst, as 00158/00164 note.2. Deleted prompts
prompt_results.insights_brand_dailykeeps them until those days are refreshed, and the sweep covers only the last 3 days.prompts_queue_insights_rollup_rebuild(BEFORE DELETE, row level) reads the deleted prompt's first and last day ininsights_prompt_dailybefore the delete. It queues the brand with that range ininsights_rollup_rebuilds(one row per brand; ranges widen withleast/greatest). As withcitation_rollup_rebuilds(00118), there is no foreign key and nothing is queued during a brand delete.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 onREBUILD_QUEUE_SCHEDULE(every 5 minutes) beside the citation rebuilds, and at the end of the nightly sweep.Deploy:
Numbering: 00188 is #1210's, 00189 #1223's, 00190 #1225's and 00191 #1226's.
schema.sqlis built without them; whichever merges later needsbuild-schema.shagain.Related issue
Follows #1226 (Insights vs Mentions numbers reported by the product owner).
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
I applied 00192 on the dev DB. On the demo brand, in a transaction that was rolled back:
2026-09-16 .. 2026-09-26After 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_dailycalls 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):requested_at <=the one read;Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromweb/): web not touchedyarn typecheckpasses (run fromweb/): web not touchedyarn format:checkpasses (run fromweb/): web not touchedAlso run:
npm run format,npm run lint,npm test(1822 passed);bash supabase/build-schema.sh.🤖 Generated with Claude Code