Skip to content

Commit 62d08bf

Browse files
craig[bot]rafiss
andcommitted
Merge #154051
154051: sql: add setting to gate leased descriptors for vtables r=rafiss a=rafiss This adds a cluster setting to control if we will use leased descriptors to populate virtual tables. It's off by default, and we plan to enable it eventually in a later release. As part of this, we also update the remaining places where we call GetAllDescriptors or related functions to make those use leased descriptors if the setting is enabled. fixes #154043 Release note (sql change): Added the sql.catalog.allow_leased_descriptors.enabled cluster setting, which is false by default. When set to true, queries that access the pg_catalog or information_schema can use cached leased descriptors to populate the data in those tables, with the tradeoff that some of the data could be stale. Co-authored-by: Rafi Shamim <[email protected]>
2 parents 4e6b6f8 + fccf26d commit 62d08bf

File tree

8 files changed

+164
-74
lines changed

8 files changed

+164
-74
lines changed

docs/generated/settings/settings-for-tenants.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ sql.auth.change_own_password.enabled boolean false controls whether a user is al
177177
sql.auth.grant_option_for_owner.enabled boolean true determines whether the GRANT OPTION for privileges is implicitly given to the owner of an object application
178178
sql.auth.grant_option_inheritance.enabled boolean true determines whether the GRANT OPTION for privileges is inherited through role membership application
179179
sql.auth.public_schema_create_privilege.enabled boolean true determines whether to grant all users the CREATE privileges on the public schema when it is created application
180+
sql.catalog.allow_leased_descriptors.enabled boolean false if true, catalog views (crdb_internal, information_schema, pg_catalog) can use leased descriptors for improved performance application
180181
sql.closed_session_cache.capacity integer 1000 the maximum number of sessions in the cache application
181182
sql.closed_session_cache.time_to_live integer 3600 the maximum time to live, in seconds application
182183
sql.contention.event_store.capacity byte size 64 MiB the in-memory storage capacity per-node of contention event store application

docs/generated/settings/settings.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@
214214
<tr><td><div id="setting-sql-auth-grant-option-for-owner-enabled" class="anchored"><code>sql.auth.grant_option_for_owner.enabled</code></div></td><td>boolean</td><td><code>true</code></td><td>determines whether the GRANT OPTION for privileges is implicitly given to the owner of an object</td><td>Basic/Standard/Advanced/Self-Hosted</td></tr>
215215
<tr><td><div id="setting-sql-auth-grant-option-inheritance-enabled" class="anchored"><code>sql.auth.grant_option_inheritance.enabled</code></div></td><td>boolean</td><td><code>true</code></td><td>determines whether the GRANT OPTION for privileges is inherited through role membership</td><td>Basic/Standard/Advanced/Self-Hosted</td></tr>
216216
<tr><td><div id="setting-sql-auth-public-schema-create-privilege-enabled" class="anchored"><code>sql.auth.public_schema_create_privilege.enabled</code></div></td><td>boolean</td><td><code>true</code></td><td>determines whether to grant all users the CREATE privileges on the public schema when it is created</td><td>Basic/Standard/Advanced/Self-Hosted</td></tr>
217+
<tr><td><div id="setting-sql-catalog-allow-leased-descriptors-enabled" class="anchored"><code>sql.catalog.allow_leased_descriptors.enabled</code></div></td><td>boolean</td><td><code>false</code></td><td>if true, catalog views (crdb_internal, information_schema, pg_catalog) can use leased descriptors for improved performance</td><td>Basic/Standard/Advanced/Self-Hosted</td></tr>
217218
<tr><td><div id="setting-sql-closed-session-cache-capacity" class="anchored"><code>sql.closed_session_cache.capacity</code></div></td><td>integer</td><td><code>1000</code></td><td>the maximum number of sessions in the cache</td><td>Basic/Standard/Advanced/Self-Hosted</td></tr>
218219
<tr><td><div id="setting-sql-closed-session-cache-time-to-live" class="anchored"><code>sql.closed_session_cache.time_to_live</code></div></td><td>integer</td><td><code>3600</code></td><td>the maximum time to live, in seconds</td><td>Basic/Standard/Advanced/Self-Hosted</td></tr>
219220
<tr><td><div id="setting-sql-contention-event-store-capacity" class="anchored"><code>sql.contention.event_store.capacity</code></div></td><td>byte size</td><td><code>64 MiB</code></td><td>the in-memory storage capacity per-node of contention event store</td><td>Basic/Standard/Advanced/Self-Hosted</td></tr>

