Repository navigation
Conversation
gkhngyk
left a comment
There was a problem hiding this comment.
Thanks, the lock and the two-session check are what the issue asked for. Main has moved since this branch, so three changes before it can land:
- Migration number. 00156–00163 are taken on main now. Please rename the file to
00164_serialize_rollup_refreshes.sqland regenerateschema.sqlwithsupabase/build-schema.sh. refresh_insights_dailybody. 00162 redefined it. Its inserts now find the answers bypr.created_at >= v_from and pr.created_at < v_toinstead of(pr.created_at at time zone 'utc')::date between ..., so they read the range through(brand_id, created_at)(12 s → 0.66 s for one day of the largest brand). As written, this migration copies 00131's body and would undo that. Please start from 00162's body (with itsdeclareblock) and add the lock there.refresh_mentions_daily. 00157 added a third function with the same pattern: it deletes and reinsertsmentions_prompt_dailyandmentions_competitor_daily, which have no unique key, from the same callers (a completed run and the nightly sweep). Please give it its own lock key in the same migration.
refresh_citations_daily matches 00118, which is still current.
|
Update to my review: #867 just landed as
|
|
Correction on the migration number: 00165–00169 have landed since my last note, so please take the next free number when you rebase (00170 as of now). The rest stands: leave |
|
@shahzainshafique Before this can merge, the migration needs to shrink. Main has moved on under it:
What's still new here is the lock on #1227 does the same for the Mentions rollups and adds a dedup with unique keys. Copying that pattern for |
…llups refresh_citations_daily (00118) deletes a brand's citation_*_daily rows for a day range and inserts them again, with no lock and no unique key. A run's refresh, the nightly sweep and the topic-move rebuild can overlap on the same brand: the second refresh's delete waits for the first to commit, but its snapshot predates the first one's insert, so the days they share end up counted twice. As 00164 did for refresh_insights_daily and 00192 for refresh_mentions_daily: take a per-brand advisory lock before the deletes, drop rows written twice, and add nulls-not-distinct unique keys on citation_result_daily, citation_domain_daily and citation_url_daily so a second copy is impossible. The body is 00118's unchanged apart from the lock; schema.sql rebuilt. Closes ansvisor#1020
7e7609c to
c5a5a32
Compare
|
Hi @gkhngyk, Thanks for the follow-ups. I have rebased onto current main and cut it down as you described. 00198_serialize_citation_refreshes.sql now only touches refresh_citations_daily (00118’s body plus the per-brand lock); insights and mentions are left to 00164/00192. I also followed 00192’s pattern for the citation tables: rows written twice are dropped and nulls not distinct unique keys on all three citation_*_daily tables make a second copy impossible. schema.sql rebuilt. Re-ran the two-session check locally: with 00118, overlapping refreshes left 142 duplicated groups in citation_result_daily; with 00198, zero, and the issue’s acceptance query returns nothing. Duplicate domain/URL rows with null keys are removed (highest copy kept) and a new null-key duplicate is rejected by the unique key. A refresh of a different brand doesn’t wait on the lock. |
00198 went to the webhook event subscriptions (ansvisor#1239), so this migration moves to 00199. schema.sql is rebuilt with build-schema.sh. Claude-Session: https://claude.ai/code/session_0121amRjX7UFrgmGELAtkc7A
gkhngyk
left a comment
There was a problem hiding this comment.
Thanks, this is the right size now. The function body is 00118's plus the lock, and the dedup and unique keys follow 00192.
00198 went to #1239 a few minutes before this, so I pushed a merge commit that renames the migration to 00199_serialize_citation_refreshes.sql and rebuilds schema.sql. Before applying it to production I checked three things:
- the three citation tables had no duplicate groups;
- no citation row has a different day than its answer, so the delete and insert ranges always match;
- each statement fits the timeout.
00199 is applied: the lock is in place, all three unique keys are valid, and the row counts are unchanged. Tested with #1239–#1242: server 1,897 and web 1,796 tests pass.
… to 00200 main now has 00198 (ansvisor#1239) and 00199 (ansvisor#1131), so the soft-404 migration moves from 00198 to 00200, with the comments that name it, and schema.sql is rebuilt from the migrations. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Summary
refresh_insights_dailyandrefresh_citations_dailydelete a brand's rollup rows for a day range and reinsert them, on tables with no unique key. When two refreshes of the same brand and days overlap (a completed run, the nightly sweep and the topic-move rebuild can all fire together), the second one'sDELETEwaits for the first to commit, but its snapshot predates the first one'sINSERT. It skips those rows and adds its own, so every overlapping row ends up twice.New migration
00156_serialize_rollup_refreshes.sqltakes a transaction-scoped advisory lock per brand at the start of both functions, before the deletes, as proposed in the issue:Each function has its own key, since they write different tables. A second refresh of the same brand now waits, then deletes the first one's rows. Refreshes of different brands still run in parallel. The function bodies are 00131's and 00118's unchanged apart from the lock; I generated them by copying the originals and diffed them to confirm.
schema.sqlis regenerated (+240, purely additive).Statement timeout: the lock lasts one call.
refreshCitationHistoryrebuilds a brand inHISTORY_CHUNK_DAYS(14-day) calls, each its own transaction, so a refresh queued behind a rebuild waits for one chunk, not the whole history, and stays inside the service role's 120 sstatement_timeout(00100).Verification (local Supabase, demo seed brand)
Two sessions refresh the same brand and days at once. Session A refreshes and then holds its transaction open for 4 s; session B starts 1 s in.
refresh_citations_dailyrefresh_insights_dailyrefresh_citations_dailyrefresh_insights_daily… group by brand_id, day, model_used, platform, region, topic_id having count(*) > 1) returns nothing after the migration.Related issue
Closes #1020
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 testsChecklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromweb/) — no web changesyarn typecheckpasses (run fromweb/) — no web changesyarn format:checkpasses (run fromweb/) — no web changesNote:
00156is the next free number onmainas of this branch. If another open PR lands a migration first, I'm happy to renumber.