perf(core): aggregate roles via LATERAL in getUsersByOrganizationId#8826
Merged
simeng-li merged 1 commit intoMay 19, 2026
Conversation
COMPARE TO
|
| Name | Diff |
|---|---|
| .changeset/perf-list-org-users-lateral.md | 📈 +1.05 KB |
| packages/core/src/queries/organization/user-relations.ts | 📈 +766 Bytes |
| packages/integration-tests/src/tests/api/organization/organization-user.test.ts | 📈 +3.17 KB |
Contributor
There was a problem hiding this comment.
Pull request overview
Rewrites the entities branch of getUsersByOrganizationId so role aggregation runs inside a LEFT JOIN LATERAL subquery (one execution per user row) instead of joining roles up-front and collapsing with GROUP BY users.id. This lets LIMIT/OFFSET prune users before role lookups, leveraging the recently added organization_role_user_relations__tenant_id_org_id_user_id index. An explicit ORDER BY fields.userId replaces the implicit ordering that the previous GROUP BY provided.
Changes:
- Replaces the role join +
aggregateRoles()+GROUP BY users.idwith a per-userLATERALsubquery emitting theorganizationRolesJSON array directly. - Adds explicit
ORDER BY ${fields.userId}to keep pagination ordering deterministic. - Adds a
@logto/corepatch changeset documenting the performance fix.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| packages/core/src/queries/organization/user-relations.ts | Rewrites entities query in getUsersByOrganizationId to use a LATERAL role-aggregation subquery and explicit ordering. |
| .changeset/perf-list-org-users-lateral.md | Patch changeset describing the perf fix. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
aecad0f to
296ce5f
Compare
The entities query joined organization_role_user_relations and organization_roles, then collapsed back with GROUP BY users.id before applying LIMIT. Postgres had to materialize the full members x roles_per_member intermediate join on every paginated request regardless of page size — a 20-row page over a 10k-member org with 3 roles each produced ~30k intermediate rows. The rewrite moves the per-user role aggregation into a LATERAL subquery joined to the outer user set. LIMIT now prunes user rows before the role-table lookups fire, and each lateral lookup hits organization_role_user_relations__tenant_id_org_id_user_id (the index landed in LOG-13473) directly. ORDER BY fields.userId is added explicitly. The previous query relied on GROUP BY users.id to stabilize row ordering for pagination; without it, page boundaries would be non-deterministic. fields.userId is the relation table's own column and is NOT NULL, so the ordering remains stable even for the edge case where the LEFT JOIN to users produces a NULL users.id. Refs LOG-13475
296ce5f to
1707952
Compare
wangsijie
approved these changes
May 19, 2026
gao-sun
approved these changes
May 19, 2026
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.
Summary
Refs LOG-13475. Fourth task in the Phase 0.5 performance milestone. Branched from current master — both prerequisite indexes (Perf-A merged via #8818, Perf-B merged via #8819) are already there.
What changed
packages/core/src/queries/organization/user-relations.ts—getUsersByOrganizationIdentities query rewritten:LEFT JOIN organization_role_user_relations JOIN organization_roles GROUP BY users.idto aLEFT JOIN LATERAL (...) ON TRUEsubquery that produces exactly oneorganizationRolesJSON array per user row.INNER JOINbetweenorganization_role_user_relationsandorganization_roles, so thefilter (where roles.id is not null)guard fromaggregateRoles()is no longer needed;coalesce(json_agg(...), '[]'::json)handles the empty-role case.ORDER BY fields.userId(the relation table'suser_id, NOT NULL by table definition) is added explicitly. The previous query relied onGROUP BY users.idto stabilize pagination row order; without it, page boundaries would be non-deterministic. Usingfields.userIdinstead ofusers.fields.idkeeps the sort key non-null even if the LEFT JOIN touserswere ever to produce an orphan row.count(*)remains consistent with the entities branch..changeset/perf-list-org-users-lateral.md—@logto/corepatch.Expected result
GET /organizations/:id/users?page=1&page_size=20on a 10k-member org with an average of 3 roles per user runs the role-aggregation lateral ~20 times (one per user in the page) instead of materializing a ~30k-row intermediate join. Each lateral execution hitsorganization_role_user_relations__tenant_id_org_id_user_id(added by LOG-13473 / #8819) directly.organizationRolesarray contents (ids + names, ordered by role name), same[]for users with no roles, same[totalCount, entities]return shape, same pagination semantics.Reviewer notes
GROUP BYin master; a CTE wouldn't help because the planner still needs to know whether to materialize the full join before LIMIT.aggregateRoles()inqueries/organization/utils.tsis still in use bygetOrganizationsByUserId(which has the symmetric problem on the org-side but isn't in this PR's scope). The aggregation is inlined here instead of reusing the helper because the lateral form uses an INNER JOIN inside the subquery, so the helper'sfilter (where roles.id is not null)guard isn't needed.organizationRolescontents, same orphan-row behavior, same identifier casing on the returned column.Testing
Tested locally
Checklist
.changeset