Repository navigation
core: don't add DISTINCT to users list when filtering by type - #26413
Merged
BeryJu merged 1 commit intoSep 27, 2026
Merged
Conversation
`UsersFilter.type` is a `MultipleChoiceFilter`, which defaults to `distinct=True` because filters across to-many relationships can return duplicate rows. `type` is a column on the user table, so it can't, but the DISTINCT made the paginator count and every page evaluate the superuser annotation for, and sort, all matching users (all columns), usually on disk.
✅ Deploy Preview for authentik-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
BeryJu
approved these changes
Sep 24, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #26413 +/- ##
==========================================
- Coverage 92.13% 92.09% -0.04%
==========================================
Files 1177 1177
Lines 76842 76872 +30
Branches 4121 4121
==========================================
- Hits 70796 70793 -3
- Misses 5993 6026 +33
Partials 53 53
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
rissson
approved these changes
Sep 24, 2026
Contributor
|
🍒 Cherry-pick to |
BeryJu
pushed a commit
that referenced
this pull request
Nov 3, 2026
…-pick #26413 to version-2026.8) (#27463) core: don't add DISTINCT to users list when filtering by type (#26413) `UsersFilter.type` is a `MultipleChoiceFilter`, which defaults to `distinct=True` because filters across to-many relationships can return duplicate rows. `type` is a column on the user table, so it can't, but the DISTINCT made the paginator count and every page evaluate the superuser annotation for, and sort, all matching users (all columns), usually on disk. Co-authored-by: Ryan Pesek <[email protected]>
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.
Details
What does this PR change?
Sets
distinct=Falseon thetypefilter of the users API (UsersFilter), asmembers_by_pkalready does on the groups filter.MultipleChoiceFilterdefaults todistinct=True, because filters across to-many relationships can return duplicate rows.typeis a column on the user table, so it can't. The users filters that do join (groups_by_*,roles_by_*,is_superuser) keep their ownDISTINCT.Why is this change needed?
With
?type=..., the page query becomesSELECT DISTINCT <all user columns> ...and the paginator count becomesSELECT COUNT(*) FROM (SELECT DISTINCT ...). Before returning one page, both evaluate theis_superuserannotation for every matching user and sort all of them by every column, includingattributesandpassword. WithoutDISTINCT, the count is a plainCOUNT(*)(Django drops the unused annotation), and the page query stops afterOFFSET + LIMITrows.This hits directory sync clients that page through all users of one type (e.g.
?type=internal&path=...&page_size=200): every request sorts the whole set twice.EXPLAIN ANALYZEon PostgreSQL 15 with the defaultwork_mem(4 MB), 10,000 users matchingtype=internal&path=...(~1.5 KB ofattributeseach),page_size=200:OFFSET 4800)In one of our deployments, this list wrote ~3.2 GB of temporary files in under two hours.
How was this tested?
authentik/core/tests/test_users_api.py:test_filter_typenow checks the results and count for one and several types (before, it only checked the status code)test_filter_type_no_distinct: the list and count queries aren'tDISTINCT(fails without the change)test_filter_type_with_groups:typetogether withgroups_by_pkreturns a user who is in both groups once (fails ifgroups_by_pkstops deduplicating)black,ruff,banditandmypy --strictare cleanAI usage: I used an AI coding agent to help investigate the queries and draft the change, tests and description. I reviewed and edited all of it and ran the checks above.
Linked issues
None.
Checklist
make all)make docs)