Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release-23.1: upgrades: update system.web_sessions migration to handle orphaned rows #100244

Merged
merged 1 commit into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions pkg/upgrade/upgrades/web_sessions_table_user_id_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ WHERE w.user_id IS NULL AND w.username = u.username
LIMIT 1000
`

const deleteRowsForDroppedUsersWebSessionsTableStmt = `
DELETE FROM system.web_sessions
WHERE user_id IS NULL
LIMIT 1000
`

const setUserIDColumnToNotNullWebSessionsTableStmt = `
ALTER TABLE system.web_sessions
ALTER COLUMN user_id SET NOT NULL
Expand All @@ -62,16 +68,27 @@ func backfillWebSessionsTableUserIDColumn(
ctx context.Context, cs clusterversion.ClusterVersion, d upgrade.TenantDeps,
) error {
ie := d.DB.Executor()
for {
rowsAffected, err := ie.ExecEx(ctx, "backfill-user-id-col-web-sessions-table", nil, /* txn */
sessiondata.NodeUserSessionDataOverride,
backfillUserIDColumnWebSessionsTableStmt,
)
if err != nil {
return err
}
if rowsAffected == 0 {
break
for _, op := range []struct {
name string
query string
}{
{
name: "backfill-user-id-col-web-sessions-table",
query: backfillUserIDColumnWebSessionsTableStmt,
},
{
name: "delete-rows-for-dropped-users-web-sessions-table",
query: deleteRowsForDroppedUsersWebSessionsTableStmt,
},
} {
for {
rowsAffected, err := ie.ExecEx(ctx, op.name, nil /* txn */, sessiondata.NodeUserSessionDataOverride, op.query)
if err != nil {
return err
}
if rowsAffected == 0 {
break
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ VALUES (
})
tdb.CheckQueryResults(t, "SELECT count(*) FROM system.web_sessions", [][]string{{strconv.Itoa(numUsers)}})

// Drop a user to test that migration deletes orphaned rows.
if numUsers > 0 {
tdb.Exec(t, "DROP USER testuser0")
numUsers -= 1
}

// Run migrations.
_, err := tc.Conns[0].ExecContext(ctx, `SET CLUSTER SETTING version = $1`,
clusterversion.ByKey(clusterversion.V23_1WebSessionsTableHasUserIDColumn).String())
Expand Down