Skip to content

Commit

Permalink
server: fix bottlenecks when purging system.web_sessions
Browse files Browse the repository at this point in the history
The previous purging limit was found to be too low which resulted in the
`system.web_sessions` table growing rapidly. Additionally, we were using
`ORDER BY random()` when deleting expired sessions from the table which
caused a full table scan of a large table. This resulted in high cpu
usage on every purge attempt with only very few rows actually being
deleted. Over time, this turns into a nonideal feedback loop.

This change fixes the two issues by:
1. Increasing the defauilt purging limit to 1000 from 10.
2. Remove the `ORDER BY random()` when purging the table to avoid full
   table scans on every purge.

support ticket: cockroachlabs/support#1812

resolves cockroachdb#88622

Release note: None
  • Loading branch information
aadityasondhi committed Sep 26, 2022
1 parent 089d9c0 commit 13e8f4e
Showing 1 changed file with 1 addition and 4 deletions.
5 changes: 1 addition & 4 deletions pkg/server/purge_auth_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var (
settings.TenantWritable,
"server.web_session.purge.max_deletions_per_cycle",
"the maximum number of old sessions to delete for each purge",
10,
1000,
).WithPublic()
)

Expand Down Expand Up @@ -90,21 +90,18 @@ func (s *authenticationServer) purgeOldSessions(ctx context.Context) {
deleteOldExpiredSessionsStmt = `
DELETE FROM system.web_sessions
WHERE "expiresAt" < $1
ORDER BY random()
LIMIT $2
RETURNING 1
`
deleteOldRevokedSessionsStmt = `
DELETE FROM system.web_sessions
WHERE "revokedAt" < $1
ORDER BY random()
LIMIT $2
RETURNING 1
`
deleteSessionsAutoLogoutStmt = `
DELETE FROM system.web_sessions
WHERE "lastUsedAt" < $1
ORDER BY random()
LIMIT $2
RETURNING 1
`
Expand Down

0 comments on commit 13e8f4e

Please sign in to comment.