Skip to content

Commit 02b17cb

Browse files
committed
sql/pgwire: use a single query to update last_login_time
Rather than using a separate query for each user, now we will run one query to update multiple users at once. Release note: None
1 parent 76bf8cc commit 02b17cb

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

pkg/sql/pgwire/last_login_updater.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,10 @@ func (u *lastLoginUpdater) processPendingUpdates(ctx context.Context) error {
9696
u.mu.pendingUsers = make(map[username.SQLUsername]struct{})
9797
u.mu.Unlock()
9898

99-
// Update last login time for all pending users.
100-
for _, user := range users {
101-
if err := sql.UpdateLastLoginTime(ctx, u.execCfg, user.SQLIdentifier()); err != nil {
102-
return err
103-
}
99+
// Update last login time for all pending users in a single query.
100+
err := sql.UpdateLastLoginTime(ctx, u.execCfg, users)
101+
if err != nil {
102+
return err
104103
}
105104
return nil
106105
}

pkg/sql/user.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -837,16 +837,28 @@ func updateUserPasswordHash(
837837
})
838838
}
839839

840-
// UpdateLastLoginTime updates the estimated_last_login_time column for the user
840+
// UpdateLastLoginTime updates the estimated_last_login_time column for the users
841841
// logging in to the current time in the system.users table.
842-
func UpdateLastLoginTime(ctx context.Context, execCfg *ExecutorConfig, dbUser string) error {
842+
func UpdateLastLoginTime(
843+
ctx context.Context, execCfg *ExecutorConfig, dbUsers []username.SQLUsername,
844+
) error {
845+
if len(dbUsers) == 0 {
846+
return nil
847+
}
848+
843849
now := timeutil.Now()
844850
if err := execCfg.InternalDB.DescsTxn(ctx, func(ctx context.Context, txn descs.Txn) error {
851+
// Convert SQLUsername slice to string slice for the SQL query.
852+
usernames := make([]string, len(dbUsers))
853+
for i, user := range dbUsers {
854+
usernames[i] = user.Normalized()
855+
}
856+
845857
if _, err := txn.Exec(
846858
ctx, "UpdateLastLoginTime-authsuccess", txn.KV(),
847-
"UPDATE system.users SET estimated_last_login_time = $1 WHERE username = $2",
859+
"UPDATE system.users SET estimated_last_login_time = $1 WHERE username = ANY($2)",
848860
now,
849-
dbUser,
861+
usernames,
850862
); err != nil {
851863
return err
852864
}

0 commit comments

Comments
 (0)