Skip to content

Commit

Permalink
sql: inline GetImmutableSchemaByID
Browse files Browse the repository at this point in the history
Informs cockroachdb#87753.

Release note: None
  • Loading branch information
Marius Posta committed Jan 4, 2023
1 parent 44db76e commit 9e61d7c
Show file tree
Hide file tree
Showing 20 changed files with 41 additions and 85 deletions.
12 changes: 5 additions & 7 deletions pkg/ccl/backupccl/restore_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/ccl/backupccl/restore_planning.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/ccl/changefeedccl/changefeed_stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/scheduledjobs/schedulebase/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 2 additions & 5 deletions pkg/sql/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
10 changes: 0 additions & 10 deletions pkg/sql/catalog/descs/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/catalog/ingesting/privileges.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/crdb_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/sql/create_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/importer/import_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/importer/import_stmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/information_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 1 addition & 3 deletions pkg/sql/opt_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
32 changes: 7 additions & 25 deletions pkg/sql/pg_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 1 addition & 3 deletions pkg/sql/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 7 additions & 9 deletions pkg/sql/schema_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 1 addition & 3 deletions pkg/sql/schemachanger/scdeps/exec_deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/temporary_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/sql/type_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 9e61d7c

Please sign in to comment.