Skip to content

[DB] [Migration Needed] Add indexes on _portal_domain (domain, app_id)#5724

Open
pkong-ds wants to merge 1 commit into
authgear:mainfrom
pkong-ds:index-portal-domain
Open

[DB] [Migration Needed] Add indexes on _portal_domain (domain, app_id)#5724
pkong-ds wants to merge 1 commit into
authgear:mainfrom
pkong-ds:index-portal-domain

Conversation

@pkong-ds
Copy link
Copy Markdown
Contributor

@pkong-ds pkong-ds commented May 17, 2026

Motivation

GetAppIDByDomain (WHERE domain = ?) and GetDomainsByAppID (WHERE app_id = ?) both do full table scans on _portal_domain because no index exists on either column.

The table _portal_domain grows with tenant count

  • at 50k rows (in shared buffers) the domain lookup costs 1788 planner units vs 8 with the index (~210x).

Downtime

  • Uses CREATE INDEX CONCURRENTLY + notransaction so the build holds no ShareLock and writes are uninterrupted — safe to apply without a downtime window.
  • Drop also uses DROP INDEX CONCURRENTLY.

Tests

I asked claude to write some integration tests - which was TDD and highly detailed.'

But I think those are maintenance burden rather than helpful, so did not include in this PR

GetAppIDByDomain (WHERE domain = ?) and GetDomainsByAppID (WHERE app_id = ?)
both do full table scans on _portal_domain because no index exists on either
column. The table grows with tenant count; at 50k rows (in shared buffers)
the domain lookup costs 1788 planner units vs 8 with the index (~210x).

Uses CREATE INDEX CONCURRENTLY + notransaction so the build holds no
ShareLock and writes are uninterrupted — safe to apply without a downtime
window. Drop also uses DROP INDEX CONCURRENTLY.
@pkong-ds pkong-ds changed the title [DB] [Migration Needed] Add indexes on _portal_domain (domain, app_id) [DB] [Migration Needed] Add indexes on _portal_domain (domain, app_id) May 17, 2026
@tung2744
Copy link
Copy Markdown
Contributor

Just curious what makes you find this out? Given the size of that table I think having an index should have not much performance gain?

@pkong-ds
Copy link
Copy Markdown
Contributor Author

pkong-ds commented May 18, 2026

Just curious what makes you find this out? Given the size of that table I think having an index should have not much performance gain?

I was profiling authgear locally, to see long loading of /token /userinfo or .well-known/... are code problems.

Turns out code-level no problem - and this very, very little optimization is the only problem that claude found. So I thought, ok, doesn't hurt to open a PR for this fix, however trivial this is.

Other suspects of long loading?

  • deployment specific - network latency, deployment regions, etc etc (me no access)
  • blocking webhook calls - In authgear.yaml hook.blocking_handlers oidc.jwt.pre_create or oidc.id_token.pre_create
    • but these only affects /oauth2/token - so most likely deployment configuration
  • dead/obsolete/not well-bookkeeped records in redis/db?
  • ...

@tung2744 tung2744 self-assigned this May 18, 2026
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.

2 participants