Skip to content

Commit

Permalink
fix: improve perf in account linking (#1394)
Browse files Browse the repository at this point in the history
## What kind of change does this PR introduce?
* Currently, we use an `ilike` which doesn't use the index on
`auth.identities.email`. Since the emails in the `verifiedEmails` slice
only contains lowercased emails, and the `auth.identities.email` are
also guaranteed to be lowercase, there is no need for a case-insensitive
search
* The partial unique index (`users_email_partial_key`) in auth.users is
defined on the condition that `is_sso_user = false`. However, the query
uses `is_sso_user is false` which causes it to not use the index ever
(along with `ilike`). It would be much faster to do `lower(email) = any
(?)` rather than `email ilike any (?)`
* Fixes #1390
  • Loading branch information
kangmingtay committed Feb 5, 2024
1 parent 1f1aeea commit 8eedb95
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions internal/models/linking.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func DetermineAccountLinking(tx *storage.Connection, config *conf.GlobalConfigur
}

// we overwrite the email with the existing user's email since the user
// could have an empty empty
// could have an empty email
candidateEmail.Email = user.GetEmail()
return AccountLinkingResult{
Decision: AccountExists,
Expand Down Expand Up @@ -109,14 +109,14 @@ func DetermineAccountLinking(tx *storage.Connection, config *conf.GlobalConfigur
var similarIdentities []*Identity
var similarUsers []*User
// look for similar identities and users based on email
if terr := tx.Q().Eager().Where("email ilike any (?)", verifiedEmails).All(&similarIdentities); terr != nil {
if terr := tx.Q().Eager().Where("email = any (?)", verifiedEmails).All(&similarIdentities); terr != nil {
return AccountLinkingResult{}, terr
}

if !strings.HasPrefix(providerName, "sso:") {
// there can be multiple user accounts with the same email when is_sso_user is true
// so we just do not consider those similar user accounts
if terr := tx.Q().Eager().Where("email ilike any (?) and is_sso_user is false", verifiedEmails).All(&similarUsers); terr != nil {
if terr := tx.Q().Eager().Where("email = any (?) and is_sso_user = false", verifiedEmails).All(&similarUsers); terr != nil {
return AccountLinkingResult{}, terr
}
}
Expand Down

0 comments on commit 8eedb95

Please sign in to comment.