Skip to content

core: don't add DISTINCT to users list when filtering by type - #26413

Merged
BeryJu merged 1 commit into
goauthentik:mainfrom
ryanpesek:core/api/users-type-filter-no-distinct
Sep 27, 2026
Merged

BeryJu merged 1 commit into
goauthentik:mainfrom
ryanpesek:core/api/users-type-filter-no-distinct

Conversation

@ryanpesek

Copy link
Copy Markdown
Contributor

Details

What does this PR change?

Sets distinct=False on the type filter of the users API (UsersFilter), as members_by_pk already does on the groups filter.

MultipleChoiceFilter 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. The users filters that do join (groups_by_*, roles_by_*, is_superuser) keep their own DISTINCT.

Why is this change needed?

With ?type=..., the page query becomes SELECT DISTINCT <all user columns> ... and the paginator count becomes SELECT COUNT(*) FROM (SELECT DISTINCT ...). Before returning one page, both evaluate the is_superuser annotation for every matching user and sort all of them by every column, including attributes and password. Without DISTINCT, the count is a plain COUNT(*) (Django drops the unused annotation), and the page query stops after OFFSET + LIMIT rows.

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 ANALYZE on PostgreSQL 15 with the default work_mem (4 MB), 10,000 users matching type=internal&path=... (~1.5 KB of attributes each), page_size=200:

Before After
Count ~200 ms, external merge sort (17 MB on disk) ~4 ms, no sort
Page 1 ~185 ms, 17 MB on disk ~4 ms
Page 25 (OFFSET 4800) ~225 ms, 17 MB on disk ~100 ms
Whole request, pages 1 / 25 / 50 ~615 / 625 / 615 ms ~240 / 320 / 420 ms

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_type now 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't DISTINCT (fails without the change)
    • test_filter_type_with_groups: type together with groups_by_pk returns a user who is in both groups once (fails if groups_by_pk stops deduplicating)
  • Existing core, RBAC and API search tests pass; black, ruff, bandit and mypy --strict are clean
  • The benchmark above returns the same results and counts before and after

AI 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

  • The project has been linted, built, and tested (make all)
  • The documentation has been updated and formatted (make docs)
  • I have read the AI usage policy.

`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.
@ryanpesek
ryanpesek requested a review from a team as a code owner September 24, 2026 12:52
@netlify

netlify Bot commented Sep 24, 2026

Copy link
Copy Markdown

✅ Deploy Preview for authentik-docs ready!

Name Link
🔨 Latest commit ed604d2
🔍 Latest deploy log https://app.netlify.com/projects/authentik-docs/deploys/6ab51d088c8b3d00090d01d7
😎 Deploy Preview https://deploy-preview-26413--authentik-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@codecov

codecov Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.09%. Comparing base (0e7ad46) to head (ed604d2).
⚠️ Report is 13 commits behind head on main.

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              
Flag Coverage Δ
conformance 33.30% <3.12%> (-0.02%) ⬇️
e2e 38.07% <3.12%> (-0.04%) ⬇️
integration 29.61% <3.12%> (-0.46%) ⬇️
rust 44.79% <ø> (ø)
unit 93.62% <100.00%> (+<0.01%) ⬆️
unit-migrate 93.63% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@BeryJu
BeryJu merged commit 2f005db into goauthentik:main Sep 27, 2026
139 checks passed
@dewi-tik dewi-tik added the backport/version-2026.8 Add this label to PRs to backport changes to version-2026.8 label Nov 2, 2026
@authentik-cherry-pick

Copy link
Copy Markdown
Contributor

🍒 Cherry-pick to version-2026.8 created: #27463

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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport/version-2026.8 Add this label to PRs to backport changes to version-2026.8

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants