diff --git a/docs/generated/sql/bnf/stmt_block.bnf b/docs/generated/sql/bnf/stmt_block.bnf index 9eca79ac53c7..fa5e7c04ac4a 100644 --- a/docs/generated/sql/bnf/stmt_block.bnf +++ b/docs/generated/sql/bnf/stmt_block.bnf @@ -1886,6 +1886,7 @@ session_var ::= | 'LC_COLLATE' | 'LC_CTYPE' | 'TIME' 'ZONE' + | 'VIRTUAL_CLUSTER_NAME' var_name ::= name diff --git a/pkg/server/server_sql.go b/pkg/server/server_sql.go index 2f9ab30e73d2..b75519cf54ef 100644 --- a/pkg/server/server_sql.go +++ b/pkg/server/server_sql.go @@ -98,6 +98,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/scheduledlogging" "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scdeps" "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scexec" + "github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants" "github.com/cockroachdb/cockroach/pkg/sql/sem/eval" "github.com/cockroachdb/cockroach/pkg/sql/sessiondata" "github.com/cockroachdb/cockroach/pkg/sql/sessiondatapb" @@ -1040,6 +1041,10 @@ func newSQLServer(ctx context.Context, cfg sqlServerArgs) (*SQLServer, error) { AutoConfigProvider: cfg.AutoConfigProvider, } + if codec.ForSystemTenant() { + execCfg.VirtualClusterName = catconstants.SystemTenantName + } + if sqlSchemaChangerTestingKnobs := cfg.TestingKnobs.SQLSchemaChanger; sqlSchemaChangerTestingKnobs != nil { execCfg.SchemaChangerTestingKnobs = sqlSchemaChangerTestingKnobs.(*sql.SchemaChangerTestingKnobs) } else { @@ -1438,6 +1443,7 @@ func (s *SQLServer) preStart( // from KV (or elsewhere). if entry, _ := s.tenantConnect.TenantInfo(); entry.Name != "" { s.cfg.idProvider.SetTenantName(entry.Name) + s.execCfg.VirtualClusterName = entry.Name } if err := s.startCheckService(ctx, stopper); err != nil { return err diff --git a/pkg/sql/exec_util.go b/pkg/sql/exec_util.go index 5ca65353bcdb..36604268684c 100644 --- a/pkg/sql/exec_util.go +++ b/pkg/sql/exec_util.go @@ -1447,6 +1447,10 @@ type ExecutorConfig struct { // AutoConfigProvider informs the auto config runner job of new // tasks to run. AutoConfigProvider acprovider.Provider + + // VirtualClusterName contains the name of the virtual cluster + // (tenant). + VirtualClusterName roachpb.TenantName } // UpdateVersionSystemSettingHook provides a callback that allows us diff --git a/pkg/sql/parser/sql.y b/pkg/sql/parser/sql.y index 7b14410c7dc3..4d01958622af 100644 --- a/pkg/sql/parser/sql.y +++ b/pkg/sql/parser/sql.y @@ -7486,6 +7486,7 @@ session_var: // TIME ZONE is special: it is two tokens, but is really the identifier "TIME ZONE". | TIME ZONE { $$ = "timezone" } | TIME error // SHOW HELP: SHOW SESSION +| VIRTUAL_CLUSTER_NAME session_var_parts: '.' IDENT diff --git a/pkg/sql/tests/BUILD.bazel b/pkg/sql/tests/BUILD.bazel index 818d08501531..2316a4bfbd9d 100644 --- a/pkg/sql/tests/BUILD.bazel +++ b/pkg/sql/tests/BUILD.bazel @@ -6,10 +6,12 @@ go_library( "command_filters.go", "data.go", "explain_test_util.go", + "virtual_cluster_name.go", ], importpath = "github.com/cockroachdb/cockroach/pkg/sql/tests", visibility = ["//visibility:public"], deps = [ + "//pkg/base", "//pkg/internal/sqlsmith", "//pkg/kv", "//pkg/kv/kvpb", @@ -18,7 +20,10 @@ go_library( "//pkg/sql/sem/tree", "//pkg/storage", "//pkg/testutils/serverutils", + "//pkg/testutils/sqlutils", "//pkg/testutils/storageutils", + "//pkg/util/leaktest", + "//pkg/util/log", "//pkg/util/syncutil", "@com_github_cockroachdb_errors//:errors", "@org_golang_x_text//cases", diff --git a/pkg/sql/tests/virtual_cluster_name.go b/pkg/sql/tests/virtual_cluster_name.go new file mode 100644 index 000000000000..5e5b8fce0821 --- /dev/null +++ b/pkg/sql/tests/virtual_cluster_name.go @@ -0,0 +1,51 @@ +// Copyright 2023 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 tests + +import ( + "context" + gosql "database/sql" + "testing" + + "github.com/cockroachdb/cockroach/pkg/base" + "github.com/cockroachdb/cockroach/pkg/testutils/serverutils" + "github.com/cockroachdb/cockroach/pkg/testutils/sqlutils" + "github.com/cockroachdb/cockroach/pkg/util/leaktest" + "github.com/cockroachdb/cockroach/pkg/util/log" +) + +func TestClusterName(t *testing.T) { + defer leaktest.AfterTest(t)() + defer log.Scope(t).Close(t) + + ctx := context.Background() + s, db, _ := serverutils.StartServer(t, base.TestServerArgs{ + DefaultTestTenant: base.TestControlsTenantsExplicitly, + }) + defer s.Stopper().Stop(ctx) + + checkName := func(t *testing.T, db *gosql.DB, expected string) { + sql := sqlutils.MakeSQLRunner(db) + sql.CheckQueryResults(t, `SHOW virtual_cluster_name`, [][]string{{expected}}) + } + + t.Run("system", func(t *testing.T) { + checkName(t, db, "system") + }) + + t.Run("virtual", func(t *testing.T) { + _, db2 := serverutils.StartTenant(t, s, base.TestTenantArgs{ + TenantID: serverutils.TestTenantID(), + TenantName: "virtual", + }) + checkName(t, db2, "virtual") + }) +} diff --git a/pkg/sql/vars.go b/pkg/sql/vars.go index 3f020a15257e..bcf05e2fc30e 100644 --- a/pkg/sql/vars.go +++ b/pkg/sql/vars.go @@ -1624,6 +1624,14 @@ var varGen = map[string]sessionVar{ }, }, + // CockroachDB extension. + `virtual_cluster_name`: { + Hidden: true, + Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) { + return string(evalCtx.ExecCfg.VirtualClusterName), nil + }, + }, + // CockroachDB extension. `allow_prepare_as_opt_plan`: { Hidden: true,