Repository navigation
fix(mentions): never serve a Perception snapshot older than its rollups - #1392
Conversation
Follow-ups from ansvisor#1385's review: - 00246: refresh_mention_terms_daily sets the brand's snapshots aside at its end, in the rebuild's transaction, so the default view reads the new rollups live until the snapshots are refreshed, even when that refresh fails or the rollups were rebuilt in SQL. - The deleted-prompt drain refreshes the snapshots only after the terms refresh has run. - The slow note is cleared when its read's effect ends, so going back to filters whose last read was slow doesn't show it before SLOW_MS. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@gkhngyk #1385's three follow-ups, as you suggested them. 1: the set-aside in |
gkhngyk
left a comment
There was a problem hiding this comment.
Looks good. All three items from #1385's review are done:
-
A snapshot is never older than its rollups.
refresh_mention_terms_dailynow sets the brand's snapshots aside as its last statement, inside the rebuild's transaction (00246_perception_snapshots_follow_rollups.sql:98-100). Every rollup rebuild reaches it:refresh_mention_termscalls it last, for runs, the sweep and the history backfill.- The drain and the terms backfill call it directly.
Each of these server paths refreshes the snapshots afterwards. A refresh that started before the set-aside can't write over it, because its
computed_atis earlier. -
The drain refreshes only after a rebuild.
if (ok) { … }is atserver/src/lib/insights-rollups.js:138-141, with the b1 assertion atinsights-rollups.test.js:278-279. -
The slow note is cleared in the effect's cleanup (
_perception.tsx:86-87). Without the cleanup,slowwould still hold the June read's key and the note would show at once, so the new test'stoBe('')(_perception.test.tsx:352) catches it.
Checked on production:
-
Same function, plus one statement. I compared production's
refresh_mention_terms_daily(pg_get_functiondef) with 00246, comments stripped. The only difference is the three-lineupdateat the end.- The header is unchanged: security invoker,
search_path = public. - Execute stays with postgres and service_role.
create or replacekeeps the ACL, and therevokechanges nothing. - The only other statement is the table comment.
- service_role has UPDATE on
mentions_perception_snapshots. - The only SQL caller is
refresh_mention_terms, which is also service-role only, so no other role runs the new update.
- The header is unchanged: security invoker,
-
Cost. I timed both versions on the largest brand as
pg_tempcopies in a rolled-back block:Call Before After One day (a run's call) 0.78–0.91 s 0.75–0.86 s A 7-day drain chunk 6.2 s 5.8 s The added update takes 0.5 ms for the brand's three rows, so the difference is within noise.
-
Locks. The update row-locks the brand's three snapshot rows from the update until commit. Commit follows at once, because it is the last statement of both functions.
- The Perception read is a plain select and never waits on these locks.
- A snapshot refresh computes its payload first and only locks its row in the upsert. Either side waits at most for the other's commit.
- No new lock cycle: the refresh holds no other lock, and the advisory lock is unchanged.
- A backfill in SQL should still commit per call, as the server scripts do. A single transaction over many brands would hold these rows, and widen the gap the PR describes, until it commits.
-
Applying. The migration changes no rows. The existing snapshots are served until each brand's next rebuild (its next run, the nightly sweep or a drain). That rebuild sets them aside, and the server refreshes them right after.
-
Order. None is needed between the migration and the server. The deployed server already refreshes after every rebuild path, and the drain change only skips a wasted refresh.
The trade-off you mention: from the first set-aside until the refresh after it, the default view reads live. On the largest brand, that covers the remaining day calls of the run or the sweep, plus 3–16 s for the three windows. Runs and the sweep come about twice a night. For a deleted prompt whose answers go back to June, the drain's 21 weekly chunks at about 6 s each make it about 2 minutes. That is fine as it is.
gkhngyk
left a comment
There was a problem hiding this comment.
Re-approving after merging main into the branch. The merge only brings in the squashed parent PR; the tree matches the one tested (web and server tests, typecheck, lint, format).
Summary
The follow-ups from #1385's review. The Perception tab's default view never shows figures older than its rollups. The drain no longer recomputes views for a brand whose rebuild failed. The slow note waits its 4 s again.
A snapshot is set aside as soon as its rollups change. Migration 00246 redefines
refresh_mention_terms_daily(00231) with one addition at its end, in the same transaction:This is the update
forget_mentions_perception_snapshotsmakes. Every rebuild of the Perception rollups goes through this function: a completed run, the nightly sweep, the deleted-prompt drain, or a backfill in SQL. So from the moment new rollups commit, the default view reads them live until the server refreshes the snapshots.mentions-rollups.jsandinsights-rollups.jsalready said.computed_at, and the guard keeps it from writing over the set-aside row.The drain refreshes the snapshots only after the terms refresh has run. This is the shape suggested in the review. A brand whose Insights rebuild keeps failing no longer recomputes its three windows every 5 minutes.
The slow note waits again.
slowis cleared when its read's effect ends. Going back to filters whose earlier read was slow no longer shows the note beforeSLOW_MS.Related issue
Follow-up to #1385 (review notes 1–3). No GitHub issue.
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
select refresh_mention_terms_daily('<brand>', current_date - 2, current_date);. Its three rows inmentions_perception_snapshotsnow readday_to = 1970-01-01.refresh_mentions_perception_snapshot('<brand>', d)for 7, 30 and 90. They read today again.I ran steps 1–2 on the dev DB (00246 applied) for the demo brand: set aside after the rollups, back to today after the refresh.
Tests:
insights-rollups.test.js: b1's views aren't refreshed when its rebuild failed. "Perception rollups fail to rebuild" still holds._perception.test.tsx: back on filters whose last read was slow, the note waits its 4 s. The test fails without the fix.Deploy note: 00246 needs only 00241. Apply it any time before or with the server deploy.
Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromweb/)yarn typecheckpasses (run fromweb/)yarn format:checkpasses (run fromweb/)Also run:
yarn testandyarn build;npm run format,npm run lintandnpm test;bash supabase/build-schema.sh.🤖 Generated with Claude Code