diff --git a/pkg/ccl/backupccl/restore_job.go b/pkg/ccl/backupccl/restore_job.go index 25437edb1445..03619b9fb8f5 100644 --- a/pkg/ccl/backupccl/restore_job.go +++ b/pkg/ccl/backupccl/restore_job.go @@ -2683,13 +2683,11 @@ func setGCTTLForDroppingTable( return err } - schemaDesc, err := descsCol.GetImmutableSchemaByID(ctx, txn, tableToDrop.GetParentSchemaID(), - tree.SchemaLookupFlags{ - Required: true, - IncludeDropped: true, - IncludeOffline: true, - AvoidLeased: true, - }) + schemaDesc, err := descsCol.ByID(txn).WithFlags(tree.SchemaLookupFlags{ + IncludeDropped: true, + IncludeOffline: true, + AvoidLeased: true, + }).Immutable().Schema(ctx, tableToDrop.GetParentSchemaID()) if err != nil { return err } diff --git a/pkg/ccl/backupccl/restore_planning.go b/pkg/ccl/backupccl/restore_planning.go index 692e8ae8ef8d..2b39b4944d7f 100644 --- a/pkg/ccl/backupccl/restore_planning.go +++ b/pkg/ccl/backupccl/restore_planning.go @@ -332,11 +332,11 @@ func allocateDescriptorRewrites( } else { // If we found an existing schema, then we need to remap all references // to this schema to the existing one. - desc, err := col.GetImmutableSchemaByID(ctx, txn, id, tree.SchemaLookupFlags{ + desc, err := col.ByID(txn).WithFlags(tree.SchemaLookupFlags{ AvoidLeased: true, IncludeDropped: true, IncludeOffline: true, - }) + }).Immutable().Schema(ctx, id) if err != nil { return err } diff --git a/pkg/ccl/changefeedccl/changefeed_stmt.go b/pkg/ccl/changefeedccl/changefeed_stmt.go index fd8c97ca3ad7..a18027697305 100644 --- a/pkg/ccl/changefeedccl/changefeed_stmt.go +++ b/pkg/ccl/changefeedccl/changefeed_stmt.go @@ -1186,7 +1186,7 @@ func getQualifiedTableNameObj( if err != nil { return tree.TableName{}, err } - sc, err := col.GetImmutableSchemaByID(ctx, txn, desc.GetParentSchemaID(), flags) + sc, err := col.ByID(txn).WithFlags(flags).Immutable().Schema(ctx, desc.GetParentSchemaID()) if err != nil { return tree.TableName{}, err } diff --git a/pkg/scheduledjobs/schedulebase/util.go b/pkg/scheduledjobs/schedulebase/util.go index d0eb93aaf5a7..436673b5c293 100644 --- a/pkg/scheduledjobs/schedulebase/util.go +++ b/pkg/scheduledjobs/schedulebase/util.go @@ -197,8 +197,7 @@ func FullyQualifyTables( } // Resolve the schema. - schemaDesc, err := col.GetImmutableSchemaByID(ctx, txn, tableDesc.GetParentSchemaID(), - tree.SchemaLookupFlags{Required: true}) + schemaDesc, err := col.ByID(txn).WithFlags(tree.SchemaLookupFlags{}).Immutable().Schema(ctx, tableDesc.GetParentSchemaID()) if err != nil { return err } diff --git a/pkg/server/status.go b/pkg/server/status.go index 953bc263aa39..9096a057d4f3 100644 --- a/pkg/server/status.go +++ b/pkg/server/status.go @@ -2497,7 +2497,7 @@ func (s *statusServer) HotRangesV2( dbName = dbDesc.GetName() } - if schemaDesc, err := col.GetImmutableSchemaByID(ctx, txn, desc.GetParentSchemaID(), commonLookupFlags); err != nil { + if schemaDesc, err := col.ByID(txn).WithFlags(commonLookupFlags).Immutable().Schema(ctx, desc.GetParentSchemaID()); err != nil { log.Warningf(ctx, "cannot get schema name for range descriptor: %s: %v", r.Desc, err) } else { schemaName = schemaDesc.GetName() diff --git a/pkg/sql/authorization.go b/pkg/sql/authorization.go index 8be56bdc9b77..20ac772d84e2 100644 --- a/pkg/sql/authorization.go +++ b/pkg/sql/authorization.go @@ -745,8 +745,7 @@ func (p *planner) canCreateOnSchema( user username.SQLUsername, checkPublicSchema shouldCheckPublicSchema, ) error { - scDesc, err := p.Descriptors().GetImmutableSchemaByID( - ctx, p.Txn(), schemaID, tree.SchemaLookupFlags{Required: true}) + scDesc, err := p.Descriptors().ByID(p.Txn()).WithFlags(tree.SchemaLookupFlags{}).Immutable().Schema(ctx, schemaID) if err != nil { return err } @@ -850,9 +849,7 @@ func (p *planner) HasOwnershipOnSchema( // Only the node user has ownership over the system database. return p.User().IsNodeUser(), nil } - scDesc, err := p.Descriptors().GetImmutableSchemaByID( - ctx, p.Txn(), schemaID, tree.SchemaLookupFlags{Required: true}, - ) + scDesc, err := p.Descriptors().ByID(p.Txn()).WithFlags(tree.SchemaLookupFlags{}).Immutable().Schema(ctx, schemaID) if err != nil { return false, err } diff --git a/pkg/sql/catalog/descs/schema.go b/pkg/sql/catalog/descs/schema.go index 29b14ece45b6..ec450a70ca3e 100644 --- a/pkg/sql/catalog/descs/schema.go +++ b/pkg/sql/catalog/descs/schema.go @@ -53,16 +53,6 @@ func (tc *Collection) GetImmutableSchemaByName( return tc.ByName(txn).WithFlags(flags).Immutable().Schema(ctx, db, scName) } -// GetImmutableSchemaByID returns a ResolvedSchema wrapping an immutable -// descriptor, if applicable. RequireMutable is ignored. -// Required is ignored, and an error is always returned if no descriptor with -// the ID exists. -func (tc *Collection) GetImmutableSchemaByID( - ctx context.Context, txn *kv.Txn, schemaID descpb.ID, flags tree.SchemaLookupFlags, -) (catalog.SchemaDescriptor, error) { - return tc.ByID(txn).WithFlags(flags).Immutable().Schema(ctx, schemaID) -} - // GetMutableSchemaByID returns a mutable schema descriptor with the given // schema ID. An error is always returned if the descriptor is not physical. func (tc *Collection) GetMutableSchemaByID( diff --git a/pkg/sql/catalog/ingesting/privileges.go b/pkg/sql/catalog/ingesting/privileges.go index 60de80fc66da..1fb9afe8c731 100644 --- a/pkg/sql/catalog/ingesting/privileges.go +++ b/pkg/sql/catalog/ingesting/privileges.go @@ -145,11 +145,11 @@ func getIngestingPrivilegesForTableOrSchema( } else { // If we are restoring into an existing schema, resolve it, and fetch // its default privileges. - parentSchema, err := descsCol.GetImmutableSchemaByID(ctx, txn, desc.GetParentSchemaID(), tree.SchemaLookupFlags{ + parentSchema, err := descsCol.ByID(txn).WithFlags(tree.SchemaLookupFlags{ AvoidLeased: true, IncludeDropped: true, IncludeOffline: true, - }) + }).Immutable().Schema(ctx, desc.GetParentSchemaID()) if err != nil { return nil, errors.Wrapf(err, "failed to lookup parent schema %d", errors.Safe(desc.GetParentSchemaID())) diff --git a/pkg/sql/crdb_internal.go b/pkg/sql/crdb_internal.go index 3979a731294e..5fb737313578 100644 --- a/pkg/sql/crdb_internal.go +++ b/pkg/sql/crdb_internal.go @@ -7097,7 +7097,7 @@ func convertContentionEventsToJSON( return nil, err } - schemaDesc, err := desc.GetImmutableSchemaByID(ctx, p.txn, tableDesc.GetParentSchemaID(), tree.SchemaLookupFlags{}) + schemaDesc, err := desc.ByID(p.txn).WithFlags(tree.SchemaLookupFlags{}).Immutable().Schema(ctx, tableDesc.GetParentSchemaID()) if err != nil { return nil, err } diff --git a/pkg/sql/create_schema.go b/pkg/sql/create_schema.go index 74fb13df2096..19543c81ec47 100644 --- a/pkg/sql/create_schema.go +++ b/pkg/sql/create_schema.go @@ -82,12 +82,11 @@ func CreateUserDefinedSchemaDescriptor( // and can't be in a dropping state. if schemaID != descpb.InvalidID { // Check if the object already exists in a dropped state - sc, err := descriptors.GetImmutableSchemaByID(ctx, txn, schemaID, tree.SchemaLookupFlags{ - Required: true, + sc, err := descriptors.ByID(txn).WithFlags(tree.SchemaLookupFlags{ AvoidLeased: true, IncludeOffline: true, IncludeDropped: true, - }) + }).Immutable().Schema(ctx, schemaID) if err != nil || sc.SchemaKind() != catalog.SchemaUserDefined { return nil, nil, err } diff --git a/pkg/sql/importer/import_job.go b/pkg/sql/importer/import_job.go index 6bca3e35c0fc..5e25e27ed9b3 100644 --- a/pkg/sql/importer/import_job.go +++ b/pkg/sql/importer/import_job.go @@ -816,7 +816,7 @@ func getPublicSchemaDescForDatabase( ctx context.Context, txn *kv.Txn, descriptors *descs.Collection, ) error { publicSchemaID := db.GetSchemaID(tree.PublicSchema) - scDesc, err = descriptors.GetImmutableSchemaByID(ctx, txn, publicSchemaID, tree.SchemaLookupFlags{Required: true}) + scDesc, err = descriptors.ByID(txn).WithFlags(tree.SchemaLookupFlags{}).Immutable().Schema(ctx, publicSchemaID) return err }); err != nil { return nil, err diff --git a/pkg/sql/importer/import_stmt_test.go b/pkg/sql/importer/import_stmt_test.go index e9869a2308eb..4497f083f1fa 100644 --- a/pkg/sql/importer/import_stmt_test.go +++ b/pkg/sql/importer/import_stmt_test.go @@ -6418,11 +6418,11 @@ func TestImportPgDumpSchemas(t *testing.T) { for _, schemaID := range schemaIDs { // Expect that the schema descriptor is deleted. if err := sql.TestingDescsTxn(ctx, tc.Server(0), func(ctx context.Context, txn *kv.Txn, col *descs.Collection) error { - _, err := col.GetImmutableSchemaByID(ctx, txn, schemaID, tree.CommonLookupFlags{ + _, err := col.ByID(txn).WithFlags(tree.CommonLookupFlags{ AvoidLeased: true, IncludeDropped: true, IncludeOffline: true, - }) + }).Immutable().Schema(ctx, schemaID) if pgerror.GetPGCode(err) == pgcode.InvalidSchemaName { return nil } diff --git a/pkg/sql/information_schema.go b/pkg/sql/information_schema.go index 5c4f7fa75caa..28f8bbadb3f0 100644 --- a/pkg/sql/information_schema.go +++ b/pkg/sql/information_schema.go @@ -1673,7 +1673,7 @@ var informationSchemaRoleRoutineGrantsTable = virtualSchemaTable{ } err := db.ForEachSchema(func(id descpb.ID, name string) error { - sc, err := p.Descriptors().GetImmutableSchemaByID(ctx, p.txn, id, tree.SchemaLookupFlags{Required: true}) + sc, err := p.Descriptors().ByID(p.txn).WithFlags(tree.SchemaLookupFlags{}).Immutable().Schema(ctx, id) if err != nil { return err } diff --git a/pkg/sql/opt_catalog.go b/pkg/sql/opt_catalog.go index 5378f3294216..50bb9c7a482e 100644 --- a/pkg/sql/opt_catalog.go +++ b/pkg/sql/opt_catalog.go @@ -448,9 +448,7 @@ func (oc *optCatalog) fullyQualifiedNameWithTxn( if scID == keys.PublicSchemaID { scName = tree.PublicSchemaName } else { - scDesc, err := oc.planner.Descriptors().GetImmutableSchemaByID( - ctx, txn, scID, tree.SchemaLookupFlags{AvoidLeased: true}, - ) + scDesc, err := oc.planner.Descriptors().ByID(txn).WithFlags(tree.SchemaLookupFlags{AvoidLeased: true}).Immutable().Schema(ctx, scID) if err != nil { return cat.DataSourceName{}, err } diff --git a/pkg/sql/pg_catalog.go b/pkg/sql/pg_catalog.go index de7dcdb7efc7..473d1a4086ef 100644 --- a/pkg/sql/pg_catalog.go +++ b/pkg/sql/pg_catalog.go @@ -1076,11 +1076,11 @@ func (r oneAtATimeSchemaResolver) getTableByID(id descpb.ID) (catalog.TableDescr } func (r oneAtATimeSchemaResolver) getSchemaByID(id descpb.ID) (catalog.SchemaDescriptor, error) { - return r.p.Descriptors().GetImmutableSchemaByID(r.ctx, r.p.txn, id, tree.SchemaLookupFlags{ + return r.p.Descriptors().ByID(r.p.txn).WithFlags(tree.SchemaLookupFlags{ AvoidLeased: true, IncludeDropped: true, IncludeOffline: true, - }) + }).Immutable().Schema(r.ctx, id) } // makeAllRelationsVirtualTableWithDescriptorIDIndex creates a virtual table that searches through @@ -1148,10 +1148,7 @@ func makeAllRelationsVirtualTableWithDescriptorIDIndex( } h := makeOidHasher() scResolver := oneAtATimeSchemaResolver{p: p, ctx: ctx} - sc, err := p.Descriptors().GetImmutableSchemaByID( - ctx, p.txn, table.GetParentSchemaID(), tree.SchemaLookupFlags{ - Required: true, - }) + sc, err := p.Descriptors().ByID(p.txn).WithFlags(tree.SchemaLookupFlags{}).Immutable().Schema(ctx, table.GetParentSchemaID()) if err != nil { return false, err } @@ -1611,13 +1608,7 @@ https://www.postgresql.org/docs/9.5/catalog-pg-description.html`, if err != nil { return err } - schema, err := p.Descriptors().GetImmutableSchemaByID( - ctx, - p.txn, - tableDesc.GetParentSchemaID(), - tree.CommonLookupFlags{ - Required: true, - }) + schema, err := p.Descriptors().ByID(p.txn).WithFlags(tree.CommonLookupFlags{}).Immutable().Schema(ctx, tableDesc.GetParentSchemaID()) if err != nil { return err } @@ -2110,9 +2101,7 @@ https://www.postgresql.org/docs/9.5/catalog-pg-namespace.html`, if sc, ok := schemadesc.GetVirtualSchemaByID(descpb.ID(ooid)); ok { return sc, true, nil } - if sc, err := p.Descriptors().GetImmutableSchemaByID( - ctx, p.Txn(), descpb.ID(ooid), tree.SchemaLookupFlags{Required: true}, - ); err == nil { + if sc, err := p.Descriptors().ByID(p.Txn()).WithFlags(tree.SchemaLookupFlags{}).Immutable().Schema(ctx, descpb.ID(ooid)); err == nil { return sc, true, nil } else if !sqlerrors.IsUndefinedSchemaError(err) { return nil, false, err @@ -2610,9 +2599,7 @@ https://www.postgresql.org/docs/9.5/catalog-pg-proc.html`, return false, err } - scDesc, err := p.Descriptors().GetImmutableSchemaByID( - ctx, p.Txn(), descpb.ID(ooid), tree.SchemaLookupFlags{Required: true}, - ) + scDesc, err := p.Descriptors().ByID(p.Txn()).WithFlags(tree.SchemaLookupFlags{}).Immutable().Schema(ctx, descpb.ID(ooid)) if err != nil { return false, err } @@ -3218,12 +3205,7 @@ func getSchemaAndTypeByTypeID( return nil, nil, nil } - sc, err := p.Descriptors().GetImmutableSchemaByID( - ctx, - p.txn, - typDesc.GetParentSchemaID(), - tree.SchemaLookupFlags{Required: true}, - ) + sc, err := p.Descriptors().ByID(p.txn).WithFlags(tree.SchemaLookupFlags{}).Immutable().Schema(ctx, typDesc.GetParentSchemaID()) if err != nil { return nil, nil, err } diff --git a/pkg/sql/resolver.go b/pkg/sql/resolver.go index 84a47d9da399..becaf4c57d4b 100644 --- a/pkg/sql/resolver.go +++ b/pkg/sql/resolver.go @@ -650,9 +650,7 @@ func (p *planner) getQualifiedTypeName( } schemaID := desc.GetParentSchemaID() - scDesc, err := p.Descriptors().GetImmutableSchemaByID( - ctx, p.txn, schemaID, tree.SchemaLookupFlags{Required: true}, - ) + scDesc, err := p.Descriptors().ByID(p.txn).WithFlags(tree.SchemaLookupFlags{}).Immutable().Schema(ctx, schemaID) if err != nil { return nil, err } diff --git a/pkg/sql/schema_resolver.go b/pkg/sql/schema_resolver.go index 77d9a0be9288..e28ae3ab724f 100644 --- a/pkg/sql/schema_resolver.go +++ b/pkg/sql/schema_resolver.go @@ -256,13 +256,11 @@ func (sr *schemaResolver) getQualifiedTableName( // information from the namespace table. var schemaName tree.Name schemaID := desc.GetParentSchemaID() - scDesc, err := sr.descCollection.GetImmutableSchemaByID(ctx, sr.txn, schemaID, - tree.SchemaLookupFlags{ - Required: true, - IncludeOffline: true, - IncludeDropped: true, - AvoidLeased: true, - }) + scDesc, err := sr.descCollection.ByID(sr.txn).WithFlags(tree.SchemaLookupFlags{ + IncludeOffline: true, + IncludeDropped: true, + AvoidLeased: true, + }).Immutable().Schema(ctx, schemaID) switch { case scDesc != nil: schemaName = tree.Name(scDesc.GetName()) @@ -300,7 +298,7 @@ func (sr *schemaResolver) getQualifiedFunctionName( if err != nil { return nil, err } - scDesc, err := sr.descCollection.GetImmutableSchemaByID(ctx, sr.txn, fnDesc.GetParentSchemaID(), lookupFlags) + scDesc, err := sr.descCollection.ByID(sr.txn).WithFlags(lookupFlags).Immutable().Schema(ctx, fnDesc.GetParentSchemaID()) if err != nil { return nil, err } @@ -378,7 +376,7 @@ func (sr *schemaResolver) GetTypeDescriptor( } dbName = db.GetName() } - sc, err := tc.GetImmutableSchemaByID(ctx, sr.txn, desc.GetParentSchemaID(), flags) + sc, err := tc.ByID(sr.txn).WithFlags(flags).Immutable().Schema(ctx, desc.GetParentSchemaID()) if err != nil { return tree.TypeName{}, nil, err } diff --git a/pkg/sql/schemachanger/scdeps/exec_deps.go b/pkg/sql/schemachanger/scdeps/exec_deps.go index 344b50d81241..b92305c562e9 100644 --- a/pkg/sql/schemachanger/scdeps/exec_deps.go +++ b/pkg/sql/schemachanger/scdeps/exec_deps.go @@ -161,9 +161,7 @@ func (d *txnDeps) GetFullyQualifiedName(ctx context.Context, id descpb.ID) (stri if err != nil { return "", err } - schemaDesc, err := d.descsCollection.GetImmutableSchemaByID( - ctx, d.txn, objectDesc.GetParentSchemaID(), flags, - ) + schemaDesc, err := d.descsCollection.ByID(d.txn).WithFlags(flags).Immutable().Schema(ctx, objectDesc.GetParentSchemaID()) if err != nil { return "", err } diff --git a/pkg/sql/temporary_schema.go b/pkg/sql/temporary_schema.go index 2016719e1c0a..dfa8cfd64985 100644 --- a/pkg/sql/temporary_schema.go +++ b/pkg/sql/temporary_schema.go @@ -122,7 +122,7 @@ func (p *planner) getOrCreateTemporarySchema( m.SetTemporarySchemaName(tempSchemaName) m.SetTemporarySchemaIDForDatabase(uint32(db.GetID()), uint32(id)) }) - return p.Descriptors().GetImmutableSchemaByID(ctx, p.Txn(), id, p.CommonLookupFlagsRequired()) + return p.Descriptors().ByID(p.Txn()).WithFlags(p.CommonLookupFlagsRequired()).Immutable().Schema(ctx, id) } // temporarySchemaName returns the session specific temporary schema name given @@ -317,7 +317,7 @@ func cleanupTempSchemaObjects( if err != nil { return err } - sc, err := descsCol.GetImmutableSchemaByID(ctx, txn, dTableDesc.GetParentSchemaID(), flags) + sc, err := descsCol.ByID(txn).WithFlags(flags).Immutable().Schema(ctx, dTableDesc.GetParentSchemaID()) if err != nil { return err } diff --git a/pkg/sql/type_change.go b/pkg/sql/type_change.go index 76193ddab228..6292c7d97835 100644 --- a/pkg/sql/type_change.go +++ b/pkg/sql/type_change.go @@ -1164,8 +1164,7 @@ func (t *typeSchemaChanger) canRemoveEnumValueFromArrayUsages( } if len(rows) > 0 { // Use an FQN in the error message. - parentSchema, err := descsCol.GetImmutableSchemaByID( - ctx, txn, desc.GetParentSchemaID(), tree.SchemaLookupFlags{Required: true}) + parentSchema, err := descsCol.ByID(txn).WithFlags(tree.SchemaLookupFlags{}).Immutable().Schema(ctx, desc.GetParentSchemaID()) if err != nil { return err }