perf(schemas): index organization_role_user_relations on (tenant_id, organization_id, user_id)#8819
Merged
simeng-li merged 1 commit intoMay 18, 2026
Conversation
COMPARE TO
|
| Name | Diff |
|---|---|
| .changeset/index-organization-role-user-relations-org-user.md | 📈 +554 Bytes |
| packages/schemas/alterations/next-1778500001-add-organization-role-user-relations-org-user-index.ts | 📈 +1.7 KB |
| packages/schemas/tables/organization_role_user_relations.sql | 📈 +150 Bytes |
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a secondary B-tree index on organization_role_user_relations (tenant_id, organization_id, user_id) to avoid partial PK scans for queries that filter without organization_role_id.
Changes:
- New alteration
next-1778500001-add-organization-role-user-relations-org-user-index.ts(reversible). - Mirror the index inline in
tables/organization_role_user_relations.sqlfor fresh installs. - Changeset and a
.gitignoreentry fordocs/superpowers/.
Reviewed changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| packages/schemas/alterations/next-1778500001-add-organization-role-user-relations-org-user-index.ts | New up/down migration creating/dropping the index. |
| packages/schemas/tables/organization_role_user_relations.sql | Inline index definition for fresh installs. |
| .changeset/index-organization-role-user-relations-org-user.md | Patch changeset for @logto/schemas. |
| .gitignore | Ignore local docs/superpowers/ directory. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
fec6ec9 to
5abc9c6
Compare
5abc9c6 to
c24fc13
Compare
wangsijie
approved these changes
May 17, 2026
gao-sun
approved these changes
May 18, 2026
…organization_id, user_id) The primary key on organization_role_user_relations is (tenant_id, organization_id, organization_role_id, user_id). Queries that filter by (organization_id, user_id) without specifying organization_role_id cannot use the PK efficiently and degrade to a partial scan over all rows for the org. Hot call sites: - getUserScopes — invoked on every GET /organizations/:id/users/:userId/scopes. - The per-user role join inside getUsersByOrganizationId — fan-out from organization_user_relations into this table on every paginated list. - The DELETE filter in UserRoleRelationQueries.replace(). The new index is added to both the alteration script and the table source file, matching the existing convention. The name `organization_role_user_relations__tenant_id_org_id_user_id` abbreviates `organization` to `org` because the unabbreviated form would exceed the 63-char Postgres identifier limit. Refs LOG-13473
c24fc13 to
e5916dc
Compare
4 tasks
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-13473. Second migration in the Phase 0.5 performance milestone. Independent of LOG-13472 (separate table, no shared call sites); branch is cut from master, not stacked on #8818.
What changed
packages/schemas/alterations/next-1778500001-add-organization-role-user-relations-org-user-index.ts— new migration that addscreate index concurrently if not exists organization_role_user_relations__tenant_id_org_id_user_id on organization_role_user_relations (tenant_id, organization_id, user_id). Builtconcurrentlyvia thebeforeUphook so no table lock is held during the build. Reversible viabeforeDownrunningdrop index concurrently if exists.packages/schemas/tables/organization_role_user_relations.sql— mirrors the new index inline so fresh installs get it without replaying the alteration..changeset/index-organization-role-user-relations-org-user.md—@logto/schemaspatch.Expected result
Queries that filter by
(organization_id, user_id)without specifyingorganization_role_idswitch from a partial PK scan to anIndex Scanon the new secondary index. Three hot call sites this fixes:getUserScopes(packages/core/src/queries/organization/user-role-relations.ts:27-46) — everyGET /organizations/:id/users/:userId/scopes.getUsersByOrganizationId(packages/core/src/queries/organization/user-relations.ts:121-141) — fan-out into this table on every paginated list-users request.UserRoleRelationQueries.replace()— filter(organization_id, user_id).No schema, API, or behavior change. Index is purely additive.
Reviewer notes
(tenant_id, organization_id, organization_role_id, user_id)does not serve queries that skiporganization_role_id. Postgres can use the PK efficiently only when all preceding columns are equality-matched; skippingorganization_role_idforces a partial scan.tenant_idbecause every table inpackages/schemas/tables/carries the_after_each.sqlRLS policy injectingtenant_id = (select id from tenants where db_user = current_user). The index column order matches.organization_role_user_relations__tenant_id_organization_id_user_id) is 67 chars and would be silently truncated by Postgres at the 63-char identifier limit. Abbreviated toorg_id. Confirmed unique via grep.beforeUp/beforeDownnon-transactional hooks soCONCURRENTLYcan be used without holding the write-blockingSHARElock that a plainCREATE INDEXwould hold for the duration of the build. This matches the pattern in1.35.0-1765183934-add-logs-created-at-id-index.tsand1.38.0-1772615848-add-oidc-model-instances-grant-id-partial-index.ts.if not exists/if existskeep both directions idempotent against transient failures.Testing
Tested locally
Checklist
.changeset