perf(migrations): bulk backfill in 0268 release_authorization_to_pro#15044
Open
Maffooch wants to merge 1 commit into
Open
perf(migrations): bulk backfill in 0268 release_authorization_to_pro#15044Maffooch wants to merge 1 commit into
Maffooch wants to merge 1 commit into
Conversation
Replace the row-by-row get_or_create in backfill_authorized_users with a set-based, in-memory-deduplicated, batched bulk_create, and add progress logging. The old backfill issued ~2 queries per membership pair, and the group flattening ran a Dojo_Group_Member query per Product_Group / Product_Type_Group row plus a get_or_create per member -- O(groups x members) sequential round-trips. On a 15k-user / 17k-product instance this was ~1.9M queries and took many hours, with no log output to show progress. Now: one pass over Dojo_Group_Member into an in-memory group->users map (reused by both the group-grant expansion and the Global_Role flag updates), pairs deduplicated in a set, then bulk_create(batch_size=1000, ignore_conflicts=True) per through table. ignore_conflicts + the through table's unique constraint preserves the idempotency get_or_create provided. logger.info now reports detection, per-batch progress, flag counts, and completion. Benchmarked against a 15k/17k dataset: ~1,900-2,000x fewer queries (e.g. 175,438 -> 92 at 86k pairs); results verified identical and idempotent on re-run. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A customer with 15k users / 17k products reported that
dojo/db_migrations/0268_release_authorization_to_pro.pytook many hours to run.The slow part is
backfill_authorized_users, which translates RBAC rows into thelegacy
authorized_usersM2M:get_or_createperProduct_Member/Product_Type_Memberrow — aSELECT+ conditionalINSERTeach.Dojo_Group_Memberquery for everyProduct_Group/Product_Type_Grouprow, then aget_or_createper member —O(groups × members)sequential round-trips.At customer scale this is ~1.9M sequential queries, which on a remote/managed Postgres
(with pghistory audit triggers per INSERT) reaches multiple hours.
Fix
Dojo_Group_Memberinto an in-memorygroup_id → [user_id]map, reused by the group-grant expansion and theGlobal_Roleflag updates.(obj_id, user_id)pairs into aset(in-memory dedup), thenbulk_create(batch_size=1000, ignore_conflicts=True)per through table.ignore_conflicts+ the unique constraint preserves theget_or_createidempotency.Global_Role→is_superuser/is_staffupdates stay bulk.update()and reuse the map.logger.infofor detection, per-batch progress, flag counts, and completion (matches the0082/0201migration pattern).The migration's operations list is unchanged (
makemigrations --checkreports no changes) — only theRunPythoncallable internals changed.Benchmarks (dockerized Postgres, results identical at every scale)
~1,900–2,000× fewer queries — the load-independent proof. The old path is
latency-bound on sequential round-trips; the new path issues a near-constant handful.
Verification
is_superuser/is_staffflags correct.🤖 Generated with Claude Code