diff --git a/DEPS.bzl b/DEPS.bzl index 0f75000dcb4a..fc6dbdd161a0 100644 --- a/DEPS.bzl +++ b/DEPS.bzl @@ -1485,10 +1485,10 @@ def go_deps(): patches = [ "@com_github_cockroachdb_cockroach//build/patches:com_github_cockroachdb_pebble.patch", ], - sha256 = "76575b5e3c38334308d006475c1ec3cdf714ceaa4ae3baef8945046f943fab75", - strip_prefix = "github.com/cockroachdb/pebble@v0.0.0-20221207223118-463b049c1dd1", + sha256 = "0401364025128ab1d3d592293d0c3e1109eb6b50f6c30bb948c5743e7200e9e1", + strip_prefix = "github.com/cockroachdb/pebble@v0.0.0-20221212215000-0893071d8a52", urls = [ - "https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/pebble/com_github_cockroachdb_pebble-v0.0.0-20221207223118-463b049c1dd1.zip", + "https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/pebble/com_github_cockroachdb_pebble-v0.0.0-20221212215000-0893071d8a52.zip", ], ) go_repository( @@ -9614,10 +9614,10 @@ def go_deps(): name = "org_golang_x_sys", build_file_proto_mode = "disable_global", importpath = "golang.org/x/sys", - sha256 = "5ebd34faf34cfb7824ed6d12e95e793c8564ccc989af9f9c7998422c36ec9838", - strip_prefix = "golang.org/x/sys@v0.0.0-20220811171246-fbc7d0a398ab", + sha256 = "cbdf18e3be9f211ca962684ac19d41d4be24042898ec0cc664bf8d1c2955d384", + strip_prefix = "golang.org/x/sys@v0.3.0", urls = [ - "https://storage.googleapis.com/cockroach-godeps/gomod/golang.org/x/sys/org_golang_x_sys-v0.0.0-20220811171246-fbc7d0a398ab.zip", + "https://storage.googleapis.com/cockroach-godeps/gomod/golang.org/x/sys/org_golang_x_sys-v0.3.0.zip", ], ) go_repository( diff --git a/build/bazelutil/distdir_files.bzl b/build/bazelutil/distdir_files.bzl index 149fd5512351..5f562b8c2547 100644 --- a/build/bazelutil/distdir_files.bzl +++ b/build/bazelutil/distdir_files.bzl @@ -193,7 +193,7 @@ DISTDIR_FILES = { "https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/google-api-go-client/com_github_cockroachdb_google_api_go_client-v0.80.1-0.20221117193156-6a9f7150cb93.zip": "b3378c579f4f4340403038305907d672c86f615f8233118a8873ebe4229c4f39", "https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/gostdlib/com_github_cockroachdb_gostdlib-v1.19.0.zip": "c4d516bcfe8c07b6fc09b8a9a07a95065b36c2855627cb3514e40c98f872b69e", "https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/logtags/com_github_cockroachdb_logtags-v0.0.0-20211118104740-dabe8e521a4f.zip": "1972c3f171f118add3fd9e64bcea6cbb9959a3b7fa0ada308e8a7310813fea74", - "https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/pebble/com_github_cockroachdb_pebble-v0.0.0-20221207223118-463b049c1dd1.zip": "76575b5e3c38334308d006475c1ec3cdf714ceaa4ae3baef8945046f943fab75", + "https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/pebble/com_github_cockroachdb_pebble-v0.0.0-20221212215000-0893071d8a52.zip": "0401364025128ab1d3d592293d0c3e1109eb6b50f6c30bb948c5743e7200e9e1", "https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/redact/com_github_cockroachdb_redact-v1.1.3.zip": "7778b1e4485e4f17f35e5e592d87eb99c29e173ac9507801d000ad76dd0c261e", "https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/returncheck/com_github_cockroachdb_returncheck-v0.0.0-20200612231554-92cdbca611dd.zip": "ce92ba4352deec995b1f2eecf16eba7f5d51f5aa245a1c362dfe24c83d31f82b", "https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/sentry-go/com_github_cockroachdb_sentry_go-v0.6.1-cockroachdb.2.zip": "fbb2207d02aecfdd411b1357efe1192dbb827959e36b7cab7491731ac55935c9", @@ -896,7 +896,7 @@ DISTDIR_FILES = { "https://storage.googleapis.com/cockroach-godeps/gomod/golang.org/x/oauth2/org_golang_x_oauth2-v0.0.0-20220411215720-9780585627b5.zip": "81f60a99f4f3bcb34993ca5831386d8399c472a0ca4dc6f1e3659a071d002029", "https://storage.googleapis.com/cockroach-godeps/gomod/golang.org/x/perf/org_golang_x_perf-v0.0.0-20180704124530-6e6d33e29852.zip": "a2c7d02cc94c4ba767b6322f70ddcba4941cb5f60fed1bada3aa7a4d3a8128f1", "https://storage.googleapis.com/cockroach-godeps/gomod/golang.org/x/sync/org_golang_x_sync-v0.0.0-20220722155255-886fb9371eb4.zip": "39d2431ae1db11be34b4012b719e7bcec1572006a122d44509da038a5b498ff1", - "https://storage.googleapis.com/cockroach-godeps/gomod/golang.org/x/sys/org_golang_x_sys-v0.0.0-20220811171246-fbc7d0a398ab.zip": "5ebd34faf34cfb7824ed6d12e95e793c8564ccc989af9f9c7998422c36ec9838", + "https://storage.googleapis.com/cockroach-godeps/gomod/golang.org/x/sys/org_golang_x_sys-v0.3.0.zip": "cbdf18e3be9f211ca962684ac19d41d4be24042898ec0cc664bf8d1c2955d384", "https://storage.googleapis.com/cockroach-godeps/gomod/golang.org/x/term/org_golang_x_term-v0.0.0-20210927222741-03fcf44c2211.zip": "3adf713afa49fe26580ffe4adb1f4fb2f4921c945301aa5a9fb6d34031fa30cd", "https://storage.googleapis.com/cockroach-godeps/gomod/golang.org/x/text/org_golang_x_text-v0.3.7.zip": "e1a9115e61a38da8bdc893d0ba83b65f89cc1114f152a98eb572c5ea6551e8d4", "https://storage.googleapis.com/cockroach-godeps/gomod/golang.org/x/time/org_golang_x_time-v0.0.0-20210723032227-1f47c861a9ac.zip": "e5d8ade42804ec7d96a632c031dde7db087e8bc4cd5dfd6f38df03ce4f16d9b6", diff --git a/go.mod b/go.mod index 3791f19a3f02..0224e63526c0 100644 --- a/go.mod +++ b/go.mod @@ -24,7 +24,7 @@ require ( golang.org/x/net v0.0.0-20220722155237-a158d28d115b golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 - golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab + golang.org/x/sys v0.3.0 golang.org/x/text v0.3.7 golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac golang.org/x/tools v0.1.12 @@ -112,7 +112,7 @@ require ( github.com/cockroachdb/go-test-teamcity v0.0.0-20191211140407-cff980ad0a55 github.com/cockroachdb/gostdlib v1.19.0 github.com/cockroachdb/logtags v0.0.0-20211118104740-dabe8e521a4f - github.com/cockroachdb/pebble v0.0.0-20221207223118-463b049c1dd1 + github.com/cockroachdb/pebble v0.0.0-20221212215000-0893071d8a52 github.com/cockroachdb/redact v1.1.3 github.com/cockroachdb/returncheck v0.0.0-20200612231554-92cdbca611dd github.com/cockroachdb/stress v0.0.0-20220803192808-1806698b1b7b diff --git a/go.sum b/go.sum index f187b9dce26e..e266979e12bd 100644 --- a/go.sum +++ b/go.sum @@ -475,8 +475,8 @@ github.com/cockroachdb/gostdlib v1.19.0/go.mod h1:+dqqpARXbE/gRDEhCak6dm0l14AaTy github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f/go.mod h1:i/u985jwjWRlyHXQbwatDASoW0RMlZ/3i9yJHE2xLkI= github.com/cockroachdb/logtags v0.0.0-20211118104740-dabe8e521a4f h1:6jduT9Hfc0njg5jJ1DdKCFPdMBrp/mdZfCpa5h+WM74= github.com/cockroachdb/logtags v0.0.0-20211118104740-dabe8e521a4f/go.mod h1:Vz9DsVWQQhf3vs21MhPMZpMGSht7O/2vFW2xusFUVOs= -github.com/cockroachdb/pebble v0.0.0-20221207223118-463b049c1dd1 h1:KRKt6vnkAGZ4AOlPIwSq7GvVGdbCEzLFXWQD9OgmhlE= -github.com/cockroachdb/pebble v0.0.0-20221207223118-463b049c1dd1/go.mod h1:8vvNzfaCFGp5Yvnqu0+a1LCL5i+NCID7YsNdhe0xhM8= +github.com/cockroachdb/pebble v0.0.0-20221212215000-0893071d8a52 h1:kT8xHygytiJSQpqKv01NpGMG3ZdTlCjr9lW5K5RDh+k= +github.com/cockroachdb/pebble v0.0.0-20221212215000-0893071d8a52/go.mod h1:JsehdjcR1QgLZkqBeYrbVdE3cdxbdrycA/PN+Cg+RNw= github.com/cockroachdb/redact v1.1.3 h1:AKZds10rFSIj7qADf0g46UixK8NNLwWTNdCIGS5wfSQ= github.com/cockroachdb/redact v1.1.3/go.mod h1:BVNblN9mBWFyMyqK1k3AAiSxhvhfK2oOZZ2lK+dpvRg= github.com/cockroachdb/returncheck v0.0.0-20200612231554-92cdbca611dd h1:KFOt5I9nEKZgCnOSmy8r4Oykh8BYQO8bFOTgHDS8YZA= @@ -2716,8 +2716,9 @@ golang.org/x/sys v0.0.0-20220513210249-45d2b4557a2a/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ= +golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= diff --git a/pkg/bench/rttanalysis/testdata/benchmark_expectations b/pkg/bench/rttanalysis/testdata/benchmark_expectations index 8f6a1e46e4eb..1bd78e78bbbb 100644 --- a/pkg/bench/rttanalysis/testdata/benchmark_expectations +++ b/pkg/bench/rttanalysis/testdata/benchmark_expectations @@ -33,29 +33,29 @@ exp,benchmark 14,CreateRole/create_role_with_no_options 10,DropDatabase/drop_database_0_tables 11,DropDatabase/drop_database_1_table -12,DropDatabase/drop_database_2_tables -13,DropDatabase/drop_database_3_tables +11,DropDatabase/drop_database_2_tables +11,DropDatabase/drop_database_3_tables 18,DropRole/drop_1_role 26,DropRole/drop_2_roles 34,DropRole/drop_3_roles 11,DropSequence/drop_1_sequence 12,DropSequence/drop_2_sequences -13,DropSequence/drop_3_sequences +12,DropSequence/drop_3_sequences 11,DropTable/drop_1_table 12,DropTable/drop_2_tables -13,DropTable/drop_3_tables +12,DropTable/drop_3_tables 12,DropView/drop_1_view 13,DropView/drop_2_views 13,DropView/drop_3_views 10,Grant/grant_all_on_1_table -11,Grant/grant_all_on_2_tables -11,Grant/grant_all_on_3_tables +10,Grant/grant_all_on_2_tables +10,Grant/grant_all_on_3_tables 11,GrantRole/grant_1_role 15,GrantRole/grant_2_roles -3,ORMQueries/activerecord_type_introspection_query -6,ORMQueries/django_table_introspection_1_table -6,ORMQueries/django_table_introspection_4_tables -6,ORMQueries/django_table_introspection_8_tables +4,ORMQueries/activerecord_type_introspection_query +8,ORMQueries/django_table_introspection_1_table +8,ORMQueries/django_table_introspection_4_tables +8,ORMQueries/django_table_introspection_8_tables 2,ORMQueries/has_column_privilege_using_attnum 2,ORMQueries/has_column_privilege_using_column_name 1,ORMQueries/has_schema_privilege_1 @@ -69,19 +69,19 @@ exp,benchmark 6,ORMQueries/has_table_privilege_5 85,ORMQueries/hasura_column_descriptions 85,ORMQueries/hasura_column_descriptions_8_tables -6,ORMQueries/hasura_column_descriptions_modified -4,ORMQueries/information_schema._pg_index_position -3,ORMQueries/pg_attribute -3,ORMQueries/pg_class -7,ORMQueries/pg_is_other_temp_schema -7,ORMQueries/pg_is_other_temp_schema_multiple_times -4,ORMQueries/pg_my_temp_schema -4,ORMQueries/pg_my_temp_schema_multiple_times -4,ORMQueries/pg_namespace -3,ORMQueries/pg_type +8,ORMQueries/hasura_column_descriptions_modified +6,ORMQueries/information_schema._pg_index_position +5,ORMQueries/pg_attribute +5,ORMQueries/pg_class +9,ORMQueries/pg_is_other_temp_schema +9,ORMQueries/pg_is_other_temp_schema_multiple_times +6,ORMQueries/pg_my_temp_schema +6,ORMQueries/pg_my_temp_schema_multiple_times +6,ORMQueries/pg_namespace +5,ORMQueries/pg_type 10,Revoke/revoke_all_on_1_table -11,Revoke/revoke_all_on_2_tables -11,Revoke/revoke_all_on_3_tables +10,Revoke/revoke_all_on_2_tables +10,Revoke/revoke_all_on_3_tables 9,RevokeRole/revoke_1_role 11,RevokeRole/revoke_2_roles 1,SystemDatabaseQueries/select_system.users_with_empty_database_Name diff --git a/pkg/ccl/backupccl/restore_job.go b/pkg/ccl/backupccl/restore_job.go index 7626a6925019..531d1f512d4e 100644 --- a/pkg/ccl/backupccl/restore_job.go +++ b/pkg/ccl/backupccl/restore_job.go @@ -2077,7 +2077,7 @@ func (r *restoreResumer) publishDescriptors( // Write the new TableDescriptors and flip state over to public so they can be // accessed. for i := range details.TableDescs { - mutTable := all.LookupDescriptorEntry(details.TableDescs[i].GetID()).(*tabledesc.Mutable) + mutTable := all.LookupDescriptor(details.TableDescs[i].GetID()).(*tabledesc.Mutable) // Note that we don't need to worry about the re-validated indexes for descriptors // with a declarative schema change job. if mutTable.GetDeclarativeSchemaChangerState() != nil { @@ -2132,7 +2132,7 @@ func (r *restoreResumer) publishDescriptors( // For all of the newly created types, make type schema change jobs for any // type descriptors that were backed up in the middle of a type schema change. for i := range details.TypeDescs { - typ := all.LookupDescriptorEntry(details.TypeDescs[i].GetID()).(catalog.TypeDescriptor) + typ := all.LookupDescriptor(details.TypeDescs[i].GetID()).(catalog.TypeDescriptor) newTypes = append(newTypes, typ.TypeDesc()) if typ.GetDeclarativeSchemaChangerState() == nil && typ.HasPendingSchemaChanges() { @@ -2144,19 +2144,19 @@ func (r *restoreResumer) publishDescriptors( } } for i := range details.SchemaDescs { - sc := all.LookupDescriptorEntry(details.SchemaDescs[i].GetID()).(catalog.SchemaDescriptor) + sc := all.LookupDescriptor(details.SchemaDescs[i].GetID()).(catalog.SchemaDescriptor) newSchemas = append(newSchemas, sc.SchemaDesc()) } for i := range details.DatabaseDescs { - db := all.LookupDescriptorEntry(details.DatabaseDescs[i].GetID()).(catalog.DatabaseDescriptor) + db := all.LookupDescriptor(details.DatabaseDescs[i].GetID()).(catalog.DatabaseDescriptor) newDBs = append(newDBs, db.DatabaseDesc()) } for i := range details.FunctionDescs { - fn := all.LookupDescriptorEntry(details.FunctionDescs[i].GetID()).(catalog.FunctionDescriptor) + fn := all.LookupDescriptor(details.FunctionDescs[i].GetID()).(catalog.FunctionDescriptor) newFunctions = append(newFunctions, fn.FuncDesc()) } b := txn.NewBatch() - if err := all.ForEachDescriptorEntry(func(desc catalog.Descriptor) error { + if err := all.ForEachDescriptor(func(desc catalog.Descriptor) error { d := desc.(catalog.MutableDescriptor) d.SetPublic() return descsCol.WriteDescToBatch( @@ -2250,7 +2250,7 @@ func prefetchDescriptors( got[i].GetID(), got[i].GetVersion(), expVersion[id], ) } - all.UpsertDescriptorEntry(got[i]) + all.UpsertDescriptor(got[i]) } return all.Catalog, nil } diff --git a/pkg/ccl/backupccl/restore_planning.go b/pkg/ccl/backupccl/restore_planning.go index d13220b3a864..c3d7e280f639 100644 --- a/pkg/ccl/backupccl/restore_planning.go +++ b/pkg/ccl/backupccl/restore_planning.go @@ -768,7 +768,7 @@ func maybeUpgradeDescriptors(descs []catalog.Descriptor, skipFKsWithNoMatchingTa // A data structure for efficient descriptor lookup by ID or by name. descCatalog := &nstree.MutableCatalog{} for _, d := range descs { - descCatalog.UpsertDescriptorEntry(d) + descCatalog.UpsertDescriptor(d) } for j, desc := range descs { @@ -781,7 +781,7 @@ func maybeUpgradeDescriptors(descs []catalog.Descriptor, skipFKsWithNoMatchingTa if err := b.RunPostDeserializationChanges(); err != nil { return errors.NewAssertionErrorWithWrappedErrf(err, "error during RunPostDeserializationChanges") } - err := b.RunRestoreChanges(descCatalog.LookupDescriptorEntry) + err := b.RunRestoreChanges(descCatalog.LookupDescriptor) if err != nil { return err } diff --git a/pkg/ccl/backupccl/show.go b/pkg/ccl/backupccl/show.go index 6fddcdfb3480..ddfd1343c2e9 100644 --- a/pkg/ccl/backupccl/show.go +++ b/pkg/ccl/backupccl/show.go @@ -704,7 +704,7 @@ func backupShowerDefault(p sql.PlanHookState, showSchemas bool, opts tree.KVOpti if len(hydratedDescriptors) == 0 { var c nstree.MutableCatalog for _, desc := range descriptors { - c.UpsertDescriptorEntry(desc) + c.UpsertDescriptor(desc) } if err := descs.HydrateCatalog(ctx, c); err != nil { return "", err diff --git a/pkg/ccl/serverccl/diagnosticsccl/reporter_test.go b/pkg/ccl/serverccl/diagnosticsccl/reporter_test.go index f02b64ae0cbd..7fc1b664422c 100644 --- a/pkg/ccl/serverccl/diagnosticsccl/reporter_test.go +++ b/pkg/ccl/serverccl/diagnosticsccl/reporter_test.go @@ -88,7 +88,7 @@ func TestTenantReport(t *testing.T) { require.NotZero(t, len(last.FeatureUsage)) // Call PeriodicallyReportDiagnostics and ensure it sends out a report. - reporter.PeriodicallyReportDiagnostics(ctx, rt.server.Stopper()) + reporter.PeriodicallyReportDiagnostics(ctx, tenant.Stopper()) testutils.SucceedsSoon(t, func() error { if rt.diagServer.NumRequests() != 2 { return errors.Errorf("did not receive a diagnostics report") diff --git a/pkg/sql/catalog/catalog.go b/pkg/sql/catalog/catalog.go index 689927d6baaa..30443454fee3 100644 --- a/pkg/sql/catalog/catalog.go +++ b/pkg/sql/catalog/catalog.go @@ -59,6 +59,7 @@ type VirtualSchemas interface { GetVirtualSchema(schemaName string) (VirtualSchema, bool) GetVirtualSchemaByID(id descpb.ID) (VirtualSchema, bool) GetVirtualObjectByID(id descpb.ID) (VirtualObject, bool) + Visit(func(desc Descriptor, comment string) error) error } // VirtualSchema represents a collection of VirtualObjects. diff --git a/pkg/sql/catalog/dbdesc/database_test.go b/pkg/sql/catalog/dbdesc/database_test.go index faa67a6dfea1..998c7262ab7c 100644 --- a/pkg/sql/catalog/dbdesc/database_test.go +++ b/pkg/sql/catalog/dbdesc/database_test.go @@ -267,14 +267,14 @@ func TestValidateCrossDatabaseReferences(t *testing.T) { var cb nstree.MutableCatalog test.desc.Privileges = privilege desc := NewBuilder(&test.desc).BuildImmutable() - cb.UpsertDescriptorEntry(desc) + cb.UpsertDescriptor(desc) test.multiRegionEnum.Privileges = privilege - cb.UpsertDescriptorEntry(typedesc.NewBuilder(&test.multiRegionEnum).BuildImmutable()) + cb.UpsertDescriptor(typedesc.NewBuilder(&test.multiRegionEnum).BuildImmutable()) for _, schemaDesc := range test.schemaDescs { schemaDesc.Privileges = privilege - cb.UpsertDescriptorEntry(schemadesc.NewBuilder(&schemaDesc).BuildImmutable()) + cb.UpsertDescriptor(schemadesc.NewBuilder(&schemaDesc).BuildImmutable()) } - _ = cb.ForEachDescriptorEntry(func(desc catalog.Descriptor) error { + _ = cb.ForEachDescriptor(func(desc catalog.Descriptor) error { cb.UpsertNamespaceEntry(desc, desc.GetID(), desc.GetModificationTime()) return nil }) diff --git a/pkg/sql/catalog/descs/collection.go b/pkg/sql/catalog/descs/collection.go index 37c744b92e15..e436461455be 100644 --- a/pkg/sql/catalog/descs/collection.go +++ b/pkg/sql/catalog/descs/collection.go @@ -30,11 +30,11 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog/internal/validate" "github.com/cockroachdb/cockroach/pkg/sql/catalog/lease" "github.com/cockroachdb/cockroach/pkg/sql/catalog/nstree" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/schemadesc" "github.com/cockroachdb/cockroach/pkg/sql/catalog/systemschema" "github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants" "github.com/cockroachdb/cockroach/pkg/sql/sem/catid" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" - "github.com/cockroachdb/cockroach/pkg/sql/sqlerrors" "github.com/cockroachdb/cockroach/pkg/sql/sqlliveness" "github.com/cockroachdb/cockroach/pkg/sql/sqlutil" "github.com/cockroachdb/cockroach/pkg/util/hlc" @@ -375,7 +375,7 @@ func (tc *Collection) DeleteNamespaceEntryToBatch( } func (tc *Collection) markAsShadowedName(id descpb.ID) { - desc := tc.cr.Cache().LookupDescriptorEntry(id) + desc := tc.cr.Cache().LookupDescriptor(id) if desc == nil { return } @@ -696,274 +696,438 @@ func newMutableSyntheticDescriptorAssertionError(id descpb.ID) error { return errors.AssertionFailedf("attempted mutable access of synthetic descriptor %d", id) } -// GetAllDescriptorsForDatabase retrieves the complete set of descriptors -// in the requested database. -func (tc *Collection) GetAllDescriptorsForDatabase( - ctx context.Context, txn *kv.Txn, db catalog.DatabaseDescriptor, -) (nstree.Catalog, error) { - read, err := tc.cr.ScanNamespaceForDatabaseSchemasAndObjects(ctx, txn, db) +// GetAll returns all descriptors, namespace entries, comments and +// zone configs visible by the transaction. +func (tc *Collection) GetAll(ctx context.Context, txn *kv.Txn) (nstree.Catalog, error) { + stored, err := tc.cr.ScanAll(ctx, txn) + if err != nil { + return nstree.Catalog{}, err + } + ret, err := tc.aggregateAllLayers(ctx, txn, stored) if err != nil { return nstree.Catalog{}, err } - var ids catalog.DescriptorIDSet - ids.Add(db.GetID()) - _ = read.ForEachNamespaceEntry(func(e nstree.NamespaceEntry) error { - if e.GetParentID() != db.GetID() || tc.isShadowedName(e) { + return ret.Catalog, nil +} + +// GetAllDatabases is like GetAll but filtered to non-dropped databases. +func (tc *Collection) GetAllDatabases(ctx context.Context, txn *kv.Txn) (nstree.Catalog, error) { + stored, err := tc.cr.ScanNamespaceForDatabases(ctx, txn) + if err != nil { + return nstree.Catalog{}, err + } + ret, err := tc.aggregateAllLayers(ctx, txn, stored) + if err != nil { + return nstree.Catalog{}, err + } + var dbIDs catalog.DescriptorIDSet + _ = ret.ForEachDescriptor(func(desc catalog.Descriptor) error { + if desc.DescriptorType() != catalog.Database { return nil } - if !strings.HasPrefix(e.GetName(), catconstants.PgTempSchemaName) && - e.GetID() != catconstants.PublicSchemaID { - ids.Add(e.GetID()) - } + dbIDs.Add(desc.GetID()) return nil }) - for _, iterator := range []func(func(catalog.Descriptor) error) error{ - tc.uncommitted.iterateUncommittedByID, - tc.synthetic.iterateSyntheticByID, - } { - _ = iterator(func(desc catalog.Descriptor) error { - if desc.GetParentID() == db.GetID() { - ids.Add(desc.GetID()) - } - return nil - }) + return ret.FilterByIDs(dbIDs.Ordered()), nil +} + +// GetAllSchemasInDatabase is like GetAll but filtered to the schemas with +// the specified parent database. Includes virtual schemas. +func (tc *Collection) GetAllSchemasInDatabase( + ctx context.Context, txn *kv.Txn, db catalog.DatabaseDescriptor, +) (nstree.Catalog, error) { + stored, err := tc.cr.ScanNamespaceForDatabaseSchemas(ctx, txn, db) + if err != nil { + return nstree.Catalog{}, err } - // Look up all non-function descriptors. - flags := tree.CommonLookupFlags{ - AvoidLeased: true, - IncludeOffline: true, - IncludeDropped: true, + var ret nstree.MutableCatalog + if db.HasPublicSchemaWithDescriptor() { + ret, err = tc.aggregateAllLayers(ctx, txn, stored) + } else { + ret, err = tc.aggregateAllLayers(ctx, txn, stored, schemadesc.GetPublicSchema()) } - descs, err := tc.getDescriptorsByID(ctx, txn, flags, ids.Ordered()...) if err != nil { return nstree.Catalog{}, err } - var ret nstree.MutableCatalog - var functionIDs catalog.DescriptorIDSet - for _, desc := range descs { - ret.UpsertDescriptorEntry(desc) - if sc, ok := desc.(catalog.SchemaDescriptor); ok { - _ = sc.ForEachFunctionOverload(func(overload descpb.SchemaDescriptor_FunctionOverload) error { - functionIDs.Add(overload.ID) + var schemaIDs catalog.DescriptorIDSet + _ = ret.ForEachDescriptor(func(desc catalog.Descriptor) error { + sc, ok := desc.(catalog.SchemaDescriptor) + if !ok { + return nil + } + switch sc.SchemaKind() { + case catalog.SchemaTemporary, catalog.SchemaUserDefined: + if sc.GetParentID() != db.GetID() { return nil - }) + } + } + schemaIDs.Add(desc.GetID()) + return nil + }) + return ret.FilterByIDs(schemaIDs.Ordered()), nil +} + +// GetAllObjectsInSchema is like GetAll but filtered to the objects with +// the specified parent schema. Includes virtual objects. Does not include +// dropped objects. +func (tc *Collection) GetAllObjectsInSchema( + ctx context.Context, txn *kv.Txn, db catalog.DatabaseDescriptor, sc catalog.SchemaDescriptor, +) (nstree.Catalog, error) { + var ret nstree.MutableCatalog + if sc.SchemaKind() == catalog.SchemaVirtual { + tc.virtual.addAllToCatalog(ret) + } else { + stored, err := tc.cr.ScanNamespaceForSchemaObjects(ctx, txn, db, sc) + if err != nil { + return nstree.Catalog{}, err + } + ret, err = tc.aggregateAllLayers(ctx, txn, stored, sc) + if err != nil { + return nstree.Catalog{}, err } } - // Look up all function descriptors. - descs, err = tc.getDescriptorsByID(ctx, txn, flags, functionIDs.Ordered()...) + var objectIDs catalog.DescriptorIDSet + _ = ret.ForEachDescriptor(func(desc catalog.Descriptor) error { + if desc.GetParentSchemaID() == sc.GetID() { + objectIDs.Add(desc.GetID()) + } + return nil + }) + return ret.FilterByIDs(objectIDs.Ordered()), nil +} + +// GetAllInDatabase is like the union of GetAllSchemasInDatabase and +// GetAllObjectsInSchema applied to each of those schemas. +func (tc *Collection) GetAllInDatabase( + ctx context.Context, txn *kv.Txn, db catalog.DatabaseDescriptor, +) (nstree.Catalog, error) { + stored, err := tc.cr.ScanNamespaceForDatabaseSchemasAndObjects(ctx, txn, db) if err != nil { return nstree.Catalog{}, err } - for _, desc := range descs { - ret.UpsertDescriptorEntry(desc) + schemas, err := tc.GetAllSchemasInDatabase(ctx, txn, db) + if err != nil { + return nstree.Catalog{}, err } - return ret.Catalog, nil + var schemasSlice []catalog.SchemaDescriptor + if err := schemas.ForEachDescriptor(func(desc catalog.Descriptor) error { + sc, err := catalog.AsSchemaDescriptor(desc) + schemasSlice = append(schemasSlice, sc) + return err + }); err != nil { + return nstree.Catalog{}, err + } + ret, err := tc.aggregateAllLayers(ctx, txn, stored, schemasSlice...) + if err != nil { + return nstree.Catalog{}, err + } + var inDatabaseIDs catalog.DescriptorIDSet + _ = ret.ForEachDescriptor(func(desc catalog.Descriptor) error { + if desc.DescriptorType() == catalog.Schema { + if dbID := desc.GetParentID(); dbID != descpb.InvalidID && dbID != db.GetID() { + return nil + } + } else { + if schemas.LookupDescriptor(desc.GetParentSchemaID()) == nil { + return nil + } + } + inDatabaseIDs.Add(desc.GetID()) + return nil + }) + return ret.FilterByIDs(inDatabaseIDs.Ordered()), nil } -// GetAllDescriptors returns all descriptors visible by the transaction, -func (tc *Collection) GetAllDescriptors(ctx context.Context, txn *kv.Txn) (nstree.Catalog, error) { - read, err := tc.cr.ScanAll(ctx, txn) +// GetAllTablesInDatabase is like GetAllInDatabase but filtered to tables. +func (tc *Collection) GetAllTablesInDatabase( + ctx context.Context, txn *kv.Txn, db catalog.DatabaseDescriptor, +) (nstree.Catalog, error) { + stored, err := tc.cr.ScanNamespaceForDatabaseSchemasAndObjects(ctx, txn, db) if err != nil { return nstree.Catalog{}, err } - var ids catalog.DescriptorIDSet - _ = read.ForEachDescriptorEntry(func(desc catalog.Descriptor) error { - ids.Add(desc.GetID()) + var ret nstree.MutableCatalog + if db.HasPublicSchemaWithDescriptor() { + ret, err = tc.aggregateAllLayers(ctx, txn, stored) + } else { + ret, err = tc.aggregateAllLayers(ctx, txn, stored, schemadesc.GetPublicSchema()) + } + if err != nil { + return nstree.Catalog{}, err + } + var inDatabaseIDs catalog.DescriptorIDSet + _ = ret.ForEachDescriptor(func(desc catalog.Descriptor) error { + if desc.DescriptorType() != catalog.Table { + return nil + } + if dbID := desc.GetParentID(); dbID != descpb.InvalidID && dbID != db.GetID() { + return nil + } + inDatabaseIDs.Add(desc.GetID()) + return nil + }) + return ret.FilterByIDs(inDatabaseIDs.Ordered()), nil +} + +// aggregateAllLayers is the helper function used by GetAll* methods which +// takes care to stack all of the Collection's layer appropriately and ensures +// that the returned descriptors are properly hydrated and validated. +func (tc *Collection) aggregateAllLayers( + ctx context.Context, txn *kv.Txn, stored nstree.Catalog, schemas ...catalog.SchemaDescriptor, +) (ret nstree.MutableCatalog, _ error) { + // Descriptors need to be re-read to ensure proper validation hydration etc. + // We collect their IDs for this purpose and we'll re-add them later. + var descIDs catalog.DescriptorIDSet + // Start with the known function descriptor IDs. + for _, sc := range schemas { + if sc.SchemaKind() == catalog.SchemaPublic { + // This is needed at least for the temp system db during restores. + descIDs.Add(sc.GetID()) + } + _ = sc.ForEachFunctionOverload(func(overload descpb.SchemaDescriptor_FunctionOverload) error { + descIDs.Add(overload.ID) + return nil + }) + } + // Add IDs from descriptors retrieved from the storage layer. + _ = stored.ForEachDescriptor(func(desc catalog.Descriptor) error { + descIDs.Add(desc.GetID()) + if sc, ok := desc.(catalog.SchemaDescriptor); ok { + _ = sc.ForEachFunctionOverload(func(overload descpb.SchemaDescriptor_FunctionOverload) error { + descIDs.Add(overload.ID) + return nil + }) + } return nil }) + // Add stored namespace entries which are not shadowed. + _ = stored.ForEachNamespaceEntry(func(e nstree.NamespaceEntry) error { + if tc.isShadowedName(e) { + return nil + } + // Temporary schemas don't have descriptors and are persisted only + // as namespace table entries. + if e.GetParentID() != descpb.InvalidID && e.GetParentSchemaID() == descpb.InvalidID && + strings.HasPrefix(e.GetName(), catconstants.PgTempSchemaName) { + ret.UpsertDescriptor(schemadesc.NewTemporarySchema(e.GetName(), e.GetID(), e.GetParentID())) + } else { + descIDs.Add(e.GetID()) + } + ret.UpsertNamespaceEntry(e, e.GetID(), e.GetMVCCTimestamp()) + return nil + }) + // Add stored comments which are not shadowed. + _ = stored.ForEachComment(func(key catalogkeys.CommentKey, cmt string) error { + if _, _, isShadowed := tc.uncommittedComments.getUncommitted(key); !isShadowed { + ret.UpsertComment(key, cmt) + } + return nil + }) + // Add stored zone configs which are not shadowed. + _ = stored.ForEachZoneConfig(func(id descpb.ID, zc catalog.ZoneConfig) error { + if _, isShadowed := tc.uncommittedZoneConfigs.getUncommitted(id); !isShadowed { + ret.UpsertZoneConfig(id, zc.ZoneConfigProto(), zc.GetRawBytesInStorage()) + } + return nil + }) + // Add uncommitted and synthetic namespace entries from descriptors, + // collect descriptor IDs to re-read. for _, iterator := range []func(func(desc catalog.Descriptor) error) error{ tc.uncommitted.iterateUncommittedByID, tc.synthetic.iterateSyntheticByID, - // TODO(postamar): include temporary descriptors? } { _ = iterator(func(desc catalog.Descriptor) error { - ids.Add(desc.GetID()) + descIDs.Add(desc.GetID()) + if sc, ok := desc.(catalog.SchemaDescriptor); ok { + _ = sc.ForEachFunctionOverload(func(overload descpb.SchemaDescriptor_FunctionOverload) error { + descIDs.Add(overload.ID) + return nil + }) + } + if !desc.Dropped() && !desc.SkipNamespace() { + ret.UpsertNamespaceEntry(desc, desc.GetID(), desc.GetModificationTime()) + } return nil }) } + // Add in-memory temporary schema IDs. + if tc.temporarySchemaProvider.HasTemporarySchema() { + tempSchemaName := tc.temporarySchemaProvider.GetTemporarySchemaName() + descIDs.ForEach(func(maybeDatabaseID descpb.ID) { + schemaID := tc.temporarySchemaProvider.GetTemporarySchemaIDForDB(maybeDatabaseID) + if schemaID == descpb.InvalidID { + return + } + ret.UpsertDescriptor(schemadesc.NewTemporarySchema(tempSchemaName, schemaID, maybeDatabaseID)) + }) + } + // Add uncommitted comments and zone configs. + tc.uncommittedComments.addAllToCatalog(ret) + tc.uncommittedZoneConfigs.addAllToCatalog(ret) + // Remove deleted descriptors from consideration, re-read and add the rest. + tc.deletedDescs.ForEach(descIDs.Remove) flags := tree.CommonLookupFlags{ AvoidLeased: true, IncludeOffline: true, IncludeDropped: true, } - // getDescriptorsByID must be used to ensure proper validation hydration etc. - descs, err := tc.getDescriptorsByID(ctx, txn, flags, ids.Ordered()...) + allDescs, err := tc.getDescriptorsByID(ctx, txn, flags, descIDs.Ordered()...) if err != nil { - return nstree.Catalog{}, err + return nstree.MutableCatalog{}, err } - var ret nstree.MutableCatalog - for _, desc := range descs { - ret.UpsertDescriptorEntry(desc) + for _, desc := range allDescs { + ret.UpsertDescriptor(desc) } - return ret.Catalog, nil + // Add the virtual catalog. + tc.virtual.addAllToCatalog(ret) + return ret, nil } -// GetAllDatabaseDescriptors returns all database descriptors visible by the -// transaction, ordered by name. -func (tc *Collection) GetAllDatabaseDescriptors( - ctx context.Context, txn *kv.Txn, -) ([]catalog.DatabaseDescriptor, error) { - - read, err := tc.cr.ScanNamespaceForDatabases(ctx, txn) +// GetAllDescriptorsForDatabase retrieves the complete set of descriptors +// in the requested database. +// Deprecated: prefer GetAllInDatabase. +func (tc *Collection) GetAllDescriptorsForDatabase( + ctx context.Context, txn *kv.Txn, db catalog.DatabaseDescriptor, +) (nstree.Catalog, error) { + // Re-read database descriptor to have the freshest version. + flags := tree.CommonLookupFlags{ + AvoidLeased: true, + IncludeDropped: true, + IncludeOffline: true, + } + var err error + _, db, err = tc.GetImmutableDatabaseByID(ctx, txn, db.GetID(), flags) if err != nil { - return nil, err + return nstree.Catalog{}, err } - var readIDs catalog.DescriptorIDSet - _ = read.ForEachDatabaseNamespaceEntry(func(e nstree.NamespaceEntry) error { - if !tc.isShadowedName(e) { - readIDs.Add(e.GetID()) - } - return nil - }) - const isDescriptorRequired = true - read, err = tc.cr.GetByIDs(ctx, txn, readIDs.Ordered(), isDescriptorRequired, catalog.Database) + c, err := tc.GetAllInDatabase(ctx, txn, db) if err != nil { - return nil, err + return nstree.Catalog{}, err } - var m nstree.NameMap - _ = read.ForEachDescriptorEntry(func(desc catalog.Descriptor) error { - m.Upsert(desc, desc.SkipNamespace()) - return nil - }) - _ = tc.uncommitted.iterateUncommittedByID(func(desc catalog.Descriptor) error { - if desc.DescriptorType() == catalog.Database { - m.Upsert(desc, desc.SkipNamespace()) - } + var ret nstree.MutableCatalog + ret.UpsertDescriptor(db) + _ = c.ForEachDescriptor(func(desc catalog.Descriptor) error { + ret.UpsertDescriptor(desc) return nil }) - _ = tc.synthetic.iterateSyntheticByID(func(desc catalog.Descriptor) error { - if desc.DescriptorType() == catalog.Database { - m.Upsert(desc, desc.SkipNamespace()) + return ret.Catalog, nil +} + +// GetAllDescriptors returns all physical descriptors visible by the +// transaction. +// Deprecated: prefer GetAll. +func (tc *Collection) GetAllDescriptors(ctx context.Context, txn *kv.Txn) (nstree.Catalog, error) { + all, err := tc.GetAll(ctx, txn) + if err != nil { + return nstree.Catalog{}, err + } + var ret nstree.MutableCatalog + _ = all.ForEachDescriptor(func(desc catalog.Descriptor) error { + switch d := desc.(type) { + case catalog.SchemaDescriptor: + switch d.SchemaKind() { + case catalog.SchemaPublic, catalog.SchemaVirtual, catalog.SchemaTemporary: + return nil + } + case catalog.TableDescriptor: + if d.IsVirtualTable() { + return nil + } } + ret.UpsertDescriptor(desc) return nil }) - var ids catalog.DescriptorIDSet - _ = m.IterateDatabasesByName(func(entry catalog.NameEntry) error { - ids.Add(entry.GetID()) - return nil - }) - flags := tree.CommonLookupFlags{ - AvoidLeased: true, - IncludeOffline: true, - IncludeDropped: true, - } - // getDescriptorsByID must be used to ensure proper validation hydration etc. - descs, err := tc.getDescriptorsByID(ctx, txn, flags, ids.Ordered()...) + return ret.Catalog, nil +} + +// GetAllDatabaseDescriptors returns all database descriptors visible by the +// transaction, ordered by name. +// Deprecated: prefer GetAllDatabases. +func (tc *Collection) GetAllDatabaseDescriptors( + ctx context.Context, txn *kv.Txn, +) (ret []catalog.DatabaseDescriptor, _ error) { + c, err := tc.GetAllDatabases(ctx, txn) if err != nil { return nil, err } // Returned slice must be ordered by name. - m.Clear() - dbDescs := make([]catalog.DatabaseDescriptor, 0, len(descs)) - for _, desc := range descs { - m.Upsert(desc, desc.SkipNamespace()) + if err := c.ForEachDatabaseNamespaceEntry(func(e nstree.NamespaceEntry) error { + desc := c.LookupDescriptor(e.GetID()) + db, err := catalog.AsDatabaseDescriptor(desc) + ret = append(ret, db) + return err + }); err != nil { + return nil, err } - _ = m.IterateDatabasesByName(func(entry catalog.NameEntry) error { - dbDescs = append(dbDescs, entry.(catalog.DatabaseDescriptor)) - return nil - }) - return dbDescs, nil + return ret, nil } // GetAllTableDescriptorsInDatabase returns all the table descriptors visible to // the transaction under the database with the given ID. +// Deprecated: prefer GetAllTablesInDatabase. func (tc *Collection) GetAllTableDescriptorsInDatabase( ctx context.Context, txn *kv.Txn, db catalog.DatabaseDescriptor, -) ([]catalog.TableDescriptor, error) { - all, err := tc.GetAllDescriptors(ctx, txn) +) (ret []catalog.TableDescriptor, _ error) { + c, err := tc.GetAllTablesInDatabase(ctx, txn, db) if err != nil { return nil, err } - // Ensure the given ID does indeed belong to a database. - if desc := all.LookupDescriptorEntry(db.GetID()); desc == nil || desc.DescriptorType() != catalog.Database { - return nil, sqlerrors.NewUndefinedDatabaseError(db.GetName()) - } - dbID := db.GetID() - var ret []catalog.TableDescriptor - _ = all.ForEachDescriptorEntry(func(desc catalog.Descriptor) error { - if desc.GetParentID() != dbID { + if err := c.ForEachDescriptor(func(desc catalog.Descriptor) error { + if desc.GetParentID() != db.GetID() { return nil } - if table, ok := desc.(catalog.TableDescriptor); ok { - ret = append(ret, table) - } - return nil - }) + tbl, err := catalog.AsTableDescriptor(desc) + ret = append(ret, tbl) + return err + }); err != nil { + return nil, err + } return ret, nil } // GetSchemasForDatabase returns the schemas for a given database // visible by the transaction. +// Deprecated: prefer GetAllSchemasInDatabase. func (tc *Collection) GetSchemasForDatabase( ctx context.Context, txn *kv.Txn, db catalog.DatabaseDescriptor, ) (map[descpb.ID]string, error) { - read, err := tc.cr.ScanNamespaceForDatabaseSchemas(ctx, txn, db) + c, err := tc.GetAllSchemasInDatabase(ctx, txn, db) if err != nil { return nil, err } ret := make(map[descpb.ID]string) - // This is needed at least for the temp system db during restores. - if !db.HasPublicSchemaWithDescriptor() { - ret[keys.PublicSchemaIDForBackup] = catconstants.PublicSchemaName - } - _ = read.ForEachSchemaNamespaceEntryInDatabase(db.GetID(), func(e nstree.NamespaceEntry) error { - if !tc.isShadowedName(e) { - ret[e.GetID()] = e.GetName() - } - return nil - }) - _ = tc.uncommitted.iterateUncommittedByID(func(desc catalog.Descriptor) error { - if desc.DescriptorType() == catalog.Schema && desc.GetParentID() == db.GetID() { - ret[desc.GetID()] = desc.GetName() + if err := c.ForEachDescriptor(func(desc catalog.Descriptor) error { + sc, err := catalog.AsSchemaDescriptor(desc) + if err != nil { + return err } - return nil - }) - _ = tc.synthetic.iterateSyntheticByID(func(desc catalog.Descriptor) error { - if desc.DescriptorType() == catalog.Schema && desc.GetParentID() == db.GetID() { + if sc.SchemaKind() != catalog.SchemaVirtual { ret[desc.GetID()] = desc.GetName() } return nil - }) + }); err != nil { + return nil, err + } return ret, nil } // GetObjectNamesAndIDs returns the names and IDs of all objects in a schema. +// Deprecated: prefer GetAllObjectsInSchema. func (tc *Collection) GetObjectNamesAndIDs( ctx context.Context, txn *kv.Txn, db catalog.DatabaseDescriptor, sc catalog.SchemaDescriptor, ) (nstree.Catalog, error) { - var mc nstree.MutableCatalog - if sc.SchemaKind() == catalog.SchemaVirtual { - tc.virtual.forEachVirtualObject(sc, func(obj catalog.Descriptor) { - mc.UpsertNamespaceEntry(obj, obj.GetID(), hlc.Timestamp{}) - }) - return mc.Catalog, nil - } - read, err := tc.cr.ScanNamespaceForSchemaObjects(ctx, txn, db, sc) + c, err := tc.GetAllObjectsInSchema(ctx, txn, db, sc) if err != nil { return nstree.Catalog{}, err } - _ = read.ForEachNamespaceEntry(func(e nstree.NamespaceEntry) error { - if !tc.isShadowedName(e) { - mc.UpsertNamespaceEntry(e, e.GetID(), e.GetMVCCTimestamp()) + var ret nstree.MutableCatalog + _ = c.ForEachDescriptor(func(desc catalog.Descriptor) error { + if !desc.Dropped() { + ret.UpsertNamespaceEntry(desc, desc.GetID(), desc.GetModificationTime()) } return nil }) - for _, iterator := range []func(func(catalog.Descriptor) error) error{ - tc.uncommitted.iterateUncommittedByID, - tc.synthetic.iterateSyntheticByID, - } { - _ = iterator(func(desc catalog.Descriptor) error { - if desc.GetParentID() != db.GetID() || desc.GetParentSchemaID() != sc.GetID() { - return nil - } - if desc.Dropped() || desc.SkipNamespace() { - return nil - } - mc.UpsertNamespaceEntry(desc, desc.GetID(), desc.GetModificationTime()) - return nil - }) - } - return mc.Catalog, nil + return ret.Catalog, nil } // SetSyntheticDescriptors sets the provided descriptors as the synthetic diff --git a/pkg/sql/catalog/descs/collection_test.go b/pkg/sql/catalog/descs/collection_test.go index 72a1b940b090..58b8022b486a 100644 --- a/pkg/sql/catalog/descs/collection_test.go +++ b/pkg/sql/catalog/descs/collection_test.go @@ -727,7 +727,7 @@ func TestDescriptorCache(t *testing.T) { if err != nil { return err } - found := cat.LookupDescriptorEntry(mut.ID) + found := cat.LookupDescriptor(mut.ID) require.NotEmpty(t, found) require.Equal(t, mut.ImmutableCopy().DescriptorProto(), found.DescriptorProto()) return nil @@ -898,6 +898,10 @@ func formatCatalog(descs []catalog.Descriptor) string { "parent\tschema\tname\tid\tkind\tversion\tdropped\tpublic\n", ) for _, d := range descs { + if d.DescriptorType() != catalog.Database && d.GetParentID() == descpb.InvalidID { + // Skip virtual schemas and virtual tables. + continue + } _, _ = fmt.Fprintf(tr, "%d\t%d\t%s\t%d\t%s\t%d\t%v\t%v\n", d.GetParentID(), d.GetParentSchemaID(), d.GetName(), @@ -999,21 +1003,21 @@ func TestHydrateCatalog(t *testing.T) { deleteDescriptor := func(name string) catalogTamperFn { return func(cat nstree.Catalog) nstree.Catalog { var descToDelete catid.DescID - _ = cat.ForEachDescriptorEntry(func(desc catalog.Descriptor) error { + _ = cat.ForEachDescriptor(func(desc catalog.Descriptor) error { if desc.GetName() == name { descToDelete = desc.GetID() } return nil }) mutCat := nstree.MutableCatalog{Catalog: cat} - mutCat.DeleteDescriptorEntry(descToDelete) + mutCat.DeleteByID(descToDelete) return mutCat.Catalog } } replaceTypeDescWithNonTypeDesc := func(name string) catalogTamperFn { return func(cat nstree.Catalog) nstree.Catalog { var typeDescID catid.DescID - _ = cat.ForEachDescriptorEntry(func(desc catalog.Descriptor) error { + _ = cat.ForEachDescriptor(func(desc catalog.Descriptor) error { if desc.GetName() == name { typeDescID = desc.GetID() } @@ -1022,7 +1026,7 @@ func TestHydrateCatalog(t *testing.T) { // Make a dummy database descriptor to replace the type descriptor. dbDesc := dbdesc.NewBuilder(&descpb.DatabaseDescriptor{ID: typeDescID}).BuildImmutable() mutCat := nstree.MutableCatalog{Catalog: cat} - mutCat.UpsertDescriptorEntry(dbDesc) + mutCat.UpsertDescriptor(dbDesc) return mutCat.Catalog } } @@ -1064,7 +1068,7 @@ func TestHydrateCatalog(t *testing.T) { mc := nstree.MutableCatalog{Catalog: cat} require.NoError(t, descs.HydrateCatalog(ctx, mc)) tbl := desctestutils.TestingGetTableDescriptor(txn.DB(), keys.SystemSQLCodec, "db", "schema", "table") - tblDesc := cat.LookupDescriptorEntry(tbl.GetID()).(catalog.TableDescriptor) + tblDesc := cat.LookupDescriptor(tbl.GetID()).(catalog.TableDescriptor) expectedEnum := types.UserDefinedTypeMetadata{ Name: &types.UserDefinedTypeName{ Catalog: "db", @@ -1152,7 +1156,7 @@ SELECT id if err != nil { return err } - require.Equal(t, tabImm, all.LookupDescriptorEntry(tabImm.GetID())) + require.Equal(t, tabImm, all.LookupDescriptor(tabImm.GetID())) return nil } diff --git a/pkg/sql/catalog/descs/descriptor.go b/pkg/sql/catalog/descs/descriptor.go index 6732a68096c3..fd42c26bc777 100644 --- a/pkg/sql/catalog/descs/descriptor.go +++ b/pkg/sql/catalog/descs/descriptor.go @@ -112,7 +112,7 @@ func (tc *Collection) GetComment(key catalogkeys.CommentKey) (string, bool) { return cmt, hasCmt } if tc.cr.IsIDInCache(descpb.ID(key.ObjectID)) { - return tc.cr.Cache().LookupCommentEntry(key) + return tc.cr.Cache().LookupComment(key) } // TODO(chengxiong): we need to ensure descriptor if it's not in either cache // and it's not a pseudo descriptor. @@ -162,7 +162,7 @@ func (tc *Collection) GetZoneConfigs( if err != nil { return nil, err } - _ = read.ForEachZoneConfigEntry(func(id descpb.ID, zc catalog.ZoneConfig) error { + _ = read.ForEachZoneConfig(func(id descpb.ID, zc catalog.ZoneConfig) error { ret[id] = zc.Clone() return nil }) @@ -329,7 +329,7 @@ func getDescriptorsByID( } for i, id := range ids { if descs[i] == nil { - descs[i] = read.LookupDescriptorEntry(id) + descs[i] = read.LookupDescriptor(id) vls[i] = tc.validationLevels[id] } } @@ -412,7 +412,7 @@ func (q *byIDLookupContext) lookupCached( id descpb.ID, ) (catalog.Descriptor, catalog.ValidationLevel, error) { if q.tc.cr.IsIDInCache(id) { - if desc := q.tc.cr.Cache().LookupDescriptorEntry(id); desc != nil { + if desc := q.tc.cr.Cache().LookupDescriptor(id); desc != nil { return desc, q.tc.validationLevels[id], nil } } diff --git a/pkg/sql/catalog/descs/hydrate.go b/pkg/sql/catalog/descs/hydrate.go index e8b72190994d..1fd4e9617f4f 100644 --- a/pkg/sql/catalog/descs/hydrate.go +++ b/pkg/sql/catalog/descs/hydrate.go @@ -118,7 +118,7 @@ func makeMutableTypeLookupFunc( if _, ok := desc.(catalog.MutableDescriptor); !ok { continue } - mut.UpsertDescriptorEntry(desc) + mut.UpsertDescriptor(desc) } mutableLookupFunc := func(ctx context.Context, id descpb.ID) (catalog.Descriptor, error) { // This special case exists to deal with the desire to use enums in the @@ -150,7 +150,7 @@ func makeImmutableTypeLookupFunc( if _, ok := desc.(catalog.MutableDescriptor); ok { continue } - imm.UpsertDescriptorEntry(desc) + imm.UpsertDescriptor(desc) } immutableLookupFunc := func(ctx context.Context, id descpb.ID) (catalog.Descriptor, error) { return tc.GetImmutableDescriptorByID(ctx, txn, id, tree.CommonLookupFlags{ @@ -173,7 +173,7 @@ func HydrateCatalog(ctx context.Context, c nstree.MutableCatalog) error { return nil, catalog.WrapDescRefErr(id, catalog.ErrDescriptorNotFound) } typeLookupFunc := hydrateddesc.MakeTypeLookupFuncForHydration(c.Catalog, fakeLookupFunc) - return c.ForEachDescriptorEntry(func(desc catalog.Descriptor) error { + return c.ForEachDescriptor(func(desc catalog.Descriptor) error { if !hydrateddesc.IsHydratable(desc) { return nil } @@ -182,7 +182,7 @@ func HydrateCatalog(ctx context.Context, c nstree.MutableCatalog) error { } // Deep-copy the immutable descriptor and overwrite the catalog entry. desc = desc.NewBuilder().BuildImmutable() - defer c.UpsertDescriptorEntry(desc) + defer c.UpsertDescriptor(desc) return hydrateddesc.Hydrate(ctx, desc, typeLookupFunc) }) } diff --git a/pkg/sql/catalog/descs/uncommitted_metadata.go b/pkg/sql/catalog/descs/uncommitted_metadata.go index 4d9285aca915..24c7914372ac 100644 --- a/pkg/sql/catalog/descs/uncommitted_metadata.go +++ b/pkg/sql/catalog/descs/uncommitted_metadata.go @@ -16,6 +16,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/catalogkeys" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/nstree" "github.com/cockroachdb/cockroach/pkg/sql/catalog/zone" ) @@ -70,6 +71,12 @@ func (uc *uncommittedComments) upsert(key catalogkeys.CommentKey, cmt string) { uc.uncommitted[key] = cmt } +func (uc *uncommittedComments) addAllToCatalog(mc nstree.MutableCatalog) { + for ck, cmt := range uc.uncommitted { + mc.UpsertComment(ck, cmt) + } +} + type uncommittedZoneConfigs struct { uncommitted map[descpb.ID]catalog.ZoneConfig cachedDescs map[descpb.ID]struct{} @@ -114,3 +121,9 @@ func (uc *uncommittedZoneConfigs) upsert(id descpb.ID, zc *zonepb.ZoneConfig) er uc.uncommitted[id] = zone.NewZoneConfigWithRawBytes(zc, rawBytes) return nil } + +func (uc *uncommittedZoneConfigs) addAllToCatalog(mc nstree.MutableCatalog) { + for id, zc := range uc.uncommitted { + mc.UpsertZoneConfig(id, zc.ZoneConfigProto(), zc.GetRawBytesInStorage()) + } +} diff --git a/pkg/sql/catalog/descs/virtual_descriptors.go b/pkg/sql/catalog/descs/virtual_descriptors.go index a5a216dfccc9..baabee69fc95 100644 --- a/pkg/sql/catalog/descs/virtual_descriptors.go +++ b/pkg/sql/catalog/descs/virtual_descriptors.go @@ -13,9 +13,13 @@ package descs import ( "context" + "github.com/cockroachdb/cockroach/pkg/keys" "github.com/cockroachdb/cockroach/pkg/sql/catalog" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catalogkeys" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/nstree" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" + "github.com/cockroachdb/cockroach/pkg/util/hlc" "github.com/cockroachdb/errors" ) @@ -27,7 +31,7 @@ func makeVirtualDescriptors(schemas catalog.VirtualSchemas) virtualDescriptors { return virtualDescriptors{vs: schemas} } -func (tc *virtualDescriptors) getSchemaByName(schemaName string) catalog.SchemaDescriptor { +func (tc virtualDescriptors) getSchemaByName(schemaName string) catalog.SchemaDescriptor { if tc.vs == nil { return nil } @@ -37,7 +41,7 @@ func (tc *virtualDescriptors) getSchemaByName(schemaName string) catalog.SchemaD return nil } -func (tc *virtualDescriptors) getObjectByName( +func (tc virtualDescriptors) getObjectByName( schema string, object string, flags tree.ObjectLookupFlags, ) (isVirtual bool, _ catalog.Descriptor, _ error) { if tc.vs == nil { @@ -99,14 +103,27 @@ func (tc virtualDescriptors) getSchemaByID( } } -func (tc virtualDescriptors) forEachVirtualObject( - sc catalog.SchemaDescriptor, fn func(obj catalog.Descriptor), -) { - scEntry, ok := tc.vs.GetVirtualSchemaByID(sc.GetID()) - if !ok { - return - } - scEntry.VisitTables(func(object catalog.VirtualObject) { - fn(object.Desc()) +func (tc virtualDescriptors) addAllToCatalog(mc nstree.MutableCatalog) { + _ = tc.vs.Visit(func(vd catalog.Descriptor, comment string) error { + mc.UpsertDescriptor(vd) + if vd.GetID() != keys.PublicSchemaID && !vd.Dropped() && !vd.SkipNamespace() { + mc.UpsertNamespaceEntry(vd, vd.GetID(), hlc.Timestamp{}) + } + if comment == "" { + return nil + } + ck := catalogkeys.CommentKey{ObjectID: uint32(vd.GetID())} + switch vd.DescriptorType() { + case catalog.Database: + ck.CommentType = catalogkeys.DatabaseCommentType + case catalog.Schema: + ck.CommentType = catalogkeys.SchemaCommentType + case catalog.Table: + ck.CommentType = catalogkeys.TableCommentType + default: + return nil + } + mc.UpsertComment(ck, comment) + return nil }) } diff --git a/pkg/sql/catalog/funcdesc/func_desc_test.go b/pkg/sql/catalog/funcdesc/func_desc_test.go index 6811c3650ab9..685467d03985 100644 --- a/pkg/sql/catalog/funcdesc/func_desc_test.go +++ b/pkg/sql/catalog/funcdesc/func_desc_test.go @@ -55,24 +55,24 @@ func TestValidateFuncDesc(t *testing.T) { funcDescID := descpb.ID(bootstrap.TestingUserDescID(0)) var cb nstree.MutableCatalog - cb.UpsertDescriptorEntry(dbdesc.NewBuilder(&descpb.DatabaseDescriptor{ + cb.UpsertDescriptor(dbdesc.NewBuilder(&descpb.DatabaseDescriptor{ Name: "db", ID: dbID, }).BuildImmutable()) - cb.UpsertDescriptorEntry(schemadesc.NewBuilder(&descpb.SchemaDescriptor{ + cb.UpsertDescriptor(schemadesc.NewBuilder(&descpb.SchemaDescriptor{ ID: schemaID, ParentID: dbID, Name: "schema", }).BuildImmutable()) - cb.UpsertDescriptorEntry(typedesc.NewBuilder(&descpb.TypeDescriptor{ + cb.UpsertDescriptor(typedesc.NewBuilder(&descpb.TypeDescriptor{ ID: typeID, Name: "type", }).BuildImmutable()) - cb.UpsertDescriptorEntry(tabledesc.NewBuilder(&descpb.TableDescriptor{ + cb.UpsertDescriptor(tabledesc.NewBuilder(&descpb.TableDescriptor{ ID: tableID, Name: "tbl", }).BuildImmutable()) - cb.UpsertDescriptorEntry(schemadesc.NewBuilder(&descpb.SchemaDescriptor{ + cb.UpsertDescriptor(schemadesc.NewBuilder(&descpb.SchemaDescriptor{ ID: schemaWithFuncRefID, ParentID: dbID, Name: "schema", @@ -80,17 +80,17 @@ func TestValidateFuncDesc(t *testing.T) { "f": {Overloads: []descpb.SchemaDescriptor_FunctionOverload{{ID: funcDescID}}}, }, }).BuildImmutable()) - cb.UpsertDescriptorEntry(typedesc.NewBuilder(&descpb.TypeDescriptor{ + cb.UpsertDescriptor(typedesc.NewBuilder(&descpb.TypeDescriptor{ ID: typeWithFuncRefID, Name: "type", ReferencingDescriptorIDs: []descpb.ID{funcDescID}, }).BuildImmutable()) - cb.UpsertDescriptorEntry(tabledesc.NewBuilder(&descpb.TableDescriptor{ + cb.UpsertDescriptor(tabledesc.NewBuilder(&descpb.TableDescriptor{ ID: tableWithFuncBackRefID, Name: "tbl", DependedOnBy: []descpb.TableDescriptor_Reference{{ID: funcDescID}}, }).BuildImmutable()) - cb.UpsertDescriptorEntry(tabledesc.NewBuilder(&descpb.TableDescriptor{ + cb.UpsertDescriptor(tabledesc.NewBuilder(&descpb.TableDescriptor{ ID: tableWithFuncForwardRefID, Name: "tbl", DependsOn: []descpb.ID{funcDescID}, diff --git a/pkg/sql/catalog/hydrateddesc/hydrate.go b/pkg/sql/catalog/hydrateddesc/hydrate.go index 64be5bcc323e..fed03d27fb2c 100644 --- a/pkg/sql/catalog/hydrateddesc/hydrate.go +++ b/pkg/sql/catalog/hydrateddesc/hydrate.go @@ -67,7 +67,7 @@ func MakeTypeLookupFuncForHydration( c nstree.Catalog, lookupFn HydrationLookupFunc, ) typedesc.TypeLookupFunc { return func(ctx context.Context, id descpb.ID) (tn tree.TypeName, typ catalog.TypeDescriptor, err error) { - typDesc := c.LookupDescriptorEntry(id) + typDesc := c.LookupDescriptor(id) if typDesc == nil { typDesc, err = lookupFn(ctx, id) if err != nil { @@ -89,7 +89,7 @@ func MakeTypeLookupFuncForHydration( if err != nil { return tree.TypeName{}, nil, err } - dbDesc := c.LookupDescriptorEntry(typ.GetParentID()) + dbDesc := c.LookupDescriptor(typ.GetParentID()) if dbDesc == nil { dbDesc, err = lookupFn(ctx, typ.GetParentID()) if err != nil { @@ -103,7 +103,7 @@ func MakeTypeLookupFuncForHydration( if _, err = catalog.AsDatabaseDescriptor(dbDesc); err != nil { return tree.TypeName{}, nil, err } - scDesc := c.LookupDescriptorEntry(typ.GetParentSchemaID()) + scDesc := c.LookupDescriptor(typ.GetParentSchemaID()) if scDesc == nil { scDesc, err = lookupFn(ctx, typ.GetParentSchemaID()) if err != nil { diff --git a/pkg/sql/catalog/hydrateddesc/hydratedcache_test.go b/pkg/sql/catalog/hydrateddesc/hydratedcache_test.go index 19231cf51f69..75a99b68fae8 100644 --- a/pkg/sql/catalog/hydrateddesc/hydratedcache_test.go +++ b/pkg/sql/catalog/hydrateddesc/hydratedcache_test.go @@ -77,10 +77,10 @@ func TestHydratedCache(t *testing.T) { assertMetrics(t, m, 0, 1, catalog.Table) // Change the database name. - dbDesc := dbdesc.NewBuilder(dg.LookupDescriptorEntry(dbID).(catalog.DatabaseDescriptor).DatabaseDesc()).BuildExistingMutableDatabase() + dbDesc := dbdesc.NewBuilder(dg.LookupDescriptor(dbID).(catalog.DatabaseDescriptor).DatabaseDesc()).BuildExistingMutableDatabase() dbDesc.SetName("new_name") dbDesc.Version++ - dg.UpsertDescriptorEntry(dbDesc.ImmutableCopy()) + dg.UpsertDescriptor(dbDesc.ImmutableCopy()) // Ensure that we observe a new descriptor get created due to // the name change. @@ -136,9 +136,9 @@ func TestHydratedCache(t *testing.T) { assertMetrics(t, m, 0, 1, catalog.Table) // Change the type descriptor. - typDesc := typedesc.NewBuilder(dg.LookupDescriptorEntry(typ1ID).(catalog.TypeDescriptor).TypeDesc()).BuildExistingMutableType() + typDesc := typedesc.NewBuilder(dg.LookupDescriptor(typ1ID).(catalog.TypeDescriptor).TypeDesc()).BuildExistingMutableType() typDesc.Version++ - dg.UpsertDescriptorEntry(typedesc.NewBuilder(typDesc.TypeDesc()).BuildImmutable()) + dg.UpsertDescriptor(typedesc.NewBuilder(typDesc.TypeDesc()).BuildImmutable()) // Ensure that a new descriptor is returned. retrieved, err := c.GetHydratedTableDescriptor(ctx, td, res) @@ -180,7 +180,7 @@ func TestHydratedCache(t *testing.T) { }) t.Run("modified table gets rejected", func(t *testing.T) { c, _, dg, res := makeCache() - mut := tabledesc.NewBuilder(dg.LookupDescriptorEntry(tableUDTID).(catalog.TableDescriptor).TableDesc()).BuildExistingMutable() + mut := tabledesc.NewBuilder(dg.LookupDescriptor(tableUDTID).(catalog.TableDescriptor).TableDesc()).BuildExistingMutable() mut.MaybeIncrementVersion() td := mut.ImmutableCopy().(catalog.TableDescriptor) hydrated, err := c.GetHydratedTableDescriptor(ctx, td, res) @@ -190,7 +190,7 @@ func TestHydratedCache(t *testing.T) { t.Run("modified type does not get cached", func(t *testing.T) { c, m, dg, res := makeCache() - mut := typedesc.NewBuilder(dg.LookupDescriptorEntry(typ1ID).(catalog.TypeDescriptor).TypeDesc()).BuildExistingMutable() + mut := typedesc.NewBuilder(dg.LookupDescriptor(typ1ID).(catalog.TypeDescriptor).TypeDesc()).BuildExistingMutable() mut.MaybeIncrementVersion() dgWithMut := mkDescGetter(append(descs, mut)...) resWithMut := &descGetterTypeDescriptorResolver{dg: &dgWithMut} @@ -463,7 +463,7 @@ var ( func mkDescGetter(descs ...catalog.MutableDescriptor) (cb nstree.MutableCatalog) { for _, desc := range descs { - cb.UpsertDescriptorEntry(desc.ImmutableCopy()) + cb.UpsertDescriptor(desc.ImmutableCopy()) } return cb } @@ -484,15 +484,15 @@ func (d *descGetterTypeDescriptorResolver) GetTypeDescriptor( return tree.TypeName{}, nil, err } } - desc := d.dg.LookupDescriptorEntry(id) + desc := d.dg.LookupDescriptor(id) if d.unqualifiedName { return tree.MakeUnqualifiedTypeName(desc.GetName()), desc.(catalog.TypeDescriptor), nil } - dbDesc := d.dg.LookupDescriptorEntry(desc.GetParentID()) + dbDesc := d.dg.LookupDescriptor(desc.GetParentID()) // Assume we've got a user-defined schema. // TODO(ajwerner): Unify this with some other resolution logic. - scDesc := d.dg.LookupDescriptorEntry(desc.GetParentSchemaID()) + scDesc := d.dg.LookupDescriptor(desc.GetParentSchemaID()) name := tree.MakeQualifiedTypeName(dbDesc.GetName(), scDesc.GetName(), desc.GetName()) return name, desc.(catalog.TypeDescriptor), nil } diff --git a/pkg/sql/catalog/internal/catkv/catalog_query.go b/pkg/sql/catalog/internal/catkv/catalog_query.go index 1e57b7fd7e56..9a4efd0324e3 100644 --- a/pkg/sql/catalog/internal/catkv/catalog_query.go +++ b/pkg/sql/catalog/internal/catkv/catalog_query.go @@ -115,7 +115,7 @@ func (cq catalogQuery) processDescriptorResultRow( if err != nil { return wrapError(expectedType, id, err) } - cb.UpsertDescriptorEntry(desc) + cb.UpsertDescriptor(desc) return nil } diff --git a/pkg/sql/catalog/internal/catkv/catalog_reader.go b/pkg/sql/catalog/internal/catkv/catalog_reader.go index 12f5d81f63df..ae6f53a52522 100644 --- a/pkg/sql/catalog/internal/catkv/catalog_reader.go +++ b/pkg/sql/catalog/internal/catkv/catalog_reader.go @@ -242,7 +242,7 @@ func (cr catalogReader) GetByIDs( } if isDescriptorRequired { for _, id := range ids { - if mc.LookupDescriptorEntry(id) == nil { + if mc.LookupDescriptor(id) == nil { return nstree.Catalog{}, wrapError(expectedType, id, requiredError(expectedType, id)) } } diff --git a/pkg/sql/catalog/internal/catkv/catalog_reader_cached.go b/pkg/sql/catalog/internal/catkv/catalog_reader_cached.go index 61ae3b50fe34..0f4e0e2b0d4b 100644 --- a/pkg/sql/catalog/internal/catkv/catalog_reader_cached.go +++ b/pkg/sql/catalog/internal/catkv/catalog_reader_cached.go @@ -113,7 +113,7 @@ func (c *cachedCatalogReader) IsNameInCache(key catalog.NameKey) bool { // IsDescIDKnownToNotExist is part of the CatalogReader interface. func (c *cachedCatalogReader) IsDescIDKnownToNotExist(id, maybeParentID descpb.ID) bool { - if c.cache.LookupDescriptorEntry(id) != nil { + if c.cache.LookupDescriptor(id) != nil { return false } if c.hasScanAll { @@ -303,7 +303,7 @@ func (c *cachedCatalogReader) GetByIDs( continue } if desc := c.systemDatabaseCache.lookupDescriptor(c.version, id); desc != nil { - c.cache.UpsertDescriptorEntry(desc) + c.cache.UpsertDescriptor(desc) } ids[i], ids[numUncached] = ids[numUncached], id numUncached++ @@ -323,10 +323,10 @@ func (c *cachedCatalogReader) GetByIDs( c.setByIDState(id, s) } } - ret := c.cache.FilterByIDs(ids) + ret := c.cache.FilterByIDsExclusive(ids) if isDescriptorRequired { for _, id := range ids[numUncached:] { - if ret.LookupDescriptorEntry(id) == nil { + if ret.LookupDescriptor(id) == nil { return nstree.Catalog{}, wrapError(expectedType, id, requiredError(expectedType, id)) } } @@ -378,7 +378,7 @@ func (c *cachedCatalogReader) ensure(ctx context.Context, read nstree.Catalog) e oldSize := c.cache.ByteSize() c.cache.AddAll(read) c.systemDatabaseCache.update(c.version, read) - _ = read.ForEachDescriptorEntry(func(desc catalog.Descriptor) error { + _ = read.ForEachDescriptor(func(desc catalog.Descriptor) error { if desc.Dropped() { return nil } diff --git a/pkg/sql/catalog/internal/catkv/catalog_reader_test.go b/pkg/sql/catalog/internal/catkv/catalog_reader_test.go index c3c58d1c9e3d..8fb11316d2fa 100644 --- a/pkg/sql/catalog/internal/catkv/catalog_reader_test.go +++ b/pkg/sql/catalog/internal/catkv/catalog_reader_test.go @@ -213,7 +213,7 @@ func (h testHelper) argDesc( }) require.NoErrorf(h.t, err, "%s: error reading %s descriptor %d", h.d.Pos, expectedType, id) - desc := c.LookupDescriptorEntry(id) + desc := c.LookupDescriptor(id) require.NotNilf(h.t, desc, "%s: nil %s descriptor %d", h.d.Pos, expectedType, id) require.Equal(h.t, expectedType, desc.DescriptorType(), @@ -305,7 +305,7 @@ func (h testHelper) catalogToYaml(c nstree.Catalog) interface{} { m[ns.GetID()] = j return nil }) - _ = c.ForEachDescriptorEntry(func(d catalog.Descriptor) error { + _ = c.ForEachDescriptor(func(d catalog.Descriptor) error { mut := d.NewBuilder().BuildCreatedMutable() mut.ResetModificationTime() j := m[d.GetID()] @@ -313,13 +313,13 @@ func (h testHelper) catalogToYaml(c nstree.Catalog) interface{} { m[d.GetID()] = j return nil }) - _ = c.ForEachZoneConfigEntry(func(id descpb.ID, zc catalog.ZoneConfig) error { + _ = c.ForEachZoneConfig(func(id descpb.ID, zc catalog.ZoneConfig) error { j := m[id] j.zc = zc m[id] = j return nil }) - _ = c.ForEachCommentEntry(func(key catalogkeys.CommentKey, cmt string) error { + _ = c.ForEachComment(func(key catalogkeys.CommentKey, cmt string) error { id := descpb.ID(key.ObjectID) j := m[id] j.comments = append(j.comments, struct { diff --git a/pkg/sql/catalog/internal/catkv/direct.go b/pkg/sql/catalog/internal/catkv/direct.go index 9bad4c384f0e..56059ce3eb43 100644 --- a/pkg/sql/catalog/internal/catkv/direct.go +++ b/pkg/sql/catalog/internal/catkv/direct.go @@ -224,7 +224,7 @@ func (d *direct) readDescriptorsForDirectAccess( } descs := make([]catalog.Descriptor, len(ids)) for i, id := range ids { - desc := c.LookupDescriptorEntry(id) + desc := c.LookupDescriptor(id) if desc == nil { continue } diff --git a/pkg/sql/catalog/internal/catkv/system_database_cache.go b/pkg/sql/catalog/internal/catkv/system_database_cache.go index ea146488c9df..18ff4c4c2a23 100644 --- a/pkg/sql/catalog/internal/catkv/system_database_cache.go +++ b/pkg/sql/catalog/internal/catkv/system_database_cache.go @@ -58,7 +58,7 @@ func NewSystemDatabaseCache(codec keys.SQLCodec, settings *cluster.Settings) *Sy ) var warm nstree.MutableCatalog _ = ms.ForEachCatalogDescriptor(func(desc catalog.Descriptor) error { - if desc.GetID() < keys.MaxReservedDescID { + if desc.GetID() < keys.MaxReservedDescID && !desc.SkipNamespace() { key := descpb.NameInfo{ ParentID: desc.GetParentID(), ParentSchemaID: desc.GetParentSchemaID(), diff --git a/pkg/sql/catalog/internal/catkv/validate.go b/pkg/sql/catalog/internal/catkv/validate.go index de35dd681413..ba07374ca0c0 100644 --- a/pkg/sql/catalog/internal/catkv/validate.go +++ b/pkg/sql/catalog/internal/catkv/validate.go @@ -65,7 +65,7 @@ func (c catalogReaderBackedDereferencer) DereferenceDescriptors( } ret := make([]catalog.Descriptor, len(reqs)) for i, id := range reqs { - desc := read.LookupDescriptorEntry(id) + desc := read.LookupDescriptor(id) if desc == nil { continue } diff --git a/pkg/sql/catalog/nstree/catalog.go b/pkg/sql/catalog/nstree/catalog.go index 993d97d759e0..459efadae6e4 100644 --- a/pkg/sql/catalog/nstree/catalog.go +++ b/pkg/sql/catalog/nstree/catalog.go @@ -31,9 +31,9 @@ type Catalog struct { byteSize int64 } -// ForEachDescriptorEntry iterates over all descriptor table entries in an +// ForEachDescriptor iterates over all descriptor table entries in an // ordered fashion. -func (c Catalog) ForEachDescriptorEntry(fn func(desc catalog.Descriptor) error) error { +func (c Catalog) ForEachDescriptor(fn func(desc catalog.Descriptor) error) error { if !c.IsInitialized() { return nil } @@ -45,9 +45,9 @@ func (c Catalog) ForEachDescriptorEntry(fn func(desc catalog.Descriptor) error) }) } -// ForEachCommentEntry iterates through all descriptor comments in the same +// ForEachComment iterates through all descriptor comments in the same // order as in system.comments. -func (c Catalog) ForEachCommentEntry(fn func(key catalogkeys.CommentKey, cmt string) error) error { +func (c Catalog) ForEachComment(fn func(key catalogkeys.CommentKey, cmt string) error) error { if !c.IsInitialized() { return nil } @@ -56,9 +56,24 @@ func (c Catalog) ForEachCommentEntry(fn func(key catalogkeys.CommentKey, cmt str }) } -// ForEachZoneConfigEntry iterates over all zone config table entries in an +// ForEachCommentOnDescriptor iterates through all comments on a specific +// descriptor in the same order as in system.comments. +func (c Catalog) ForEachCommentOnDescriptor( + id descpb.ID, fn func(key catalogkeys.CommentKey, cmt string) error, +) error { + if !c.IsInitialized() { + return nil + } + e := c.byID.get(id) + if e == nil { + return nil + } + return e.(*byIDEntry).forEachComment(fn) +} + +// ForEachZoneConfig iterates over all zone config table entries in an // ordered fashion. -func (c Catalog) ForEachZoneConfigEntry(fn func(id descpb.ID, zc catalog.ZoneConfig) error) error { +func (c Catalog) ForEachZoneConfig(fn func(id descpb.ID, zc catalog.ZoneConfig) error) error { if !c.IsInitialized() { return nil } @@ -106,8 +121,8 @@ func (c Catalog) ForEachSchemaNamespaceEntryInDatabase( }) } -// LookupDescriptorEntry looks up a descriptor by ID. -func (c Catalog) LookupDescriptorEntry(id descpb.ID) catalog.Descriptor { +// LookupDescriptor looks up a descriptor by ID. +func (c Catalog) LookupDescriptor(id descpb.ID) catalog.Descriptor { if !c.IsInitialized() || id == descpb.InvalidID { return nil } @@ -118,8 +133,8 @@ func (c Catalog) LookupDescriptorEntry(id descpb.ID) catalog.Descriptor { return e.(*byIDEntry).desc } -// LookupCommentEntry looks up a comment by (CommentType, ID, SubID). -func (c Catalog) LookupCommentEntry(key catalogkeys.CommentKey) (_ string, found bool) { +// LookupComment looks up a comment by (CommentType, ID, SubID). +func (c Catalog) LookupComment(key catalogkeys.CommentKey) (_ string, found bool) { if !c.IsInitialized() { return "", false } @@ -135,8 +150,8 @@ func (c Catalog) LookupCommentEntry(key catalogkeys.CommentKey) (_ string, found return cbt.comments[ordinal], true } -// LookupZoneConfigEntry looks up a zone config by ID. -func (c Catalog) LookupZoneConfigEntry(id descpb.ID) catalog.ZoneConfig { +// LookupZoneConfig looks up a zone config by ID. +func (c Catalog) LookupZoneConfig(id descpb.ID) catalog.ZoneConfig { if !c.IsInitialized() { return nil } @@ -165,7 +180,7 @@ func (c Catalog) OrderedDescriptors() []catalog.Descriptor { return nil } ret := make([]catalog.Descriptor, 0, c.byID.t.Len()) - _ = c.ForEachDescriptorEntry(func(desc catalog.Descriptor) error { + _ = c.ForEachDescriptor(func(desc catalog.Descriptor) error { ret = append(ret, desc) return nil }) @@ -200,7 +215,7 @@ func (c Catalog) DereferenceDescriptors( ) ([]catalog.Descriptor, error) { ret := make([]catalog.Descriptor, len(reqs)) for i, id := range reqs { - ret[i] = c.LookupDescriptorEntry(id) + ret[i] = c.LookupDescriptor(id) } return ret, nil } @@ -254,7 +269,7 @@ func (c Catalog) ValidateNamespaceEntry(key catalog.NameKey) error { } } // Compare the namespace entry with the referenced descriptor. - desc := c.LookupDescriptorEntry(ne.GetID()) + desc := c.LookupDescriptor(ne.GetID()) if desc == nil { return catalog.ErrDescriptorNotFound } @@ -294,3 +309,63 @@ func (c Catalog) ValidateWithRecover( func (c Catalog) ByteSize() int64 { return c.byteSize } + +// FilterByIDs returns a subset of the catalog only for the desired IDs. +func (c Catalog) FilterByIDs(ids []descpb.ID) Catalog { + var ret MutableCatalog + ret.addByIDEntries(c, ids) + if !ret.IsInitialized() { + return Catalog{} + } + _ = c.byName.ascend(func(found catalog.NameEntry) error { + if ret.byID.get(found.GetID()) == nil { + return nil + } + e := ret.ensureForName(found) + *e = *found.(*byNameEntry) + return nil + }) + return ret.Catalog +} + +// FilterByIDsExclusive is like FilterByIDs but without any by-name entries. +func (c Catalog) FilterByIDsExclusive(ids []descpb.ID) Catalog { + var ret MutableCatalog + ret.addByIDEntries(c, ids) + return ret.Catalog +} + +func (mc *MutableCatalog) addByIDEntries(c Catalog, ids []descpb.ID) { + if !c.IsInitialized() { + return + } + for _, id := range ids { + found := c.byID.get(id) + if found == nil { + continue + } + e := mc.ensureForID(id) + *e = *found.(*byIDEntry) + } +} + +// FilterByNames returns a subset of the catalog only for the desired names. +func (c Catalog) FilterByNames(nameInfos []descpb.NameInfo) Catalog { + if !c.IsInitialized() { + return Catalog{} + } + var ret MutableCatalog + for _, ni := range nameInfos { + found := c.byName.getByName(ni.ParentID, ni.ParentSchemaID, ni.Name) + if found == nil { + continue + } + e := ret.ensureForName(&ni) + *e = *found.(*byNameEntry) + if foundByID := c.byID.get(e.id); foundByID != nil { + e := ret.ensureForID(e.id) + *e = *foundByID.(*byIDEntry) + } + } + return ret.Catalog +} diff --git a/pkg/sql/catalog/nstree/catalog_mutable.go b/pkg/sql/catalog/nstree/catalog_mutable.go index ac084518b2a6..e6f809443990 100644 --- a/pkg/sql/catalog/nstree/catalog_mutable.go +++ b/pkg/sql/catalog/nstree/catalog_mutable.go @@ -72,23 +72,12 @@ func (mc *MutableCatalog) ensureForName(key catalog.NameKey) *byNameEntry { return newEntry } -// UpsertDescriptorEntry adds a descriptor to the MutableCatalog. -func (mc *MutableCatalog) UpsertDescriptorEntry(desc catalog.Descriptor) { - if desc == nil || desc.GetID() == descpb.InvalidID { - return - } - e := mc.ensureForID(desc.GetID()) - mc.byteSize -= e.ByteSize() - e.desc = desc - mc.byteSize += e.ByteSize() -} - -// DeleteDescriptorEntry removes a descriptor from the MutableCatalog. -func (mc *MutableCatalog) DeleteDescriptorEntry(id descpb.ID) { - if id == descpb.InvalidID || !mc.IsInitialized() { +// DeleteByName removes all by-name mappings from the MutableCatalog. +func (mc *MutableCatalog) DeleteByName(key catalog.NameKey) { + if key == nil || !mc.IsInitialized() { return } - if removed := mc.byID.delete(id); removed != nil { + if removed := mc.byName.delete(key); removed != nil { mc.byteSize -= removed.(catalogEntry).ByteSize() } } @@ -105,16 +94,27 @@ func (mc *MutableCatalog) UpsertNamespaceEntry( e.timestamp = mvccTimestamp } -// DeleteNamespaceEntry removes a name -> id mapping from the MutableCatalog. -func (mc *MutableCatalog) DeleteNamespaceEntry(key catalog.NameKey) { - if key == nil || !mc.IsInitialized() { +// DeleteByID removes all by-ID mappings from the MutableCatalog. +func (mc *MutableCatalog) DeleteByID(id descpb.ID) { + if !mc.IsInitialized() { return } - if removed := mc.byName.delete(key); removed != nil { + if removed := mc.byID.delete(id); removed != nil { mc.byteSize -= removed.(catalogEntry).ByteSize() } } +// UpsertDescriptor adds a descriptor to the MutableCatalog. +func (mc *MutableCatalog) UpsertDescriptor(desc catalog.Descriptor) { + if desc == nil || desc.GetID() == descpb.InvalidID { + return + } + e := mc.ensureForID(desc.GetID()) + mc.byteSize -= e.ByteSize() + e.desc = desc + mc.byteSize += e.ByteSize() +} + // UpsertComment upserts a ((ObjectID, SubID, CommentType) -> Comment) mapping // into the catalog. func (mc *MutableCatalog) UpsertComment(key catalogkeys.CommentKey, cmt string) { @@ -141,40 +141,6 @@ func (mc *MutableCatalog) UpsertZoneConfig( mc.byteSize += e.ByteSize() } -// FilterByIDs returns a subset of the catalog only for the desired IDs. -func (mc *MutableCatalog) FilterByIDs(ids []descpb.ID) Catalog { - if !mc.IsInitialized() { - return Catalog{} - } - var ret MutableCatalog - for _, id := range ids { - found := mc.byID.get(id) - if found == nil { - continue - } - e := ret.ensureForID(id) - *e = *found.(*byIDEntry) - } - return ret.Catalog -} - -// FilterByNames returns a subset of the catalog only for the desired names. -func (mc *MutableCatalog) FilterByNames(nameInfos []descpb.NameInfo) Catalog { - if !mc.IsInitialized() { - return Catalog{} - } - var ret MutableCatalog - for _, ni := range nameInfos { - found := mc.byName.getByName(ni.ParentID, ni.ParentSchemaID, ni.Name) - if found == nil { - continue - } - e := ret.ensureForName(&ni) - *e = *found.(*byNameEntry) - } - return ret.Catalog -} - // AddAll adds the contents of the provided catalog to this one. func (mc *MutableCatalog) AddAll(c Catalog) { if !c.IsInitialized() { diff --git a/pkg/sql/catalog/schemadesc/schema_desc_test.go b/pkg/sql/catalog/schemadesc/schema_desc_test.go index 8e6f82b75cd9..b23aaf906745 100644 --- a/pkg/sql/catalog/schemadesc/schema_desc_test.go +++ b/pkg/sql/catalog/schemadesc/schema_desc_test.go @@ -228,9 +228,9 @@ func TestValidateCrossSchemaReferences(t *testing.T) { var cb nstree.MutableCatalog test.desc.Privileges = privilege desc := schemadesc.NewBuilder(&test.desc).BuildImmutable() - cb.UpsertDescriptorEntry(desc) + cb.UpsertDescriptor(desc) test.dbDesc.Privileges = privilege - cb.UpsertDescriptorEntry(dbdesc.NewBuilder(&test.dbDesc).BuildImmutable()) + cb.UpsertDescriptor(dbdesc.NewBuilder(&test.dbDesc).BuildImmutable()) expectedErr := fmt.Sprintf("%s %q (%d): %s", desc.DescriptorType(), desc.GetName(), desc.GetID(), test.err) const validateCrossReferencesOnly = catalog.ValidationLevelBackReferences &^ catalog.ValidationLevelSelfOnly results := cb.Validate(ctx, clusterversion.TestingClusterVersion, catalog.NoValidationTelemetry, validateCrossReferencesOnly, desc) diff --git a/pkg/sql/catalog/schematelemetry/schema_telemetry_event.go b/pkg/sql/catalog/schematelemetry/schema_telemetry_event.go index c4ff5faec88b..df99ad41d1a0 100644 --- a/pkg/sql/catalog/schematelemetry/schema_telemetry_event.go +++ b/pkg/sql/catalog/schematelemetry/schema_telemetry_event.go @@ -80,7 +80,7 @@ func CollectClusterSchemaForTelemetry( } // Redact the descriptors. redacted := make(map[descpb.ID]*eventpb.SchemaDescriptor, descIDsInSnapshot.Len()) - _ = raw.ForEachDescriptorEntry(func(rd catalog.Descriptor) error { + _ = raw.ForEachDescriptor(func(rd catalog.Descriptor) error { if !descIDsInSnapshot.Contains(rd.GetID()) { return nil } @@ -146,7 +146,7 @@ func truncatedCatalogKeys( }) return nil }) - _ = raw.ForEachDescriptorEntry(func(desc catalog.Descriptor) error { + _ = raw.ForEachDescriptor(func(desc catalog.Descriptor) error { if !idsInNamespace.Contains(desc.GetID()) { keys = append(keys, joinedRecordKey{id: desc.GetID()}) } diff --git a/pkg/sql/catalog/tabledesc/table_desc.go b/pkg/sql/catalog/tabledesc/table_desc.go index 698bd9b70576..2d6841ac0fe2 100644 --- a/pkg/sql/catalog/tabledesc/table_desc.go +++ b/pkg/sql/catalog/tabledesc/table_desc.go @@ -75,7 +75,9 @@ func (desc *wrapper) HasConcurrentSchemaChanges() bool { // SkipNamespace implements the descriptor interface. func (desc *wrapper) SkipNamespace() bool { - return false + // Virtual tables are hard-coded and don't have entries in the + // system.namespace table. + return desc.IsVirtualTable() } // immutable is a custom type for TableDescriptors diff --git a/pkg/sql/catalog/tabledesc/validate_test.go b/pkg/sql/catalog/tabledesc/validate_test.go index 3ea670423d01..581c9f7a3764 100644 --- a/pkg/sql/catalog/tabledesc/validate_test.go +++ b/pkg/sql/catalog/tabledesc/validate_test.go @@ -2864,13 +2864,13 @@ func TestValidateCrossTableReferences(t *testing.T) { for i, test := range tests { t.Run(test.err, func(t *testing.T) { var cb nstree.MutableCatalog - cb.UpsertDescriptorEntry(dbdesc.NewBuilder(&descpb.DatabaseDescriptor{ID: 1}).BuildImmutable()) + cb.UpsertDescriptor(dbdesc.NewBuilder(&descpb.DatabaseDescriptor{ID: 1}).BuildImmutable()) for _, otherDesc := range test.otherDescs { otherDesc.Privileges = catpb.NewBasePrivilegeDescriptor(username.AdminRoleName()) - cb.UpsertDescriptorEntry(NewBuilder(&otherDesc).BuildImmutable()) + cb.UpsertDescriptor(NewBuilder(&otherDesc).BuildImmutable()) } desc := NewBuilder(&test.desc).BuildImmutable() - cb.UpsertDescriptorEntry(funcdesc.NewBuilder(&descpb.FunctionDescriptor{ID: 100, Name: "f"}).BuildImmutable()) + cb.UpsertDescriptor(funcdesc.NewBuilder(&descpb.FunctionDescriptor{ID: 100, Name: "f"}).BuildImmutable()) expectedErr := fmt.Sprintf("%s %q (%d): %s", desc.DescriptorType(), desc.GetName(), desc.GetID(), test.err) const validateCrossReferencesOnly = catalog.ValidationLevelBackReferences &^ catalog.ValidationLevelSelfOnly results := cb.Validate(ctx, clusterversion.TestingClusterVersion, catalog.NoValidationTelemetry, validateCrossReferencesOnly, desc) diff --git a/pkg/sql/catalog/typedesc/type_desc_test.go b/pkg/sql/catalog/typedesc/type_desc_test.go index c40c3281918e..d7959ae2bdea 100644 --- a/pkg/sql/catalog/typedesc/type_desc_test.go +++ b/pkg/sql/catalog/typedesc/type_desc_test.go @@ -375,20 +375,20 @@ func TestValidateTypeDesc(t *testing.T) { ) var cb nstree.MutableCatalog - cb.UpsertDescriptorEntry(dbdesc.NewBuilder(&descpb.DatabaseDescriptor{ + cb.UpsertDescriptor(dbdesc.NewBuilder(&descpb.DatabaseDescriptor{ Name: "db", ID: dbID, }).BuildImmutable()) - cb.UpsertDescriptorEntry(schemadesc.NewBuilder(&descpb.SchemaDescriptor{ + cb.UpsertDescriptor(schemadesc.NewBuilder(&descpb.SchemaDescriptor{ ID: schemaID, ParentID: dbID, Name: "schema", }).BuildImmutable()) - cb.UpsertDescriptorEntry(typedesc.NewBuilder(&descpb.TypeDescriptor{ + cb.UpsertDescriptor(typedesc.NewBuilder(&descpb.TypeDescriptor{ ID: typeID, Name: "type", }).BuildImmutable()) - cb.UpsertDescriptorEntry(dbdesc.NewBuilder(&descpb.DatabaseDescriptor{ + cb.UpsertDescriptor(dbdesc.NewBuilder(&descpb.DatabaseDescriptor{ Name: "multi-region-db", ID: multiRegionDBID, RegionConfig: &descpb.DatabaseDescriptor_RegionConfig{ diff --git a/pkg/sql/crdb_internal.go b/pkg/sql/crdb_internal.go index 8924b85f7f96..776c3e3b7f80 100644 --- a/pkg/sql/crdb_internal.go +++ b/pkg/sql/crdb_internal.go @@ -597,7 +597,7 @@ func crdbInternalTablesDatabaseLookupFunc( scNames[keys.PublicSchemaID] = catconstants.PublicSchemaName // Record database descriptors for name lookups. dbID := db.GetID() - _ = descs.ForEachDescriptorEntry(func(desc catalog.Descriptor) error { + _ = descs.ForEachDescriptor(func(desc catalog.Descriptor) error { if desc.GetParentID() != dbID { return nil } @@ -608,7 +608,7 @@ func crdbInternalTablesDatabaseLookupFunc( }) rf := makeCrdbInternalTablesAddRowFn(p, addRow) var seenAny bool - if err := descs.ForEachDescriptorEntry(func(desc catalog.Descriptor) error { + if err := descs.ForEachDescriptor(func(desc catalog.Descriptor) error { if desc.GetParentID() != dbID { return nil } @@ -5176,7 +5176,7 @@ CREATE TABLE crdb_internal.invalid_objects ( { lCtx := newInternalLookupCtx(c.OrderedDescriptors(), dbContext) - if err := c.ForEachDescriptorEntry(func(desc catalog.Descriptor) error { + if err := c.ForEachDescriptor(func(desc catalog.Descriptor) error { switch d := desc.(type) { case catalog.DatabaseDescriptor: if dbContext != nil && d.GetID() != dbContext.GetID() { @@ -5486,7 +5486,7 @@ CREATE TABLE crdb_internal.lost_descriptors_with_data ( // shouldCheck returns true iff we expect no data to exist with that // table ID prefix. shouldCheck := func(id descpb.ID) bool { - return minID <= id && id < maxID && c.LookupDescriptorEntry(id) == nil + return minID <= id && id < maxID && c.LookupDescriptor(id) == nil } // hasData returns true iff there exists at least one row with a prefix for // a table ID in [startID, endID[. diff --git a/pkg/sql/doctor/doctor.go b/pkg/sql/doctor/doctor.go index 2fc524a1dc17..0b29c1138c21 100644 --- a/pkg/sql/doctor/doctor.go +++ b/pkg/sql/doctor/doctor.go @@ -184,7 +184,7 @@ func ExamineDescriptors( descReport(stdout, desc, "different id in descriptor table: %d", row.ID) continue } - cb.UpsertDescriptorEntry(desc) + cb.UpsertDescriptor(desc) } for _, row := range descTable { id := descpb.ID(row.ID) diff --git a/pkg/sql/schemachanger/scbackup/job.go b/pkg/sql/schemachanger/scbackup/job.go index 5549ee2762eb..4a908a0fbfe4 100644 --- a/pkg/sql/schemachanger/scbackup/job.go +++ b/pkg/sql/schemachanger/scbackup/job.go @@ -39,7 +39,7 @@ func CreateDeclarativeSchemaChangeJobs( allMut nstree.Catalog, ) error { byJobID := make(map[catpb.JobID][]catalog.MutableDescriptor) - _ = allMut.ForEachDescriptorEntry(func(d catalog.Descriptor) error { + _ = allMut.ForEachDescriptor(func(d catalog.Descriptor) error { if s := d.GetDeclarativeSchemaChangerState(); s != nil { byJobID[s.JobID] = append(byJobID[s.JobID], d.(catalog.MutableDescriptor)) } diff --git a/pkg/sql/schemachanger/scdeps/sctestdeps/config.go b/pkg/sql/schemachanger/scdeps/sctestdeps/config.go index 442cef2cc688..b4be5ba8ad5b 100644 --- a/pkg/sql/schemachanger/scdeps/sctestdeps/config.go +++ b/pkg/sql/schemachanger/scdeps/sctestdeps/config.go @@ -50,7 +50,7 @@ func WithNamespace(c nstree.Catalog) Option { func WithDescriptors(c nstree.Catalog) Option { modifTime := hlc.Timestamp{WallTime: defaultOverriddenCreatedAt.UnixNano()} return optionFunc(func(state *TestState) { - _ = c.ForEachDescriptorEntry(func(desc catalog.Descriptor) error { + _ = c.ForEachDescriptor(func(desc catalog.Descriptor) error { mut := desc.NewBuilder().BuildCreatedMutable() switch m := mut.(type) { case *tabledesc.Mutable: @@ -63,8 +63,8 @@ func WithDescriptors(c nstree.Catalog) Option { } mut.ResetModificationTime() desc = mut.ImmutableCopy() - state.committed.UpsertDescriptorEntry(desc) - state.uncommitted.UpsertDescriptorEntry(desc) + state.committed.UpsertDescriptor(desc) + state.uncommitted.UpsertDescriptor(desc) return nil }) }) diff --git a/pkg/sql/schemachanger/scdeps/sctestdeps/database_state.go b/pkg/sql/schemachanger/scdeps/sctestdeps/database_state.go index 3ba11f70b286..75a4374c7143 100644 --- a/pkg/sql/schemachanger/scdeps/sctestdeps/database_state.go +++ b/pkg/sql/schemachanger/scdeps/sctestdeps/database_state.go @@ -97,7 +97,7 @@ ORDER BY id`) t.TypeDescriptor.ModificationTime = hlc.Timestamp{} } - cb.UpsertDescriptorEntry(desc) + cb.UpsertDescriptor(desc) } return cb } @@ -143,7 +143,7 @@ func ReadZoneConfigsFromDB( t *testing.T, tdb *sqlutils.SQLRunner, descCatalog nstree.Catalog, ) map[catid.DescID]catalog.ZoneConfig { zoneCfgMap := make(map[catid.DescID]catalog.ZoneConfig) - require.NoError(t, descCatalog.ForEachDescriptorEntry(func(desc catalog.Descriptor) error { + require.NoError(t, descCatalog.ForEachDescriptor(func(desc catalog.Descriptor) error { zoneCfgRow := tdb.Query(t, ` SELECT config FROM system.zones WHERE id=$1 `, diff --git a/pkg/sql/schemachanger/scdeps/sctestdeps/test_deps.go b/pkg/sql/schemachanger/scdeps/sctestdeps/test_deps.go index f56c7481e851..b9a4f958ea06 100644 --- a/pkg/sql/schemachanger/scdeps/sctestdeps/test_deps.go +++ b/pkg/sql/schemachanger/scdeps/sctestdeps/test_deps.go @@ -578,11 +578,11 @@ func (s *TestState) mustReadImmutableDescriptor(id descpb.ID) (catalog.Descripto // mustReadMutableDescriptor looks up a descriptor and returns a mutable // deep copy. func (s *TestState) mustReadMutableDescriptor(id descpb.ID) (catalog.MutableDescriptor, error) { - u := s.uncommitted.LookupDescriptorEntry(id) + u := s.uncommitted.LookupDescriptor(id) if u == nil { return nil, errors.Wrapf(catalog.ErrDescriptorNotFound, "reading mutable descriptor #%d", id) } - c := s.committed.LookupDescriptorEntry(id) + c := s.committed.LookupDescriptor(id) return descbuilder.BuildMutable(c, u.DescriptorProto(), s.mvccTimestamp()) } @@ -773,18 +773,18 @@ func (b *testCatalogChangeBatcher) ValidateAndRun(ctx context.Context) error { } } b.s.LogSideEffectf("delete %s namespace entry %v -> %d", nameType, nameInfo, expectedID) - b.s.uncommitted.DeleteNamespaceEntry(nameInfo) + b.s.uncommitted.DeleteByName(nameInfo) } for _, desc := range b.descs { mut := desc.NewBuilder().BuildCreatedMutable() mut.ResetModificationTime() desc = mut.ImmutableCopy() b.s.LogSideEffectf("upsert descriptor #%d\n%s", desc.GetID(), b.s.descriptorDiff(desc)) - b.s.uncommitted.UpsertDescriptorEntry(desc) + b.s.uncommitted.UpsertDescriptor(desc) } for _, deletedID := range b.descriptorsToDelete.Ordered() { b.s.LogSideEffectf("delete descriptor #%d", deletedID) - b.s.uncommitted.DeleteDescriptorEntry(deletedID) + b.s.uncommitted.DeleteByID(deletedID) } for _, deletedID := range b.zoneConfigsToDelete.Ordered() { b.s.LogSideEffectf("deleting zone config for #%d", deletedID) diff --git a/pkg/sql/schemachanger/scdeps/sctestdeps/test_state.go b/pkg/sql/schemachanger/scdeps/sctestdeps/test_state.go index 3e4f904db716..9927a3651a4a 100644 --- a/pkg/sql/schemachanger/scdeps/sctestdeps/test_state.go +++ b/pkg/sql/schemachanger/scdeps/sctestdeps/test_state.go @@ -119,12 +119,12 @@ func (s *TestState) WithTxn(fn func(s *TestState)) { s.uncommitted.UpsertNamespaceEntry(e, e.GetID(), e.GetMVCCTimestamp()) return nil }) - _ = u.ForEachDescriptorEntry(func(d catalog.Descriptor) error { + _ = u.ForEachDescriptor(func(d catalog.Descriptor) error { mut := d.NewBuilder().BuildCreatedMutable() mut.ResetModificationTime() d = mut.ImmutableCopy() - s.committed.UpsertDescriptorEntry(d) - s.uncommitted.UpsertDescriptorEntry(d) + s.committed.UpsertDescriptor(d) + s.uncommitted.UpsertDescriptor(d) return nil }) s.LogSideEffectf("commit transaction #%d", s.txnCounter) diff --git a/pkg/sql/schemachanger/scexec/exec_backfill_test.go b/pkg/sql/schemachanger/scexec/exec_backfill_test.go index 2929c8af48b4..342d170fc282 100644 --- a/pkg/sql/schemachanger/scexec/exec_backfill_test.go +++ b/pkg/sql/schemachanger/scexec/exec_backfill_test.go @@ -99,7 +99,7 @@ func TestExecBackfiller(t *testing.T) { } findTableWithName := func(c nstree.Catalog, name string) (tab catalog.TableDescriptor) { - _ = c.ForEachDescriptorEntry(func(desc catalog.Descriptor) error { + _ = c.ForEachDescriptor(func(desc catalog.Descriptor) error { var ok bool tab, ok = desc.(catalog.TableDescriptor) if ok && tab.GetName() == name { @@ -132,7 +132,7 @@ func TestExecBackfiller(t *testing.T) { require.NotNil(t, tab) mut := tabledesc.NewBuilder(tab.TableDesc()).BuildExistingMutableTable() addIndexMutation(t, mut, "idx", 2, false /* isTempIndex */, "j") - descs.UpsertDescriptorEntry(mut) + descs.UpsertDescriptor(mut) mc, bt, bf, _, deps := setupTestDeps(t, tdb, descs.Catalog) defer mc.Finish() read, err := deps.Catalog().MustReadImmutableDescriptors(ctx, mut.GetID()) @@ -191,7 +191,7 @@ func TestExecBackfiller(t *testing.T) { mut := tabledesc.NewBuilder(tab.TableDesc()).BuildExistingMutableTable() addIndexMutation(t, mut, "idx", 2, false /* isTempIndex */, "j") addIndexMutation(t, mut, "idx", 3, false /* isTempIndex */, "k", "j") - descs.UpsertDescriptorEntry(mut) + descs.UpsertDescriptor(mut) } var barID descpb.ID { @@ -201,7 +201,7 @@ func TestExecBackfiller(t *testing.T) { mut := tabledesc.NewBuilder(tab.TableDesc()).BuildExistingMutableTable() addIndexMutation(t, mut, "idx", 4, false /* isTempIndex */, "j") addIndexMutation(t, mut, "idx", 5, false /* isTempIndex */, "k", "j") - descs.UpsertDescriptorEntry(mut) + descs.UpsertDescriptor(mut) } mc, bt, bf, _, deps := setupTestDeps(t, tdb, descs.Catalog) @@ -290,7 +290,7 @@ func TestExecBackfiller(t *testing.T) { m.State = descpb.DescriptorMutation_WRITE_ONLY } } - descs.UpsertDescriptorEntry(mut) + descs.UpsertDescriptor(mut) mc, bt, _, m, deps := setupTestDeps(t, tdb, descs.Catalog) defer mc.Finish() read, err := deps.Catalog().MustReadImmutableDescriptors(ctx, mut.GetID()) diff --git a/pkg/sql/schemachanger/sctest/decomp.go b/pkg/sql/schemachanger/sctest/decomp.go index 902d6241dea3..148562a6cdc5 100644 --- a/pkg/sql/schemachanger/sctest/decomp.go +++ b/pkg/sql/schemachanger/sctest/decomp.go @@ -72,7 +72,7 @@ func runDecomposeTest( name := fields[0] var desc catalog.Descriptor allDescs := sctestdeps.ReadDescriptorsFromDB(ctx, t, tdb) - _ = allDescs.ForEachDescriptorEntry(func(d catalog.Descriptor) error { + _ = allDescs.ForEachDescriptor(func(d catalog.Descriptor) error { if d.GetName() == name { desc = d } @@ -86,7 +86,7 @@ func runDecomposeTest( testDeps := sctestdeps.NewTestDependencies( sctestdeps.WithComments(sctestdeps.ReadCommentsFromDB(t, tdb)), sctestdeps.WithZoneConfigs(sctestdeps.ReadZoneConfigsFromDB(t, tdb, allDescs.Catalog))) - backRefs := scdecomp.WalkDescriptor(ctx, desc, allDescs.LookupDescriptorEntry, visitor, testDeps, testDeps) + backRefs := scdecomp.WalkDescriptor(ctx, desc, allDescs.LookupDescriptor, visitor, testDeps, testDeps) return marshalResult(t, m, backRefs) default: diff --git a/pkg/sql/virtual_schema.go b/pkg/sql/virtual_schema.go index 723a6a78cc82..968aa077577a 100644 --- a/pkg/sql/virtual_schema.go +++ b/pkg/sql/virtual_schema.go @@ -42,6 +42,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry" "github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented" "github.com/cockroachdb/cockroach/pkg/util/hlc" + "github.com/cockroachdb/cockroach/pkg/util/iterutil" "github.com/cockroachdb/cockroach/pkg/util/stop" "github.com/cockroachdb/errors" ) @@ -405,6 +406,21 @@ func (vs *VirtualSchemaHolder) GetVirtualObjectByID(id descpb.ID) (catalog.Virtu return entry, true } +// Visit makes VirtualSchemaHolder implement catalog.VirtualSchemas. +func (vs *VirtualSchemaHolder) Visit(fn func(desc catalog.Descriptor, comment string) error) error { + for _, sc := range vs.schemasByID { + if err := fn(sc.desc, "" /* comment */); err != nil { + return iterutil.Map(err) + } + for _, def := range sc.defs { + if err := fn(def.desc, def.comment); err != nil { + return iterutil.Map(err) + } + } + } + return nil +} + var _ catalog.VirtualSchemas = (*VirtualSchemaHolder)(nil) type virtualSchemaEntry struct {