Repository navigation
core: users/groups reduce number of database queries - #20431
Conversation
✅ Deploy Preview for authentik-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #20431 +/- ##
==========================================
- Coverage 93.21% 93.13% -0.09%
==========================================
Files 1016 1016
Lines 58042 58089 +47
Branches 327 327
==========================================
- Hits 54104 54100 -4
- Misses 3938 3989 +51
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
✅ Deploy Preview for authentik-integrations ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for authentik-storybook ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
There was a problem hiding this comment.
Hi Ryan!
Re:
- Bulk validation for users field
Added BulkPrimaryKeyRelatedField and BulkManyRelatedField classes that validate all user PKs in a single COUNT query instead of one query per PK.Before: Updating a group with 1000 users triggered 1000 individual SELECT queries to validate each user PK exists.
After: A single SELECT COUNT(*) query validates all PKs exist. Per-PK queries only run when there's an actual validation error (to report which specific PK is invalid).
How did you produce this issue? I tried on current main (ea4848c at the time of writing) with a group that has exactly 1000 users, from the admin interface, and found that whether I
- update the group object (I toggled
is_superuser), or - update group memberships (I added a 1001st user)
the number of SQL queries was way below 1000. Scanning through them I didn't find a section that seemed to validate current users.
Am I misunderstanding something? Or is this only an issue in a very specific case I didn't try?
Hey @gergosimonyi thanks for taking a look. This fix is for the PUT endpoint |
|
Ohhh, I see. We don't allow the viewset's default This seems like a reasonable workaround until such time that the issue is fixed in DRF. They already know about it: encode/django-rest-framework#9607 Thank you for this contribuiton! |
* main: (782 commits) web: bump knip from 6.4.1 to 6.6.0 in /web (#21957) core: bump github.com/getsentry/sentry-go from 0.45.1 to 0.46.0 (#21955) core: bump uvicorn[standard] from 0.44.0 to 0.45.0 (#21956) core: bump rustls from 0.23.39 to 0.23.40 (#21958) core: support hashed password in users API + automated install (#18686) core, web: update translations (#21952) providers/saml: generate issuer url when provider is set on app (#18022) root: fix rust build with uv-installed Python (#21858) core: add support for hiding applications from the user dashboard (#21530) core: bump ruff from 0.15.11 to 0.15.12 (#21871) packages/ak-axum/router: add X-Powered-By to all responses (#21940) core: bump microsoft-kiota-serialization-form from 1.9.8 to v1.10.1 (#21909) core: bump pytest-randomly from 4.0.1 to 4.1.0 (#21873) core: users/groups reduce number of database queries (#20431) core: bump types-channels from 4.3.0.20260408 to 4.3.0.20260421 (#21872) ci: bump taiki-e/install-action from 2.75.21 to 2.75.22 in /.github/actions/setup (#21877) core, web: update translations (#21870) sources/oauth: ensure user ID is returned as str (#21880) translate: Updates for project authentik and language no_NO (#21862) core: bump maxminddb from 3.0.0 to v3.1.1 (#21907) ...
Summary
Optimizations for the users and groups API endpoints to reduce N+1 queries and database load.
Changes
Users API
Changed is_superuser from a direct BooleanField to a SerializerMethodField that uses a database annotation. The get_queryset method now annotates each user with _annotated_is_superuser using an Exists subquery that checks superuser group membership (including inherited membership via group ancestry).
Before: Each user's is_superuser property triggered a separate query to check group membership.
After: A single annotated query determines superuser status for all users in the response.
When include_groups=false or include_roles=false, the queryset now uses Prefetch with .only() to fetch just the primary keys instead of skipping the prefetch entirely.
Why: The groups and roles PKs are always serialized in the response. Previously, when include_groups=false, no prefetch occurred, causing N+1 queries when serializing the PK list. Now we always prefetch, but use ID-only queries when full objects aren't needed.
Groups API
Changed get_queryset to always prefetch parents and children alongside roles, removing the conditional prefetch logic.
Why: The parents and children PKs are always serialized in the response regardless of the include_parents/include_children query parameters (those control whether the full objects are included). Always prefetching avoids N+1 queries when serializing the PK lists.
Added BulkPrimaryKeyRelatedField and BulkManyRelatedField classes that validate all user PKs in a single COUNT query instead of one query per PK.
Before: Updating a group with 1000 users triggered 1000 individual SELECT queries to validate each user PK exists.
After: A single SELECT COUNT(*) query validates all PKs exist. Per-PK queries only run when there's an actual validation error (to report which specific PK is invalid).
Checklist
ak test authentik/)make lint-fix)If an API change has been made
make gen-build)If changes to the frontend have been made
make web)If applicable
make docs)