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-20.2: sql: fix performance regression in user authn #58739

Merged
merged 1 commit into from
Jan 12, 2021
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
8 changes: 0 additions & 8 deletions pkg/bench/ddl_analysis/ddl_analysis_bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,6 @@ func RunRoundTripBenchmark(b *testing.B, tests []RoundTripBenchTestCase) {
// counting each "txn coordinator send" operation.
func countKvBatchRequestsInRecording(r tracing.Recording) int {
root := r[0]

// Find the topmost "flow" span to start traversing from.
for _, sp := range r {
if sp.ParentSpanID == root.SpanID && sp.Operation == "flow" {
return countKvBatchRequestsInSpan(r, sp)
}
}

return countKvBatchRequestsInSpan(r, root)
}

Expand Down
41 changes: 41 additions & 0 deletions pkg/bench/ddl_analysis/system_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2021 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package bench

import "testing"

func BenchmarkSystemDatabaseQueries(b *testing.B) {
tests := []RoundTripBenchTestCase{
// This query performs 1 extra lookup since the executor first tries to
// lookup the name `current_db.system.users`.
{
name: "select system.users without schema name",
stmt: `SELECT username, "hashedPassword" FROM system.users WHERE username = 'root'`,
},
// This query performs 4 extra lookup since the executor tries to
// lookup the name `"".system.users`. Since the "" database doesn't exist,
// it also falls back to looking up that database name in the deprecated
// namespace table.
{
name: "select system.users with empty database name",
setup: `SET sql_safe_updates = false; USE "";`,
stmt: `SELECT username, "hashedPassword" FROM system.users WHERE username = 'root'`,
},
// This query performs 2 lookups: getting the descriptor ID by name, then
// fetching the system table descriptor.
{
name: "select system.users with schema name",
stmt: `SELECT username, "hashedPassword" FROM system.public.users WHERE username = 'root'`,
},
}

RunRoundTripBenchmark(b, tests)
}
6 changes: 4 additions & 2 deletions pkg/sql/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ func retrieveUserAndPassword(

// Perform the lookup with a timeout.
err = runFn(func(ctx context.Context) error {
const getHashedPassword = `SELECT "hashedPassword" FROM system.users ` +
// Use fully qualified table name to avoid looking up "".system.users.
const getHashedPassword = `SELECT "hashedPassword" FROM system.public.users ` +
`WHERE username=$1`
values, err := ie.QueryRowEx(
ctx, "get-hashed-pwd", nil, /* txn */
Expand All @@ -131,7 +132,8 @@ func retrieveUserAndPassword(
return nil
}

getLoginDependencies := `SELECT option, value FROM system.role_options ` +
// Use fully qualified table name to avoid looking up "".system.role_options.
getLoginDependencies := `SELECT option, value FROM system.public.role_options ` +
`WHERE username=$1 AND option IN ('NOLOGIN', 'VALID UNTIL')`

loginDependencies, err := ie.QueryEx(
Expand Down