8000 fix(keycloak-core): Only use DISTINCT in user searches when the query joins other tables by SamTV12345 · Pull Request #54026 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

fix(keycloak-core): Only use DISTINCT in user searches when the query joins other tables - #54026

Open
SamTV12345 wants to merge 4 commits into
keycloak:mainfrom
SamTV12345:fix/user-search-distinct-51385
Open

SamTV12345 wants to merge 4 commits into
keycloak:mainfrom
SamTV12345:fix/user-search-distinct-51385

Conversation

@SamTV12345
Copy link
Copy Markdown

Hi,

this fixes my issue that I linked below. I added the AI recommended fix as this is actually the solution. Only when a join happens we can get duplicated results. I also verified this behaviour with the below tests.

CC: @stianst and @ssilvert

Closes #51385

searchForUserStream always issued SELECT DISTINCT since the fix for
multi-valued attribute searches (keycloak#39246). For a plain listing the query
selects from USER_ENTITY alone, so the primary key already makes every
row unique and DISTINCT only forces the database to sort the whole realm
before applying the LIMIT instead of walking the (realm_id, username)
index. On a realm with 380k users this turned a 0.1 ms query into a
1.3 s query that spilled to disk.

Apply the same rule the count queries already use since keycloak#44860: DISTINCT
is only needed when predicates() joined USER_ATTRIBUTE or
FEDERATED_IDENTITY, which can match several rows per user.

The new model test captures the generated SQL and checks that a plain
listing runs without DISTINCT while attribute and IdP searches keep it
and still return each user once.

Closes keycloak#51385

Signed-off-by: SamTV12345 <40429738+SamTV12345@users.noreply.github.com>
@SamTV12345
SamTV12345 requested a review from a team as a code owner October 15, 2026 13:33
Copilot AI balanced review requested due to automatic review settings October 15, 2026 13:33
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@SamTV12345
SamTV12345 requested a balanced review from Copilot October 18, 2026 10:28
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI balanced review requested due to automatic review settings October 18, 2026 10:29
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No blocking issues were identified in the focused change and regression coverage; tests were not executed.

No open issues 🎉

Review effort: Balanced

@SamTV12345
Copy link
Copy Markdown
Author

🟢 Approval recommended

No blocking issues were identified in the focused change and regression coverage; tests were not executed.

No open issues 🎉

Review effort: Balanced

Yay

Copilot AI balanced review requested due to automatic review settings November 10, 2026 18:24
@SamTV12345
Copy link
Copy Markdown
Author
8000

@ssilvert What about the mr? :)

Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues block the change to apply DISTINCT only to JPA user searches with joins.

No open issues 🎉

Review effort: Balanced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Query Performance degradation for user stream is slowed down by distinct query

2 participants

0