pkg/cmd/roachtest/tests/large_schema_benchmark.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ func registerLargeSchemaBenchmark(r registry.Registry, numTables int, isMultiReg
156156
// of time. In multi-region this latency can be substantial.
157157
_, err := conn.Exec("SET CLUSTER SETTING sql.defaults.autocommit_before_ddl.enabled = 'false'")
158158
require.NoError(t, err)
159+
// Allow optimizations to use leased descriptors when querying
160+
// pg_catalog and information_schema.
161+
_, err = conn.Exec("SET CLUSTER SETTING sql.catalog.allow_leased_descriptors.enabled = 'true'")
162+
require.NoError(t, err)
159163
// Since we will be making a large number of databases / tables
160164
// quickly,on MR the job retention can slow things down. Let's
161165
// minimize how long jobs are kept, so that the creation / ingest

pkg/sql/catalog/descs/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ go_library(
3636
"//pkg/kv",
3737
"//pkg/roachpb",
3838
"//pkg/server/serverpb",
39+
"//pkg/settings",
3940
"//pkg/settings/cluster",
4041
"//pkg/spanconfig",
4142
"//pkg/sql/catalog",

pkg/sql/catalog/descs/collection.go

Lines changed: 107 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/cockroachdb/cockroach/pkg/keys"
1717
"github.com/cockroachdb/cockroach/pkg/kv"
1818
"github.com/cockroachdb/cockroach/pkg/roachpb"
19+
"github.com/cockroachdb/cockroach/pkg/settings"
1920
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
2021
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
2122
"github.com/cockroachdb/cockroach/pkg/sql/catalog/bootstrap"
@@ -369,6 +370,63 @@ func WithOnlyVersionBump() WriteDescOption {
369370
return onlyVersionBumpOption(true)
370371
}
371372

373+
type getAllOptions struct {
374+
allowLeased bool
375+
}
376+
377+
// GetAllOption defines functional options for GetAll* methods.
378+
type GetAllOption interface {
379+
apply(*getAllOptions)
380+
}
381+
382+
type allowLeasedOption bool
383+
384+
func (c allowLeasedOption) apply(opts *getAllOptions) {
385+
opts.allowLeased = bool(c)
386+
}
387+
388+
// WithAllowLeased configures GetAll* methods to allow leased descriptors.
389+
func WithAllowLeased() GetAllOption {
390+
return allowLeasedOption(true)
391+
}
392+
393+
// applyGetAllOptions applies the provided functional options to a getAllOptions struct.
394+
func applyGetAllOptions(opts []GetAllOption) getAllOptions {
395+
options := getAllOptions{}
396+
for _, opt := range opts {
397+
opt.apply(&options)
398+
}
399+
return options
400+
}
401+
402+
var allowLeasedDescriptorsInCatalogViews = settings.RegisterBoolSetting(
403+
settings.ApplicationLevel,
404+
"sql.catalog.allow_leased_descriptors.enabled",
405+
"if true, catalog views (crdb_internal, information_schema, pg_catalog) can use leased descriptors for improved performance",
406+
false,
407+
settings.WithPublic,
408+
)
409+
410+
// GetCatalogGetAllOptions returns the functional options for GetAll* methods
411+
// based on the cluster setting for allowing leased descriptors in catalog views.
412+
func GetCatalogGetAllOptions(sv *settings.Values) []GetAllOption {
413+
if allowLeasedDescriptorsInCatalogViews.Get(sv) {
414+
return []GetAllOption{WithAllowLeased()}
415+
}
416+
return nil
417+
}
418+
419+
// GetCatalogDescriptorGetter returns the appropriate descriptor getter for
420+
// catalog views based on the cluster setting.
421+
func GetCatalogDescriptorGetter(
422+
descriptors *Collection, txn *kv.Txn, sv *settings.Values,
423+
) ByIDGetterBuilder {
424+
if allowLeasedDescriptorsInCatalogViews.Get(sv) {
425+
return descriptors.ByIDWithLeased(txn)
426+
}
427+
return descriptors.ByIDWithoutLeased(txn)
428+
}
429+
372430
// maybeMarkVersionBumpOnly updates the version bump only flag for the
373431
// descriptor so it reflects previous mutations to the descriptor along with the
374432
// current mutation.
@@ -830,12 +888,15 @@ func newMutableSyntheticDescriptorAssertionError(id descpb.ID) error {
830888

831889
// GetAll returns all descriptors, namespace entries, comments and
832890
// zone configs visible by the transaction.
833-
func (tc *Collection) GetAll(ctx context.Context, txn *kv.Txn) (nstree.Catalog, error) {
891+
func (tc *Collection) GetAll(
892+
ctx context.Context, txn *kv.Txn, opts ...GetAllOption,
893+
) (nstree.Catalog, error) {
894+
options := applyGetAllOptions(opts)
834895
stored, err := tc.cr.ScanAll(ctx, txn)
835896
if err != nil {
836897
return nstree.Catalog{}, err
837898
}
838-
ret, err := tc.aggregateAllLayers(ctx, txn, stored)
899+
ret, err := tc.aggregateAllLayers(ctx, txn, options.allowLeased, stored)
839900
if err != nil {
840901
return nstree.Catalog{}, err
841902
}
@@ -860,7 +921,8 @@ func (tc *Collection) GetAllComments(
860921
if err != nil {
861922
return nil, err
862923
}
863-
comments, err := tc.aggregateAllLayers(ctx, txn, kvComments)
924+
const allowLeased = false
925+
comments, err := tc.aggregateAllLayers(ctx, txn, allowLeased, kvComments)
864926
if err != nil {
865927
return nil, err
866928
}
@@ -878,12 +940,15 @@ func (tc *Collection) GetAllFromStorageUnvalidated(
878940
}
879941

880942
// GetAllDatabases is like GetAll but filtered to non-dropped databases.
881-
func (tc *Collection) GetAllDatabases(ctx context.Context, txn *kv.Txn) (nstree.Catalog, error) {
943+
func (tc *Collection) GetAllDatabases(
944+
ctx context.Context, txn *kv.Txn, opts ...GetAllOption,
945+
) (nstree.Catalog, error) {
946+
options := applyGetAllOptions(opts)
882947
stored, err := tc.cr.ScanNamespaceForDatabases(ctx, txn)
883948
if err != nil {
884949
return nstree.Catalog{}, err
885950
}
886-
ret, err := tc.aggregateAllLayers(ctx, txn, stored)
951+
ret, err := tc.aggregateAllLayers(ctx, txn, options.allowLeased, stored)
887952
if err != nil {
888953
return nstree.Catalog{}, err
889954
}
@@ -901,17 +966,18 @@ func (tc *Collection) GetAllDatabases(ctx context.Context, txn *kv.Txn) (nstree.
901966
// GetAllSchemasInDatabase is like GetAll but filtered to the schemas with
902967
// the specified parent database. Includes virtual schemas.
903968
func (tc *Collection) GetAllSchemasInDatabase(
904-
ctx context.Context, txn *kv.Txn, db catalog.DatabaseDescriptor,
969+
ctx context.Context, txn *kv.Txn, db catalog.DatabaseDescriptor, opts ...GetAllOption,
905970
) (nstree.Catalog, error) {
971+
options := applyGetAllOptions(opts)
906972
stored, err := tc.cr.ScanNamespaceForDatabaseSchemas(ctx, txn, db)
907973
if err != nil {
908974
return nstree.Catalog{}, err
909975
}
910976
var ret nstree.MutableCatalog
911977
if db.HasPublicSchemaWithDescriptor() {
912-
ret, err = tc.aggregateAllLayers(ctx, txn, stored)
978+
ret, err = tc.aggregateAllLayers(ctx, txn, options.allowLeased, stored)
913979
} else {
914-
ret, err = tc.aggregateAllLayers(ctx, txn, stored, schemadesc.GetPublicSchema())
980+
ret, err = tc.aggregateAllLayers(ctx, txn, options.allowLeased, stored, schemadesc.GetPublicSchema())
915981
}
916982
if err != nil {
917983
return nstree.Catalog{}, err
@@ -938,8 +1004,13 @@ func (tc *Collection) GetAllSchemasInDatabase(
9381004
// the specified parent schema. Includes virtual objects. Does not include
9391005
// dropped objects.
9401006
func (tc *Collection) GetAllObjectsInSchema(
941-
ctx context.Context, txn *kv.Txn, db catalog.DatabaseDescriptor, sc catalog.SchemaDescriptor,
1007+
ctx context.Context,
1008+
txn *kv.Txn,
1009+
db catalog.DatabaseDescriptor,
1010+
sc catalog.SchemaDescriptor,
1011+
opts ...GetAllOption,
9421012
) (nstree.Catalog, error) {
1013+
options := applyGetAllOptions(opts)
9431014
var ret nstree.MutableCatalog
9441015
if sc.SchemaKind() == catalog.SchemaVirtual {
9451016
tc.virtual.addAllToCatalog(ret)
@@ -948,7 +1019,7 @@ func (tc *Collection) GetAllObjectsInSchema(
9481019
if err != nil {
9491020
return nstree.Catalog{}, err
9501021
}
951-
ret, err = tc.aggregateAllLayers(ctx, txn, stored, sc)
1022+
ret, err = tc.aggregateAllLayers(ctx, txn, options.allowLeased, stored, sc)
9521023
if err != nil {
9531024
return nstree.Catalog{}, err
9541025
}
@@ -967,13 +1038,14 @@ func (tc *Collection) GetAllObjectsInSchema(
9671038
// GetAllObjectsInSchema applied to each of those schemas.
9681039
// Includes virtual objects. Does not include dropped objects.
9691040
func (tc *Collection) GetAllInDatabase(
970-
ctx context.Context, txn *kv.Txn, db catalog.DatabaseDescriptor,
1041+
ctx context.Context, txn *kv.Txn, db catalog.DatabaseDescriptor, opts ...GetAllOption,
9711042
) (nstree.Catalog, error) {
1043+
options := applyGetAllOptions(opts)
9721044
stored, err := tc.cr.ScanNamespaceForDatabaseSchemasAndObjects(ctx, txn, db)
9731045
if err != nil {
9741046
return nstree.Catalog{}, err
9751047
}
976-
schemas, err := tc.GetAllSchemasInDatabase(ctx, txn, db)
1048+
schemas, err := tc.GetAllSchemasInDatabase(ctx, txn, db, opts...)
9771049
if err != nil {
9781050
return nstree.Catalog{}, err
9791051
}
@@ -985,7 +1057,7 @@ func (tc *Collection) GetAllInDatabase(
9851057
}); err != nil {
9861058
return nstree.Catalog{}, err
9871059
}
988-
ret, err := tc.aggregateAllLayers(ctx, txn, stored, schemasSlice...)
1060+
ret, err := tc.aggregateAllLayers(ctx, txn, options.allowLeased, stored, schemasSlice...)
9891061
if err != nil {
9901062
return nstree.Catalog{}, err
9911063
}
@@ -1013,17 +1085,18 @@ func (tc *Collection) GetAllInDatabase(
10131085
// GetAllTablesInDatabase is like GetAllInDatabase but filtered to tables.
10141086
// Includes virtual objects. Does not include dropped objects.
10151087
func (tc *Collection) GetAllTablesInDatabase(
1016-
ctx context.Context, txn *kv.Txn, db catalog.DatabaseDescriptor,
1088+
ctx context.Context, txn *kv.Txn, db catalog.DatabaseDescriptor, opts ...GetAllOption,
10171089
) (nstree.Catalog, error) {
1090+
options := applyGetAllOptions(opts)
10181091
stored, err := tc.cr.ScanNamespaceForDatabaseSchemasAndObjects(ctx, txn, db)
10191092
if err != nil {
10201093
return nstree.Catalog{}, err
10211094
}
10221095
var ret nstree.MutableCatalog
10231096
if db.HasPublicSchemaWithDescriptor() {
1024-
ret, err = tc.aggregateAllLayers(ctx, txn, stored)
1097+
ret, err = tc.aggregateAllLayers(ctx, txn, options.allowLeased, stored)
10251098
} else {
1026-
ret, err = tc.aggregateAllLayers(ctx, txn, stored, schemadesc.GetPublicSchema())
1099+
ret, err = tc.aggregateAllLayers(ctx, txn, options.allowLeased, stored, schemadesc.GetPublicSchema())
10271100
}
10281101
if err != nil {
10291102
return nstree.Catalog{}, err
@@ -1046,7 +1119,11 @@ func (tc *Collection) GetAllTablesInDatabase(
10461119
// takes care to stack all of the Collection's layer appropriately and ensures
10471120
// that the returned descriptors are properly hydrated and validated.
10481121
func (tc *Collection) aggregateAllLayers(
1049-
ctx context.Context, txn *kv.Txn, stored nstree.Catalog, schemas ...catalog.SchemaDescriptor,
1122+
ctx context.Context,
1123+
txn *kv.Txn,
1124+
allowLeased bool,
1125+
stored nstree.Catalog,
1126+
schemas ...catalog.SchemaDescriptor,
10501127
) (ret nstree.MutableCatalog, _ error) {
10511128
// Descriptors need to be re-read to ensure proper validation hydration etc.
10521129
// We collect their IDs for this purpose and we'll re-add them later.
@@ -1142,8 +1219,12 @@ func (tc *Collection) aggregateAllLayers(
11421219
// Remove deleted descriptors from consideration, re-read and add the rest.
11431220
tc.deletedDescs.ForEach(descIDs.Remove)
11441221
allDescs := make([]catalog.Descriptor, descIDs.Len())
1222+
flags := defaultUnleasedFlags()
1223+
if allowLeased {
1224+
flags.layerFilters.withoutLeased = false
1225+
}
11451226
if err := getDescriptorsByID(
1146-
ctx, tc, txn, defaultUnleasedFlags(), allDescs, descIDs.Ordered()...,
1227+
ctx, tc, txn, flags, allDescs, descIDs.Ordered()...,
11471228
); err != nil {
11481229
return nstree.MutableCatalog{}, err
11491230
}
@@ -1159,7 +1240,7 @@ func (tc *Collection) aggregateAllLayers(
11591240
// in the requested database.
11601241
// Deprecated: prefer GetAllInDatabase.
11611242
func (tc *Collection) GetAllDescriptorsForDatabase(
1162-
ctx context.Context, txn *kv.Txn, db catalog.DatabaseDescriptor,
1243+
ctx context.Context, txn *kv.Txn, db catalog.DatabaseDescriptor, opts ...GetAllOption,
11631244
) (nstree.Catalog, error) {
11641245
// Re-read database descriptor to have the freshest version.
11651246
{
@@ -1169,7 +1250,7 @@ func (tc *Collection) GetAllDescriptorsForDatabase(
11691250
return nstree.Catalog{}, err
11701251
}
11711252
}
1172-
c, err := tc.GetAllInDatabase(ctx, txn, db)
1253+
c, err := tc.GetAllInDatabase(ctx, txn, db, opts...)
11731254
if err != nil {
11741255
return nstree.Catalog{}, err
11751256
}
@@ -1185,8 +1266,10 @@ func (tc *Collection) GetAllDescriptorsForDatabase(
11851266
// GetAllDescriptors returns all physical descriptors visible by the
11861267
// transaction.
11871268
// Deprecated: prefer GetAll.
1188-
func (tc *Collection) GetAllDescriptors(ctx context.Context, txn *kv.Txn) (nstree.Catalog, error) {
1189-
all, err := tc.GetAll(ctx, txn)
1269+
func (tc *Collection) GetAllDescriptors(
1270+
ctx context.Context, txn *kv.Txn, opts ...GetAllOption,
1271+
) (nstree.Catalog, error) {
1272+
all, err := tc.GetAll(ctx, txn, opts...)
11901273
if err != nil {
11911274
return nstree.Catalog{}, err
11921275
}
@@ -1213,9 +1296,9 @@ func (tc *Collection) GetAllDescriptors(ctx context.Context, txn *kv.Txn) (nstre
12131296
// transaction, ordered by name.
12141297
// Deprecated: prefer GetAllDatabases.
12151298
func (tc *Collection) GetAllDatabaseDescriptors(
1216-
ctx context.Context, txn *kv.Txn,
1299+
ctx context.Context, txn *kv.Txn, opts ...GetAllOption,
12171300
) (ret []catalog.DatabaseDescriptor, _ error) {
1218-
c, err := tc.GetAllDatabases(ctx, txn)
1301+
c, err := tc.GetAllDatabases(ctx, txn, opts...)
12191302
if err != nil {
12201303
return nil, err
12211304
}

0 commit comments

Comments
 (0)