Skip to content

Avoid unconditional COUNT(*) query on every SCIM search/list request - #51570

Open
elhichaouiYassine wants to merge 1 commit into
keycloak:mainfrom
elhichaouiYassine:KC-51402
Open

Avoid unconditional COUNT(*) query on every SCIM search/list request#51570
elhichaouiYassine wants to merge 1 commit into
keycloak:mainfrom
elhichaouiYassine:KC-51402

Conversation

@elhichaouiYassine

Copy link
Copy Markdown
Contributor

Closes #51402

Closes keycloak#51402

Signed-off-by: elhichaouiYassine <y.elhichaoui@gmail.com>
Copilot AI balanced review requested due to automatic review settings August 10, 2026 09:19
@elhichaouiYassine
elhichaouiYassine requested a review from a team as a code owner August 10, 2026 09:19

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.

Pull request overview

Optimizes SCIM pagination by avoiding unnecessary count queries for partial result pages.

Changes:

  • Normalizes pagination parameters centrally.
  • Adds provider capability signaling for count optimization.
  • Updates user and group providers to consume normalized pagination.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
scim/services/.../ScimResourceTypeResource.java Computes totals from partial pages when possible.
scim/model/.../UserResourceTypeProvider.java Uses normalized user pagination.
scim/model/.../GroupResourceTypeProvider.java Uses normalized group pagination.
scim/core/.../ScimResourceTypeProvider.java Adds count-optimization capability API.
scim/core/.../AbstractScimResourceTypeProvider.java Enables optimization for model-backed providers.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +333 to +334
if(supportsCountOptimization && resourceSize < searchRequest.getCount()) {
totalResults = (long) (searchRequest.getStartIndex() - 1 + resourceSize);

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.

Valid finding from Copilot and with a good suggestion. This handles both when you page past the end and when you get 0 results starting from startIndex == 1 where we can decide for sure that the count is 0.

@sguilhen sguilhen 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.

Thanks for the PR @elhichaouiYassine . Copilot has flagged a valid finding, and I would suggest to both fix it and include a test case for the out-of-range pagination case.

One minor observation: normalizePagination() is only called when supportsCountOptimization` is true, which means pagination normalization and count optimization are implicitly coupled. Since they're independent concerns, it might be cleaner to always normalize.

Comment on lines +333 to +334
if(supportsCountOptimization && resourceSize < searchRequest.getCount()) {
totalResults = (long) (searchRequest.getStartIndex() - 1 + resourceSize);

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.

Valid finding from Copilot and with a good suggestion. This handles both when you page past the end and when you get 0 results starting from startIndex == 1 where we can decide for sure that the count is 0.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SCIM search/list executes unconditional COUNT(*) query on every request

3 participants