fix: increase performance of ListUser by login name ignore case (#12350)

# Which Problems Are Solved

Login v2 uses the `users.v2.ListUsers`-endpoint to get a user by login
name. This query had an inefficient `WHERE`-clause.

# How the Problems Are Solved

Update the view and use a specific clause for this query.

# Additional information

Added in https://github.com/zitadel/zitadel/pull/10475

---------

Co-authored-by: Marco A. <[email protected]>
Co-authored-by: Livio Spring <[email protected]>
This commit is contained in:
Silvan
2026-06-30 11:20:21 +00:00
committed by GitHub
co-authored by Marco A. Livio Spring
parent 561da54ff7
commit f9995ee39c
11 changed files with 137 additions and 27 deletions
+15
View File
@@ -70,6 +70,8 @@ type InitColumn struct {
nullable bool
defaultValue interface{}
deleteCascade string
generated string
isStored bool
}
type ColumnOption func(*InitColumn)
@@ -105,6 +107,13 @@ func DeleteCascade(column string) ColumnOption {
}
}
func Generated(stmt string, isStored bool) ColumnOption {
return func(c *InitColumn) {
c.generated = stmt
c.isStored = isStored
}
}
type PrimaryKey []string
func NewPrimaryKey(columnNames ...string) PrimaryKey {
@@ -381,6 +390,12 @@ func createColumnsStatement(cols []*InitColumn, tableName string) string {
if col.defaultValue != nil {
column += " DEFAULT " + defaultValue(col.defaultValue)
}
if col.generated != "" {
column += " GENERATED ALWAYS AS (" + col.generated + ")"
if col.isStored {
column += " STORED"
}
}
if len(col.deleteCascade) != 0 {
column += fmt.Sprintf(" REFERENCES %s (%s) ON DELETE CASCADE", tableName, col.deleteCascade)
}
+8
View File
@@ -15,6 +15,10 @@ var (
name: projection.LoginNameCol,
table: loginNameTable,
}
LoginNameNameLowerCol = Column{
name: projection.LoginNameLowerCol,
table: loginNameTable,
}
LoginNameIsPrimaryCol = Column{
name: projection.LoginNameIsPrimaryCol,
table: loginNameTable,
@@ -23,4 +27,8 @@ var (
name: projection.LoginNameInstanceIDCol,
table: loginNameTable,
}
LoginNameResourceOwnerCol = Column{
name: projection.LoginNameResourceOwnerCol,
table: loginNameTable,
}
)
+21 -23
View File
@@ -22,10 +22,12 @@ const (
LoginNamePolicyProjectionTable = LoginNameProjectionTable + "_" + loginNamePolicySuffix
LoginNameDomainProjectionTable = LoginNameProjectionTable + "_" + loginNameDomainSuffix
LoginNameCol = "login_name"
LoginNameUserCol = "user_id"
LoginNameIsPrimaryCol = "is_primary"
LoginNameInstanceIDCol = "instance_id"
LoginNameCol = "login_name"
LoginNameUserCol = "user_id"
LoginNameIsPrimaryCol = "is_primary"
LoginNameInstanceIDCol = "instance_id"
LoginNameLowerCol = "login_name_lower"
LoginNameResourceOwnerCol = "resource_owner"
usersAlias = "users"
policyCustomAlias = "policy_custom"
@@ -76,8 +78,7 @@ func (*loginNameProjection) Init() *old_handler.Check {
[]*handler.InitColumn{
handler.NewColumn(LoginNameUserIDCol, handler.ColumnTypeText),
handler.NewColumn(LoginNameUserUserNameCol, handler.ColumnTypeText),
// TODO: implement computed columns
// handler.NewComputedColumn(loginNameUserUserNameLowerCol, handler.ColumnTypeText),
handler.NewColumn(loginNameUserUserNameLowerCol, handler.ColumnTypeText, handler.Generated("lower("+LoginNameUserUserNameCol+")", true)),
handler.NewColumn(LoginNameUserResourceOwnerCol, handler.ColumnTypeText),
handler.NewColumn(LoginNameUserInstanceIDCol, handler.ColumnTypeText),
},
@@ -93,33 +94,30 @@ func (*loginNameProjection) Init() *old_handler.Check {
),
),
),
// TODO: uncomment the following line when login_names4 will be created
// handler.WithIndex(
// handler.NewIndex("search", []string{LoginNameUserInstanceIDCol, loginNameUserUserNameLowerCol},
// handler.WithInclude(LoginNameUserResourceOwnerCol),
// ),
// ),
handler.WithIndex(
handler.NewIndex("search", []string{LoginNameUserInstanceIDCol, loginNameUserUserNameLowerCol},
handler.WithInclude(LoginNameUserResourceOwnerCol),
),
),
),
handler.NewSuffixedTable(
[]*handler.InitColumn{
handler.NewColumn(LoginNameDomainNameCol, handler.ColumnTypeText),
// TODO: implement computed columns
// handler.NewComputedColumn(loginNameDomainNameLowerCol, handler.ColumnTypeText),
handler.NewColumn(loginNameDomainNameLowerCol, handler.ColumnTypeText, handler.Generated("lower("+LoginNameDomainNameCol+")", true)),
handler.NewColumn(LoginNameDomainIsPrimaryCol, handler.ColumnTypeBool, handler.Default(false)),
handler.NewColumn(LoginNameDomainResourceOwnerCol, handler.ColumnTypeText),
handler.NewColumn(LoginNameDomainInstanceIDCol, handler.ColumnTypeText),
},
handler.NewPrimaryKey(LoginNameDomainInstanceIDCol, LoginNameDomainResourceOwnerCol, LoginNameDomainNameCol),
loginNameDomainSuffix,
// TODO: uncomment the following line when login_names4 will be created
// handler.WithIndex(
// handler.NewIndex("search", []string{LoginNameDomainInstanceIDCol, LoginNameDomainResourceOwnerCol, loginNameDomainNameLowerCol}),
// ),
// handler.WithIndex(
// handler.NewIndex("search_result", []string{LoginNameDomainInstanceIDCol, LoginNameDomainResourceOwnerCol},
// handler.WithInclude(LoginNameDomainIsPrimaryCol),
// ),
// ),
handler.WithIndex(
handler.NewIndex("search", []string{LoginNameDomainInstanceIDCol, LoginNameDomainResourceOwnerCol, loginNameDomainNameLowerCol}),
),
handler.WithIndex(
handler.NewIndex("search_result", []string{LoginNameDomainInstanceIDCol, LoginNameDomainResourceOwnerCol},
handler.WithInclude(LoginNameDomainIsPrimaryCol),
),
),
),
handler.NewSuffixedTable(
[]*handler.InitColumn{
@@ -6,6 +6,11 @@ SELECT
END AS login_name
, COALESCE(d.is_primary, TRUE) AS is_primary
, u.instance_id
, u.resource_owner
, CASE
WHEN p.must_be_domain THEN CONCAT(u.user_name_lower, '@', d.name_lower)
ELSE u.user_name_lower
END AS login_name_lower
FROM
projections.login_names3_users AS u
LEFT JOIN LATERAL (
+1
View File
@@ -221,6 +221,7 @@ func Projections() []projection {
func Init(ctx context.Context) error {
for _, p := range projections {
if err := p.Init(ctx); err != nil {
logging.WithError(err).WithField("projection", p.ProjectionName()).Error("failed to initialize projection")
return err
}
}
+10 -2
View File
@@ -803,13 +803,21 @@ func NewUserLoginNameExistsQuery(value string, comparison TextComparison) (Searc
if err != nil {
return nil, err
}
resourceOwnerQuery, err := NewColumnComparisonQuery(LoginNameResourceOwnerCol, UserResourceOwnerCol, ColumnEquals)
if err != nil {
return nil, err
}
// text query to select data from the linked sub select
loginNameQuery, err := NewTextQuery(LoginNameNameCol, value, comparison)
var loginNameQuery SearchQuery
loginNameQuery, err = NewTextQuery(LoginNameNameCol, value, comparison)
if comparison == TextEqualsIgnoreCase {
loginNameQuery, err = NewTextQuery(LoginNameNameLowerCol, strings.ToLower(value), TextEquals)
}
if err != nil {
return nil, err
}
// full definition of the sub select
subSelect, err := NewSubSelect(LoginNameUserIDCol, []SearchQuery{instanceQuery, userIDQuery, loginNameQuery})
subSelect, err := NewSubSelect(LoginNameUserIDCol, []SearchQuery{instanceQuery, userIDQuery, resourceOwnerQuery, loginNameQuery})
if err != nil {
return nil, err
}