diff --git a/pkg/bench/rttanalysis/testdata/benchmark_expectations b/pkg/bench/rttanalysis/testdata/benchmark_expectations index 275b9347b628..e89d38d54975 100644 --- a/pkg/bench/rttanalysis/testdata/benchmark_expectations +++ b/pkg/bench/rttanalysis/testdata/benchmark_expectations @@ -50,7 +50,7 @@ exp,benchmark 5,GenerateObjects/generate_50000_tables 5,GenerateObjects/generate_1000_tables_-_this_test_should_use_the_same_number_of_RTTs_as_for_10_tables 5,GenerateObjects/generate_10_tables -10,GenerateObjects/generate_100_tables_from_template +12,GenerateObjects/generate_100_tables_from_template 16,GenerateObjects/generate_10x10_schemas_and_tables_in_existing_db 9,Grant/grant_all_on_1_table 9,Grant/grant_all_on_2_tables diff --git a/pkg/ccl/backupccl/backup_test.go b/pkg/ccl/backupccl/backup_test.go index 126deb9a3352..1be4395726f4 100644 --- a/pkg/ccl/backupccl/backup_test.go +++ b/pkg/ccl/backupccl/backup_test.go @@ -10448,34 +10448,30 @@ $$; udfID, err := strconv.Atoi(rows[0][0]) require.NoError(t, err) err = sql.TestingDescsTxn(ctx, srcServer, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) error { - dbDesc, err := col.GetImmutableDatabaseByName(ctx, txn, "db1", tree.DatabaseLookupFlags{Required: true}) + dbDesc, err := col.ByNameWithLeased(txn).Get().Database(ctx, "db1") require.NoError(t, err) require.Equal(t, 104, int(dbDesc.GetID())) - scDesc, err := col.GetImmutableSchemaByName(ctx, txn, dbDesc, "sc1", tree.SchemaLookupFlags{Required: true}) + scDesc, err := col.ByNameWithLeased(txn).Get().Schema(ctx, dbDesc, "sc1") require.NoError(t, err) require.Equal(t, 106, int(scDesc.GetID())) tbName := tree.MakeTableNameWithSchema("db1", "sc1", "tbl1") - _, tbDesc, err := col.GetImmutableTableByName( - ctx, txn, &tbName, tree.ObjectLookupFlagsWithRequired(), - ) + _, tbDesc, err := descs.PrefixAndTable(ctx, col.ByNameWithLeased(txn).Get(), &tbName) require.NoError(t, err) require.Equal(t, 107, int(tbDesc.GetID())) typName := tree.MakeQualifiedTypeName("db1", "sc1", "enum1") - _, typDesc, err := col.GetImmutableTypeByName(ctx, txn, &typName, tree.ObjectLookupFlagsWithRequired()) + _, typDesc, err := descs.PrefixAndType(ctx, col.ByNameWithLeased(txn).Get(), &typName) require.NoError(t, err) require.Equal(t, 108, int(typDesc.GetID())) tbName = tree.MakeTableNameWithSchema("db1", "sc1", "sq1") - _, tbDesc, err = col.GetImmutableTableByName( - ctx, txn, &tbName, tree.ObjectLookupFlagsWithRequired(), - ) + _, tbDesc, err = descs.PrefixAndTable(ctx, col.ByNameWithLeased(txn).Get(), &tbName) require.NoError(t, err) require.Equal(t, 110, int(tbDesc.GetID())) - fnDesc, err := col.GetImmutableFunctionByID(ctx, txn, descpb.ID(udfID), tree.ObjectLookupFlagsWithRequired()) + fnDesc, err := col.ByIDWithLeased(txn).WithoutNonPublic().Get().Function(ctx, descpb.ID(udfID)) require.NoError(t, err) require.Equal(t, 111, int(fnDesc.GetID())) require.Equal(t, 104, int(fnDesc.GetParentID())) @@ -10504,34 +10500,30 @@ $$; udfID, err = strconv.Atoi(rows[0][0]) require.NoError(t, err) err = sql.TestingDescsTxn(ctx, srcServer, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) error { - dbDesc, err := col.GetImmutableDatabaseByName(ctx, txn, "db1_new", tree.DatabaseLookupFlags{Required: true}) + dbDesc, err := col.ByNameWithLeased(txn).Get().Database(ctx, "db1_new") require.NoError(t, err) require.Equal(t, 112, int(dbDesc.GetID())) - scDesc, err := col.GetImmutableSchemaByName(ctx, txn, dbDesc, "sc1", tree.SchemaLookupFlags{Required: true}) + scDesc, err := col.ByNameWithLeased(txn).Get().Schema(ctx, dbDesc, "sc1") require.NoError(t, err) require.Equal(t, 114, int(scDesc.GetID())) tbName := tree.MakeTableNameWithSchema("db1_new", "sc1", "tbl1") - _, tbDesc, err := col.GetImmutableTableByName( - ctx, txn, &tbName, tree.ObjectLookupFlagsWithRequired(), - ) + _, tbDesc, err := descs.PrefixAndTable(ctx, col.ByNameWithLeased(txn).Get(), &tbName) require.NoError(t, err) require.Equal(t, 115, int(tbDesc.GetID())) typName := tree.MakeQualifiedTypeName("db1_new", "sc1", "enum1") - _, typDesc, err := col.GetImmutableTypeByName(ctx, txn, &typName, tree.ObjectLookupFlagsWithRequired()) + _, typDesc, err := descs.PrefixAndType(ctx, col.ByNameWithLeased(txn).Get(), &typName) require.NoError(t, err) require.Equal(t, 116, int(typDesc.GetID())) tbName = tree.MakeTableNameWithSchema("db1_new", "sc1", "sq1") - _, tbDesc, err = col.GetImmutableTableByName( - ctx, txn, &tbName, tree.ObjectLookupFlagsWithRequired(), - ) + _, tbDesc, err = descs.PrefixAndTable(ctx, col.ByNameWithLeased(txn).Get(), &tbName) require.NoError(t, err) require.Equal(t, 118, int(tbDesc.GetID())) - fnDesc, err := col.GetImmutableFunctionByID(ctx, txn, descpb.ID(udfID), tree.ObjectLookupFlagsWithRequired()) + fnDesc, err := col.ByIDWithLeased(txn).WithoutNonPublic().Get().Function(ctx, descpb.ID(udfID)) require.NoError(t, err) require.Equal(t, 119, int(fnDesc.GetID())) require.Equal(t, 112, int(fnDesc.GetParentID())) @@ -10595,34 +10587,30 @@ $$; udfID, err := strconv.Atoi(rows[0][0]) require.NoError(t, err) err = sql.TestingDescsTxn(ctx, srcServer, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) error { - dbDesc, err := col.GetImmutableDatabaseByName(ctx, txn, "db1", tree.DatabaseLookupFlags{Required: true}) + dbDesc, err := col.ByNameWithLeased(txn).Get().Database(ctx, "db1") require.NoError(t, err) require.Equal(t, 104, int(dbDesc.GetID())) - scDesc, err := col.GetImmutableSchemaByName(ctx, txn, dbDesc, "sc1", tree.SchemaLookupFlags{Required: true}) + scDesc, err := col.ByNameWithLeased(txn).Get().Schema(ctx, dbDesc, "sc1") require.NoError(t, err) require.Equal(t, 106, int(scDesc.GetID())) tbName := tree.MakeTableNameWithSchema("db1", "sc1", "tbl1") - _, tbDesc, err := col.GetImmutableTableByName( - ctx, txn, &tbName, tree.ObjectLookupFlagsWithRequired(), - ) + _, tbDesc, err := descs.PrefixAndTable(ctx, col.ByNameWithLeased(txn).Get(), &tbName) require.NoError(t, err) require.Equal(t, 107, int(tbDesc.GetID())) typName := tree.MakeQualifiedTypeName("db1", "sc1", "enum1") - _, typDesc, err := col.GetImmutableTypeByName(ctx, txn, &typName, tree.ObjectLookupFlagsWithRequired()) + _, typDesc, err := descs.PrefixAndType(ctx, col.ByNameWithLeased(txn).Get(), &typName) require.NoError(t, err) require.Equal(t, 108, int(typDesc.GetID())) tbName = tree.MakeTableNameWithSchema("db1", "sc1", "sq1") - _, tbDesc, err = col.GetImmutableTableByName( - ctx, txn, &tbName, tree.ObjectLookupFlagsWithRequired(), - ) + _, tbDesc, err = descs.PrefixAndTable(ctx, col.ByNameWithLeased(txn).Get(), &tbName) require.NoError(t, err) require.Equal(t, 110, int(tbDesc.GetID())) - fnDesc, err := col.GetImmutableFunctionByID(ctx, txn, descpb.ID(udfID), tree.ObjectLookupFlagsWithRequired()) + fnDesc, err := col.ByIDWithLeased(txn).WithoutNonPublic().Get().Function(ctx, descpb.ID(udfID)) require.NoError(t, err) require.Equal(t, 111, int(fnDesc.GetID())) require.Equal(t, 104, int(fnDesc.GetParentID())) @@ -10653,34 +10641,30 @@ $$; udfID, err = strconv.Atoi(rows[0][0]) require.NoError(t, err) err = sql.TestingDescsTxn(ctx, tgtServer, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) error { - dbDesc, err := col.GetImmutableDatabaseByName(ctx, txn, "db1", tree.DatabaseLookupFlags{Required: true}) + dbDesc, err := col.ByNameWithLeased(txn).Get().Database(ctx, "db1") require.NoError(t, err) require.Equal(t, 107, int(dbDesc.GetID())) - scDesc, err := col.GetImmutableSchemaByName(ctx, txn, dbDesc, "sc1", tree.SchemaLookupFlags{Required: true}) + scDesc, err := col.ByNameWithLeased(txn).Get().Schema(ctx, dbDesc, "sc1") require.NoError(t, err) require.Equal(t, 125, int(scDesc.GetID())) tbName := tree.MakeTableNameWithSchema("db1", "sc1", "tbl1") - _, tbDesc, err := col.GetImmutableTableByName( - ctx, txn, &tbName, tree.ObjectLookupFlagsWithRequired(), - ) + _, tbDesc, err := descs.PrefixAndTable(ctx, col.ByNameWithLeased(txn).Get(), &tbName) require.NoError(t, err) require.Equal(t, 126, int(tbDesc.GetID())) typName := tree.MakeQualifiedTypeName("db1", "sc1", "enum1") - _, typDesc, err := col.GetImmutableTypeByName(ctx, txn, &typName, tree.ObjectLookupFlagsWithRequired()) + _, typDesc, err := descs.PrefixAndType(ctx, col.ByNameWithLeased(txn).Get(), &typName) require.NoError(t, err) require.Equal(t, 127, int(typDesc.GetID())) tbName = tree.MakeTableNameWithSchema("db1", "sc1", "sq1") - _, tbDesc, err = col.GetImmutableTableByName( - ctx, txn, &tbName, tree.ObjectLookupFlagsWithRequired(), - ) + _, tbDesc, err = descs.PrefixAndTable(ctx, col.ByNameWithLeased(txn).Get(), &tbName) require.NoError(t, err) require.Equal(t, 129, int(tbDesc.GetID())) - fnDesc, err := col.GetImmutableFunctionByID(ctx, txn, descpb.ID(udfID), tree.ObjectLookupFlagsWithRequired()) + fnDesc, err := col.ByIDWithLeased(txn).WithoutNonPublic().Get().Function(ctx, descpb.ID(udfID)) require.NoError(t, err) require.Equal(t, 130, int(fnDesc.GetID())) require.Equal(t, 107, int(fnDesc.GetParentID())) diff --git a/pkg/ccl/backupccl/restore_job.go b/pkg/ccl/backupccl/restore_job.go index 366bcdbfbe30..aa5883605116 100644 --- a/pkg/ccl/backupccl/restore_job.go +++ b/pkg/ccl/backupccl/restore_job.go @@ -1073,7 +1073,7 @@ func createImportingDescriptors( // Write the updated databases. for dbID, schemas := range existingDBsWithNewSchemas { log.Infof(ctx, "writing %d schema entries to database %d", len(schemas), dbID) - desc, err := descsCol.GetMutableDescriptorByID(ctx, txn, dbID) + desc, err := descsCol.MutableByID(txn).Desc(ctx, dbID) if err != nil { return err } @@ -1093,12 +1093,7 @@ func createImportingDescriptors( // to the new tables being restored. for _, table := range mutableTables { // Collect all types used by this table. - _, dbDesc, err := descsCol.GetImmutableDatabaseByID( - ctx, txn, table.GetParentID(), tree.DatabaseLookupFlags{ - Required: true, - AvoidLeased: true, - IncludeOffline: true, - }) + dbDesc, err := descsCol.ByID(txn).WithoutDropped().Get().Database(ctx, table.GetParentID()) if err != nil { return err } @@ -1120,7 +1115,7 @@ func createImportingDescriptors( continue } // Otherwise, add a backreference to this table. - typDesc, err := descsCol.GetMutableTypeVersionByID(ctx, txn, id) + typDesc, err := descsCol.MutableByID(txn).Type(ctx, id) if err != nil { return err } @@ -1144,16 +1139,7 @@ func createImportingDescriptors( if details.DescriptorCoverage != tree.AllDescriptors { for _, table := range tableDescs { if lc := table.GetLocalityConfig(); lc != nil { - _, desc, err := descsCol.GetImmutableDatabaseByID( - ctx, - txn, - table.ParentID, - tree.DatabaseLookupFlags{ - Required: true, - AvoidLeased: true, - IncludeOffline: true, - }, - ) + desc, err := descsCol.ByID(txn).WithoutDropped().Get().Database(ctx, table.ParentID) if err != nil { return err } @@ -1163,7 +1149,7 @@ func createImportingDescriptors( table.ID, table.ParentID) } - mutTable, err := descsCol.GetMutableTableVersionByID(ctx, table.GetID(), txn) + mutTable, err := descsCol.MutableByID(txn).Table(ctx, table.GetID()) if err != nil { return err } @@ -2242,7 +2228,7 @@ func prefetchDescriptors( // and we're going to write them to KV very soon as part of a // single batch). ids := allDescIDs.Ordered() - got, err := descsCol.GetMutableDescriptorsByID(ctx, txn, ids...) + got, err := descsCol.MutableByID(txn).Descs(ctx, ids) if err != nil { return nstree.Catalog{}, errors.Wrap(err, "prefetch descriptors") } @@ -2374,7 +2360,7 @@ func (r *restoreResumer) dropDescriptors( mutableTables := make([]*tabledesc.Mutable, len(details.TableDescs)) for i := range details.TableDescs { var err error - mutableTables[i], err = descsCol.GetMutableTableVersionByID(ctx, details.TableDescs[i].ID, txn) + mutableTables[i], err = descsCol.MutableByID(txn).Table(ctx, details.TableDescs[i].ID) if err != nil { return err } @@ -2453,12 +2439,7 @@ func (r *restoreResumer) dropDescriptors( // TypeDescriptors don't have a GC job process, so we can just write them // as dropped here. typDesc := details.TypeDescs[i] - mutType, err := descsCol.GetMutableTypeByID(ctx, txn, typDesc.ID, tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - AvoidLeased: true, - IncludeOffline: true, - }, - }) + mutType, err := descsCol.MutableByID(txn).Type(ctx, typDesc.ID) if err != nil { return err } @@ -2474,12 +2455,7 @@ func (r *restoreResumer) dropDescriptors( for i := range details.FunctionDescs { fnDesc := details.FunctionDescs[i] - mutFn, err := descsCol.GetMutableFunctionByID(ctx, txn, fnDesc.ID, tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - AvoidLeased: true, - IncludeOffline: true, - }, - }) + mutFn, err := descsCol.MutableByID(txn).Function(ctx, fnDesc.ID) if err != nil { return err } @@ -2551,13 +2527,13 @@ func (r *restoreResumer) dropDescriptors( continue } - mutSchema, err := descsCol.GetMutableDescriptorByID(ctx, txn, schemaDesc.GetID()) + mutSchema, err := descsCol.MutableByID(txn).Desc(ctx, schemaDesc.GetID()) if err != nil { return err } entry, hasEntry := dbsWithDeletedSchemas[schemaDesc.GetParentID()] if !hasEntry { - mutParent, err := descsCol.GetMutableDescriptorByID(ctx, txn, schemaDesc.GetParentID()) + mutParent, err := descsCol.MutableByID(txn).Desc(ctx, schemaDesc.GetParentID()) if err != nil { return err } @@ -2628,7 +2604,7 @@ func (r *restoreResumer) dropDescriptors( continue } - db, err := descsCol.GetMutableDescriptorByID(ctx, txn, dbDesc.GetID()) + db, err := descsCol.MutableByID(txn).Desc(ctx, dbDesc.GetID()) if err != nil { return err } @@ -2682,24 +2658,12 @@ func setGCTTLForDroppingTable( log.VInfof(ctx, 2, "lowering TTL for table %q (%d)", tableToDrop.GetName(), tableToDrop.GetID()) // We get a mutable descriptor here because we are going to construct a // synthetic descriptor collection in which they are online. - _, dbDesc, err := descsCol.GetImmutableDatabaseByID(ctx, txn, tableToDrop.GetParentID(), - tree.DatabaseLookupFlags{ - Required: true, - IncludeOffline: true, - IncludeDropped: true, - AvoidLeased: true, - }) + dbDesc, err := descsCol.ByID(txn).Get().Database(ctx, tableToDrop.GetParentID()) if err != nil { 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).Get().Schema(ctx, tableToDrop.GetParentSchemaID()) if err != nil { return err } @@ -2758,7 +2722,7 @@ func (r *restoreResumer) removeExistingTypeBackReferences( return restored, nil } // Finally, look it up using the transaction. - typ, err := descsCol.GetMutableTypeVersionByID(ctx, txn, id) + typ, err := descsCol.MutableByID(txn).Type(ctx, id) if err != nil { return nil, err } @@ -2766,12 +2730,7 @@ func (r *restoreResumer) removeExistingTypeBackReferences( return typ, nil } - _, dbDesc, err := descsCol.GetImmutableDatabaseByID( - ctx, txn, tbl.GetParentID(), tree.DatabaseLookupFlags{ - Required: true, - AvoidLeased: true, - IncludeOffline: true, - }) + dbDesc, err := descsCol.ByID(txn).WithoutDropped().Get().Database(ctx, tbl.GetParentID()) if err != nil { return err } diff --git a/pkg/ccl/backupccl/restore_planning.go b/pkg/ccl/backupccl/restore_planning.go index 8575e49b050d..06ea10fa7796 100644 --- a/pkg/ccl/backupccl/restore_planning.go +++ b/pkg/ccl/backupccl/restore_planning.go @@ -332,11 +332,7 @@ 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{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - }) + desc, err := col.ByID(txn).Get().Schema(ctx, id) if err != nil { return err } @@ -385,11 +381,7 @@ func allocateDescriptorRewrites( } // Check privileges. - _, parentDB, err := col.GetImmutableDatabaseByID(ctx, txn, parentID, tree.DatabaseLookupFlags{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - }) + parentDB, err := col.ByID(txn).Get().Database(ctx, parentID) if err != nil { return errors.Wrapf(err, "failed to lookup parent DB %d", errors.Safe(parentID)) @@ -454,11 +446,7 @@ func allocateDescriptorRewrites( targetDB, typ.Name) } // Check privileges on the parent DB. - _, parentDB, err := col.GetImmutableDatabaseByID(ctx, txn, parentID, tree.DatabaseLookupFlags{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - }) + parentDB, err := col.ByID(txn).Get().Database(ctx, parentID) if err != nil { return errors.Wrapf(err, "failed to lookup parent DB %d", errors.Safe(parentID)) @@ -706,11 +694,7 @@ func getDatabaseIDAndDesc( return dbID, nil, errors.Errorf("a database named %q needs to exist", targetDB) } // Check privileges on the parent DB. - _, dbDesc, err = col.GetImmutableDatabaseByID(ctx, txn, dbID, tree.DatabaseLookupFlags{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - }) + dbDesc, err = col.ByID(txn).Get().Database(ctx, dbID) if err != nil { return 0, nil, errors.Wrapf(err, "failed to lookup parent DB %d", errors.Safe(dbID)) @@ -2050,7 +2034,7 @@ func renameTargetDatabaseDescriptor( } db, ok := restoreDBs[0].(*dbdesc.Mutable) if !ok { - return errors.AssertionFailedf("expected *dbdesc.Mutable but found %T", db) + return errors.AssertionFailedf("expected *dbdesc.mutable but found %T", db) } db.SetName(newDBName) return nil diff --git a/pkg/ccl/backupccl/system_schema.go b/pkg/ccl/backupccl/system_schema.go index fd83579fc9b3..11eb9a450ed9 100644 --- a/pkg/ccl/backupccl/system_schema.go +++ b/pkg/ccl/backupccl/system_schema.go @@ -655,7 +655,7 @@ func GetSystemTableIDsToExcludeFromClusterBackup( if backupConfig.shouldIncludeInClusterBackup == optOutOfClusterBackup { err := sql.DescsTxn(ctx, execCfg, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) error { tn := tree.MakeTableNameWithSchema("system", tree.PublicSchemaName, tree.Name(systemTableName)) - found, desc, err := col.GetImmutableTableByName(ctx, txn, &tn, tree.ObjectLookupFlags{}) + _, desc, err := descs.PrefixAndTable(ctx, col.ByNameWithLeased(txn).MaybeGet(), &tn) isNotFoundErr := errors.Is(err, catalog.ErrDescriptorNotFound) if err != nil && !isNotFoundErr { return err @@ -664,7 +664,7 @@ func GetSystemTableIDsToExcludeFromClusterBackup( // Some system tables are not present when running inside a secondary // tenant egs: `systemschema.TenantsTable`. In such situations we are // print a warning and move on. - if !found || isNotFoundErr { + if desc == nil || isNotFoundErr { log.Warningf(ctx, "could not find system table descriptor %q", systemTableName) return nil } diff --git a/pkg/ccl/backupccl/targets.go b/pkg/ccl/backupccl/targets.go index 8366474d15d1..cb6b1c9de4f7 100644 --- a/pkg/ccl/backupccl/targets.go +++ b/pkg/ccl/backupccl/targets.go @@ -595,13 +595,7 @@ func checkMultiRegionCompatible( // For REGION BY TABLE IN tables, allow the restore if the // database has the region. regionEnumID := database.GetRegionConfig().RegionEnumID - regionEnum, err := col.GetImmutableTypeByID(ctx, txn, regionEnumID, tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - }, - }) + regionEnum, err := col.ByID(txn).Get().Type(ctx, regionEnumID) if err != nil { return err } diff --git a/pkg/ccl/changefeedccl/cdcevent/rowfetcher_cache.go b/pkg/ccl/changefeedccl/cdcevent/rowfetcher_cache.go index 3a6171dd8e8f..7e7e2ac3cb48 100644 --- a/pkg/ccl/changefeedccl/cdcevent/rowfetcher_cache.go +++ b/pkg/ccl/changefeedccl/cdcevent/rowfetcher_cache.go @@ -106,7 +106,7 @@ func refreshUDT( if err != nil { return err } - tableDesc, err = collection.GetImmutableTableByID(ctx, txn, tableID, tree.ObjectLookupFlags{}) + tableDesc, err = collection.ByIDWithLeased(txn).WithoutNonPublic().Get().Table(ctx, tableID) return err }); err != nil { // Manager can return all kinds of errors during chaos, but based on @@ -189,7 +189,7 @@ func (c *rowFetcherCache) RowFetcherForColumnFamily( // version and the desired version to use the cache. It is safe to use // UserDefinedTypeColsHaveSameVersion if we have a hit because we are // guaranteed that the tables have the same version. Additionally, these - // fetchers are always initialized with a single tabledesc.Immutable. + // fetchers are always initialized with a single tabledesc.Get. if safe, err := catalog.UserDefinedTypeColsInFamilyHaveSameVersion(tableDesc, f.tableDesc, family); err != nil { return nil, nil, err } else if safe { diff --git a/pkg/ccl/changefeedccl/cdctest/row.go b/pkg/ccl/changefeedccl/cdctest/row.go index f91f987abee7..7a0137a3ed54 100644 --- a/pkg/ccl/changefeedccl/cdctest/row.go +++ b/pkg/ccl/changefeedccl/cdctest/row.go @@ -101,19 +101,11 @@ func GetHydratedTableDescriptor( }() execCfg := execCfgI.(sql.ExecutorConfig) - var found bool require.NoError(t, sql.DescsTxn(context.Background(), &execCfg, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) (err error) { - found, td, err = col.GetImmutableTableByName(ctx, txn, - tree.NewTableNameWithSchema(dbName, scName, tableName), - tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - Required: true, - AvoidLeased: true, - }, - }) + _, td, err = descs.PrefixAndTable(ctx, col.ByName(txn).Get(), tree.NewTableNameWithSchema(dbName, scName, tableName)) return err })) - require.True(t, found) + require.NotNil(t, td) return td } diff --git a/pkg/ccl/changefeedccl/changefeed_dist.go b/pkg/ccl/changefeedccl/changefeed_dist.go index ec562bd8f5da..bc4c60366524 100644 --- a/pkg/ccl/changefeedccl/changefeed_dist.go +++ b/pkg/ccl/changefeedccl/changefeed_dist.go @@ -148,9 +148,7 @@ func fetchTableDescriptors( // and lie within the primary index span. Deduplication is important // here as requesting the same span twice will deadlock. return targets.EachTableID(func(id catid.DescID) error { - flags := tree.ObjectLookupFlagsWithRequired() - flags.AvoidLeased = true - tableDesc, err := descriptors.GetImmutableTableByID(ctx, txn, id, flags) + tableDesc, err := descriptors.ByID(txn).WithoutNonPublic().Get().Table(ctx, id) if err != nil { return err } diff --git a/pkg/ccl/changefeedccl/changefeed_stmt.go b/pkg/ccl/changefeedccl/changefeed_stmt.go index 30d491a09cc2..9c43e5907ac6 100644 --- a/pkg/ccl/changefeedccl/changefeed_stmt.go +++ b/pkg/ccl/changefeedccl/changefeed_stmt.go @@ -1177,16 +1177,11 @@ func getQualifiedTableNameObj( ctx context.Context, execCfg *sql.ExecutorConfig, txn *kv.Txn, desc catalog.TableDescriptor, ) (tree.TableName, error) { col := execCfg.CollectionFactory.NewCollection(ctx) - flags := tree.CommonLookupFlags{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - } - _, db, err := col.GetImmutableDatabaseByID(ctx, txn, desc.GetParentID(), flags) + db, err := col.ByID(txn).Get().Database(ctx, desc.GetParentID()) if err != nil { return tree.TableName{}, err } - sc, err := col.GetImmutableSchemaByID(ctx, txn, desc.GetParentSchemaID(), flags) + sc, err := col.ByID(txn).Get().Schema(ctx, desc.GetParentSchemaID()) if err != nil { return tree.TableName{}, err } diff --git a/pkg/ccl/changefeedccl/schemafeed/BUILD.bazel b/pkg/ccl/changefeedccl/schemafeed/BUILD.bazel index 47aa357a95dd..90aab901008e 100644 --- a/pkg/ccl/changefeedccl/schemafeed/BUILD.bazel +++ b/pkg/ccl/changefeedccl/schemafeed/BUILD.bazel @@ -28,7 +28,6 @@ go_library( "//pkg/sql/catalog/typedesc", "//pkg/sql/execinfra", "//pkg/sql/schemachanger/scpb", - "//pkg/sql/sem/tree", "//pkg/storage", "//pkg/util/contextutil", "//pkg/util/encoding", diff --git a/pkg/ccl/changefeedccl/schemafeed/schema_feed.go b/pkg/ccl/changefeedccl/schemafeed/schema_feed.go index 72346e9a3fc4..e30e6f34dfa4 100644 --- a/pkg/ccl/changefeedccl/schemafeed/schema_feed.go +++ b/pkg/ccl/changefeedccl/schemafeed/schema_feed.go @@ -28,7 +28,6 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog/lease" "github.com/cockroachdb/cockroach/pkg/sql/catalog/typedesc" "github.com/cockroachdb/cockroach/pkg/sql/execinfra" - "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/storage" "github.com/cockroachdb/cockroach/pkg/util/contextutil" "github.com/cockroachdb/cockroach/pkg/util/encoding" @@ -273,9 +272,7 @@ func (tf *schemaFeed) primeInitialTableDescs(ctx context.Context) error { } // Note that all targets are currently guaranteed to be tables. return tf.targets.EachTableID(func(id descpb.ID) error { - flags := tree.ObjectLookupFlagsWithRequired() - flags.AvoidLeased = true - tableDesc, err := descriptors.GetImmutableTableByID(ctx, txn, id, flags) + tableDesc, err := descriptors.ByID(txn).WithoutNonPublic().Get().Table(ctx, id) if err != nil { return err } diff --git a/pkg/ccl/multiregionccl/datadriven_test.go b/pkg/ccl/multiregionccl/datadriven_test.go index 4804a29f8ac8..2b76854eda31 100644 --- a/pkg/ccl/multiregionccl/datadriven_test.go +++ b/pkg/ccl/multiregionccl/datadriven_test.go @@ -797,9 +797,7 @@ func lookupTable(ec *sql.ExecutorConfig, database, table string) (catalog.TableD context.Background(), ec, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) error { - _, desc, err := col.GetImmutableTableByName(ctx, txn, tbName, tree.ObjectLookupFlags{ - DesiredObjectKind: tree.TableObject, - }) + _, desc, err := descs.PrefixAndTable(ctx, col.ByNameWithLeased(txn).MaybeGet(), tbName) if err != nil { return err } diff --git a/pkg/ccl/multiregionccl/region_util_test.go b/pkg/ccl/multiregionccl/region_util_test.go index b3ec84c6e291..2b767c39c002 100644 --- a/pkg/ccl/multiregionccl/region_util_test.go +++ b/pkg/ccl/multiregionccl/region_util_test.go @@ -20,7 +20,6 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descs" - "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/testutils/serverutils" "github.com/cockroachdb/cockroach/pkg/testutils/sqlutils" "github.com/cockroachdb/cockroach/pkg/util/leaktest" @@ -124,13 +123,11 @@ func getEnumMembers( t.Helper() enumMembers := make(map[string][]byte) err := sql.TestingDescsTxn(ctx, ts, func(ctx context.Context, txn *kv.Txn, descsCol *descs.Collection) error { - _, dbDesc, err := descsCol.GetImmutableDatabaseByID(ctx, txn, dbID, - tree.DatabaseLookupFlags{Required: true}) + dbDesc, err := descsCol.ByIDWithLeased(txn).WithoutNonPublic().Get().Database(ctx, dbID) require.NoError(t, err) regionEnumID, err := dbDesc.MultiRegionEnumID() require.NoError(t, err) - regionEnumDesc, err := descsCol.GetImmutableTypeByID(ctx, txn, regionEnumID, - tree.ObjectLookupFlags{CommonLookupFlags: tree.CommonLookupFlags{Required: true}}) + regionEnumDesc, err := descsCol.ByIDWithLeased(txn).WithoutNonPublic().Get().Type(ctx, regionEnumID) require.NoError(t, err) for ord := 0; ord < regionEnumDesc.NumEnumMembers(); ord++ { enumMembers[regionEnumDesc.GetMemberLogicalRepresentation(ord)] = regionEnumDesc.GetMemberPhysicalRepresentation(ord) diff --git a/pkg/scheduledjobs/schedulebase/util.go b/pkg/scheduledjobs/schedulebase/util.go index 9cb02051065d..7c7ff9ce6b2b 100644 --- a/pkg/scheduledjobs/schedulebase/util.go +++ b/pkg/scheduledjobs/schedulebase/util.go @@ -191,18 +191,13 @@ func FullyQualifyTables( } // Resolve the database. - found, dbDesc, err := col.GetImmutableDatabaseByID(ctx, txn, tableDesc.GetParentID(), - tree.DatabaseLookupFlags{Required: true}) + dbDesc, err := col.ByIDWithLeased(txn).WithoutNonPublic().Get().Database(ctx, tableDesc.GetParentID()) if err != nil { return err } - if !found { - return errors.Newf("database of target table %s could not be resolved", tp.String()) - } // Resolve the schema. - schemaDesc, err := col.GetImmutableSchemaByID(ctx, txn, tableDesc.GetParentSchemaID(), - tree.SchemaLookupFlags{Required: true}) + schemaDesc, err := col.ByIDWithLeased(txn).WithoutNonPublic().Get().Schema(ctx, tableDesc.GetParentSchemaID()) if err != nil { return err } @@ -227,8 +222,7 @@ func FullyQualifyTables( // database. var schemaID descpb.ID if err := sql.DescsTxn(ctx, p.ExecCfg(), func(ctx context.Context, txn *kv.Txn, col *descs.Collection) error { - flags := tree.DatabaseLookupFlags{Required: true} - dbDesc, err := col.GetImmutableDatabaseByName(ctx, txn, p.CurrentDatabase(), flags) + dbDesc, err := col.ByNameWithLeased(txn).Get().Database(ctx, p.CurrentDatabase()) if err != nil { return err } diff --git a/pkg/server/status.go b/pkg/server/status.go index 0d0c29277afe..f71b82a3eb41 100644 --- a/pkg/server/status.go +++ b/pkg/server/status.go @@ -62,7 +62,6 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/privilege" "github.com/cockroachdb/cockroach/pkg/sql/roleoption" "github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants" - "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sqlinstance" "github.com/cockroachdb/cockroach/pkg/sql/sqlstats/insights" "github.com/cockroachdb/cockroach/pkg/sql/sqlutil" @@ -2470,13 +2469,7 @@ func (s *statusServer) HotRangesV2( } else { if err = s.sqlServer.distSQLServer.InternalExecutorFactory.DescsTxnWithExecutor( ctx, s.db, nil, func(ctx context.Context, txn *kv.Txn, col *descs.Collection, ie sqlutil.InternalExecutor) error { - commonLookupFlags := tree.CommonLookupFlags{ - Required: false, - AvoidLeased: true, - } - desc, err := col.GetImmutableTableByID(ctx, txn, descpb.ID(tableID), tree.ObjectLookupFlags{ - CommonLookupFlags: commonLookupFlags, - }) + desc, err := col.ByID(txn).WithoutNonPublic().Get().Table(ctx, descpb.ID(tableID)) if err != nil { return errors.Wrapf(err, "cannot get table descriptor with tableID: %d, %s", tableID, r.Desc) } @@ -2494,13 +2487,13 @@ func (s *statusServer) HotRangesV2( } } - if ok, dbDesc, err := col.GetImmutableDatabaseByID(ctx, txn, desc.GetParentID(), commonLookupFlags); err != nil { + if dbDesc, err := col.ByID(txn).WithoutNonPublic().Get().Database(ctx, desc.GetParentID()); err != nil { log.Warningf(ctx, "cannot get database by descriptor ID: %s: %v", r.Desc, err) - } else if ok { + } else { dbName = dbDesc.GetName() } - if schemaDesc, err := col.GetImmutableSchemaByID(ctx, txn, desc.GetParentSchemaID(), commonLookupFlags); err != nil { + if schemaDesc, err := col.ByID(txn).WithoutNonPublic().Get().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/spanconfig/spanconfigreconciler/BUILD.bazel b/pkg/spanconfig/spanconfigreconciler/BUILD.bazel index 4ff015f80a00..93e7d0c00a54 100644 --- a/pkg/spanconfig/spanconfigreconciler/BUILD.bazel +++ b/pkg/spanconfig/spanconfigreconciler/BUILD.bazel @@ -18,7 +18,6 @@ go_library( "//pkg/sql/catalog", "//pkg/sql/catalog/descpb", "//pkg/sql/catalog/descs", - "//pkg/sql/sem/tree", "//pkg/sql/sqlliveness", "//pkg/sql/sqlutil", "//pkg/util/hlc", diff --git a/pkg/spanconfig/spanconfigreconciler/reconciler.go b/pkg/spanconfig/spanconfigreconciler/reconciler.go index db2c1bad9fcd..c290e70cf2c3 100644 --- a/pkg/spanconfig/spanconfigreconciler/reconciler.go +++ b/pkg/spanconfig/spanconfigreconciler/reconciler.go @@ -25,7 +25,6 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descs" - "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sqlliveness" "github.com/cockroachdb/cockroach/pkg/sql/sqlutil" "github.com/cockroachdb/cockroach/pkg/util/hlc" @@ -643,12 +642,7 @@ func (r *incrementalReconciler) filterForMissingTableIDs( continue // nothing to do } - desc, err := descsCol.GetImmutableDescriptorByID(ctx, txn, descriptorUpdate.ID, tree.CommonLookupFlags{ - Required: true, // we want to error out for missing descriptors - IncludeDropped: true, - IncludeOffline: true, - AvoidLeased: true, // we want consistent reads - }) + desc, err := descsCol.ByID(txn).Get().Desc(ctx, descriptorUpdate.ID) considerAsMissing := false if errors.Is(err, catalog.ErrDescriptorNotFound) { diff --git a/pkg/spanconfig/spanconfigsqltranslator/BUILD.bazel b/pkg/spanconfig/spanconfigsqltranslator/BUILD.bazel index 8b619ebcd433..ac617a79d49d 100644 --- a/pkg/spanconfig/spanconfigsqltranslator/BUILD.bazel +++ b/pkg/spanconfig/spanconfigsqltranslator/BUILD.bazel @@ -17,7 +17,6 @@ go_library( "//pkg/sql/catalog", "//pkg/sql/catalog/descpb", "//pkg/sql/catalog/descs", - "//pkg/sql/sem/tree", "//pkg/sql/sqlutil", "//pkg/util/hlc", "@com_github_cockroachdb_errors//:errors", diff --git a/pkg/spanconfig/spanconfigsqltranslator/sqltranslator.go b/pkg/spanconfig/spanconfigsqltranslator/sqltranslator.go index 9d9ebcc4dc91..8a082173ced7 100644 --- a/pkg/spanconfig/spanconfigsqltranslator/sqltranslator.go +++ b/pkg/spanconfig/spanconfigsqltranslator/sqltranslator.go @@ -26,7 +26,6 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descs" - "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sqlutil" "github.com/cockroachdb/cockroach/pkg/util/hlc" "github.com/cockroachdb/errors" @@ -180,18 +179,6 @@ func (s *SQLTranslator) Translate( return records, s.GetTxn().CommitTimestamp(), nil } -// descLookupFlags is the set of look up flags used when fetching descriptors. -var descLookupFlags = tree.CommonLookupFlags{ - // We act on errors being surfaced when the descriptor being looked up is - // not found. - Required: true, - // We can (do) generate span configurations for dropped and offline tables. - IncludeDropped: true, - IncludeOffline: true, - // We want consistent reads. - AvoidLeased: true, -} - // generateSystemSpanConfigRecords is responsible for generating all the SpanConfigs // that apply to spanconfig.SystemTargets. func (s *SQLTranslator) generateSystemSpanConfigRecords( @@ -261,7 +248,7 @@ func (s *SQLTranslator) generateSpanConfigurations( } // We're dealing with a SQL object. - desc, err := descsCol.GetImmutableDescriptorByID(ctx, txn, id, descLookupFlags) + desc, err := descsCol.ByID(txn).Get().Desc(ctx, id) if err != nil { if errors.Is(err, catalog.ErrDescriptorNotFound) { return nil, nil // the descriptor has been deleted; nothing to do here @@ -542,7 +529,7 @@ func (s *SQLTranslator) findDescendantLeafIDs( func (s *SQLTranslator) findDescendantLeafIDsForDescriptor( ctx context.Context, id descpb.ID, txn *kv.Txn, descsCol *descs.Collection, ) (descpb.IDs, error) { - desc, err := descsCol.GetImmutableDescriptorByID(ctx, txn, id, descLookupFlags) + desc, err := descsCol.ByID(txn).Get().Desc(ctx, id) if err != nil { if errors.Is(err, catalog.ErrDescriptorNotFound) { return nil, nil // the descriptor has been deleted; nothing to do here diff --git a/pkg/spanconfig/spanconfigtestutils/spanconfigtestcluster/tenant_state.go b/pkg/spanconfig/spanconfigtestutils/spanconfigtestcluster/tenant_state.go index 2aa3b1a96287..923874ed5faa 100644 --- a/pkg/spanconfig/spanconfigtestutils/spanconfigtestcluster/tenant_state.go +++ b/pkg/spanconfig/spanconfigtestutils/spanconfigtestcluster/tenant_state.go @@ -150,20 +150,16 @@ func (s *Tenant) WithMutableDatabaseDescriptor( require.NoError(s.t, sql.DescsTxn(ctx, &execCfg, func( ctx context.Context, txn *kv.Txn, descsCol *descs.Collection, ) error { - desc, err := descsCol.GetMutableDatabaseByName( - ctx, - txn, - dbName, - tree.DatabaseLookupFlags{ - Required: true, - IncludeOffline: true, - }, - ) + imm, err := descsCol.ByName(txn).WithOffline().Get().Database(ctx, dbName) if err != nil { return err } - f(desc) - return descsCol.WriteDesc(ctx, false, desc, txn) + mut, err := descsCol.MutableByID(txn).Database(ctx, imm.GetID()) + if err != nil { + return err + } + f(mut) + return descsCol.WriteDesc(ctx, false, mut, txn) })) } @@ -177,32 +173,21 @@ func (s *Tenant) WithMutableTableDescriptor( require.NoError(s.t, sql.DescsTxn(ctx, &execCfg, func( ctx context.Context, txn *kv.Txn, descsCol *descs.Collection, ) error { - _, desc, err := descsCol.GetMutableTableByName( - ctx, - txn, - tree.NewTableNameWithSchema(tree.Name(dbName), "public", tree.Name(tbName)), - tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - Required: true, - IncludeOffline: true, - }, - }, - ) + g := descsCol.ByName(txn).WithOffline().Get() + tn := tree.NewTableNameWithSchema(tree.Name(dbName), "public", tree.Name(tbName)) + _, imm, err := descs.PrefixAndTable(ctx, g, tn) + if err != nil { + return err + } + mut, err := descsCol.MutableByID(txn).Table(ctx, imm.GetID()) if err != nil { return err } - f(desc) - return descsCol.WriteDesc(ctx, false, desc, txn) + f(mut) + return descsCol.WriteDesc(ctx, false /* kvTrace */, mut, txn) })) } -// descLookupFlags is the set of look up flags used when fetching descriptors. -var descLookupFlags = tree.CommonLookupFlags{ - IncludeDropped: true, - IncludeOffline: true, - AvoidLeased: true, // we want consistent reads -} - // LookupTableDescriptorByID returns the table identified by the given ID. func (s *Tenant) LookupTableDescriptorByID( ctx context.Context, id descpb.ID, @@ -212,11 +197,7 @@ func (s *Tenant) LookupTableDescriptorByID( ctx context.Context, txn *kv.Txn, descsCol *descs.Collection, ) error { var err error - desc, err = descsCol.GetImmutableTableByID(ctx, txn, id, - tree.ObjectLookupFlags{ - CommonLookupFlags: descLookupFlags, - }, - ) + desc, err = descsCol.ByID(txn).Get().Table(ctx, id) return err })) return desc @@ -231,12 +212,9 @@ func (s *Tenant) LookupTableByName( ctx context.Context, txn *kv.Txn, descsCol *descs.Collection, ) error { var err error - _, desc, err = descsCol.GetImmutableTableByName(ctx, txn, - tree.NewTableNameWithSchema(tree.Name(dbName), "public", tree.Name(tbName)), - tree.ObjectLookupFlags{ - CommonLookupFlags: descLookupFlags, - }, - ) + g := descsCol.ByName(txn).WithOffline().MaybeGet() + tn := tree.NewTableNameWithSchema(tree.Name(dbName), "public", tree.Name(tbName)) + _, desc, err = descs.PrefixAndTable(ctx, g, tn) return err })) return desc @@ -251,13 +229,7 @@ func (s *Tenant) LookupDatabaseByName( require.NoError(s.t, sql.DescsTxn(ctx, &execCfg, func(ctx context.Context, txn *kv.Txn, descsCol *descs.Collection) error { var err error - desc, err = descsCol.GetImmutableDatabaseByName(ctx, txn, dbName, - tree.DatabaseLookupFlags{ - Required: true, - IncludeOffline: true, - AvoidLeased: true, - }, - ) + desc, err = descsCol.ByName(txn).WithOffline().Get().Database(ctx, dbName) return err })) return desc diff --git a/pkg/sql/alter_database.go b/pkg/sql/alter_database.go index c061571a17aa..c82ef1431370 100644 --- a/pkg/sql/alter_database.go +++ b/pkg/sql/alter_database.go @@ -60,8 +60,7 @@ func (p *planner) AlterDatabaseOwner( return nil, err } - dbDesc, err := p.Descriptors().GetMutableDatabaseByName(ctx, p.txn, string(n.Name), - tree.DatabaseLookupFlags{Required: true}) + dbDesc, err := p.Descriptors().MutableByName(p.txn).Database(ctx, string(n.Name)) if err != nil { return nil, err } @@ -146,9 +145,7 @@ func (p *planner) AlterDatabaseAddRegion( return nil, err } - dbDesc, err := p.Descriptors().GetMutableDatabaseByName(ctx, p.txn, string(n.Name), - tree.DatabaseLookupFlags{Required: true}, - ) + dbDesc, err := p.Descriptors().MutableByName(p.txn).Database(ctx, string(n.Name)) if err != nil { return nil, err } @@ -221,11 +218,7 @@ func (n *alterDatabaseAddRegionNode) startExec(params runParams) error { } // Get the type descriptor for the multi-region enum. - typeDesc, err := params.p.Descriptors().GetMutableTypeVersionByID( - params.ctx, - params.p.txn, - n.desc.RegionConfig.RegionEnumID, - ) + typeDesc, err := params.p.Descriptors().MutableByID(params.p.txn).Type(params.ctx, n.desc.RegionConfig.RegionEnumID) if err != nil { return err } @@ -312,8 +305,7 @@ func (p *planner) AlterDatabaseDropRegion( return nil, err } - dbDesc, err := p.Descriptors().GetMutableDatabaseByName(ctx, p.txn, string(n.Name), - tree.DatabaseLookupFlags{Required: true}) + dbDesc, err := p.Descriptors().MutableByName(p.txn).Database(ctx, string(n.Name)) if err != nil { return nil, err } @@ -384,7 +376,7 @@ func (p *planner) AlterDatabaseDropRegion( if err != nil { return nil, err } - typeDesc, err := p.Descriptors().GetMutableTypeVersionByID(ctx, p.txn, typeID) + typeDesc, err := p.Descriptors().MutableByID(p.txn).Type(ctx, typeID) if err != nil { return nil, err } @@ -423,7 +415,7 @@ func (p *planner) AlterDatabaseDropRegion( // detritus multi-region type descriptor. This includes both the // type descriptor and its array counterpart. toDrop = append(toDrop, typeDesc) - arrayTypeDesc, err := p.Descriptors().GetMutableTypeVersionByID(ctx, p.txn, typeDesc.ArrayTypeID) + arrayTypeDesc, err := p.Descriptors().MutableByID(p.txn).Type(ctx, typeDesc.ArrayTypeID) if err != nil { return nil, err } @@ -592,11 +584,7 @@ func (n *alterDatabaseDropRegionNode) startExec(params runParams) error { if n.n == nil { return nil } - typeDesc, err := params.p.Descriptors().GetMutableTypeVersionByID( - params.ctx, - params.p.txn, - n.desc.RegionConfig.RegionEnumID, - ) + typeDesc, err := params.p.Descriptors().MutableByID(params.p.txn).Type(params.ctx, n.desc.RegionConfig.RegionEnumID) if err != nil { return err } @@ -693,9 +681,7 @@ func (p *planner) AlterDatabasePrimaryRegion( return nil, err } - dbDesc, err := p.Descriptors().GetMutableDatabaseByName(ctx, p.txn, string(n.Name), - tree.DatabaseLookupFlags{Required: true}, - ) + dbDesc, err := p.Descriptors().MutableByName(p.txn).Database(ctx, string(n.Name)) if err != nil { return nil, err } @@ -750,10 +736,7 @@ func (n *alterDatabasePrimaryRegionNode) switchPrimaryRegion(params runParams) e } // Get the type descriptor for the multi-region enum. - typeDesc, err := params.p.Descriptors().GetMutableTypeVersionByID( - params.ctx, - params.p.txn, - n.desc.RegionConfig.RegionEnumID) + typeDesc, err := params.p.Descriptors().MutableByID(params.p.txn).Type(params.ctx, n.desc.RegionConfig.RegionEnumID) if err != nil { return err } @@ -1085,9 +1068,7 @@ func (p *planner) AlterDatabaseSurvivalGoal( return nil, err } - dbDesc, err := p.Descriptors().GetMutableDatabaseByName(ctx, p.txn, string(n.Name), - tree.DatabaseLookupFlags{Required: true}, - ) + dbDesc, err := p.Descriptors().MutableByName(p.txn).Database(ctx, string(n.Name)) if err != nil { return nil, err } @@ -1228,9 +1209,7 @@ func (p *planner) AlterDatabasePlacement( ) } - dbDesc, err := p.Descriptors().GetMutableDatabaseByName(ctx, p.txn, string(n.Name), - tree.DatabaseLookupFlags{Required: true}, - ) + dbDesc, err := p.Descriptors().MutableByName(p.txn).Database(ctx, string(n.Name)) if err != nil { return nil, err } @@ -1362,9 +1341,7 @@ func (p *planner) AlterDatabaseAddSuperRegion( return nil, err } - dbDesc, err := p.Descriptors().GetMutableDatabaseByName(ctx, p.txn, string(n.DatabaseName), - tree.DatabaseLookupFlags{Required: true}, - ) + dbDesc, err := p.Descriptors().MutableByName(p.txn).Database(ctx, string(n.DatabaseName)) if err != nil { return nil, err } @@ -1410,7 +1387,7 @@ func (n *alterDatabaseAddSuperRegion) startExec(params runParams) error { if err != nil { return err } - typeDesc, err := params.p.Descriptors().GetMutableTypeVersionByID(params.ctx, params.p.txn, typeID) + typeDesc, err := params.p.Descriptors().MutableByID(params.p.txn).Type(params.ctx, typeID) if err != nil { return err } @@ -1458,9 +1435,7 @@ func (p *planner) AlterDatabaseDropSuperRegion( return nil, err } - dbDesc, err := p.Descriptors().GetMutableDatabaseByName(ctx, p.txn, string(n.DatabaseName), - tree.DatabaseLookupFlags{Required: true}, - ) + dbDesc, err := p.Descriptors().MutableByName(p.txn).Database(ctx, string(n.DatabaseName)) if err != nil { return nil, err } @@ -1489,7 +1464,7 @@ func (n *alterDatabaseDropSuperRegion) startExec(params runParams) error { if err != nil { return err } - typeDesc, err := params.p.Descriptors().GetMutableTypeVersionByID(params.ctx, params.p.txn, typeID) + typeDesc, err := params.p.Descriptors().MutableByID(params.p.txn).Type(params.ctx, typeID) if err != nil { return err } @@ -1554,7 +1529,7 @@ func (p *planner) getSuperRegionsForDatabase( if err != nil { return nil, err } - typeDesc, err := p.Descriptors().GetMutableTypeVersionByID(ctx, p.txn, typeID) + typeDesc, err := p.Descriptors().MutableByID(p.txn).Type(ctx, typeID) if err != nil { return nil, err } @@ -1586,9 +1561,7 @@ func (p *planner) AlterDatabaseAlterSuperRegion( return nil, err } - dbDesc, err := p.Descriptors().GetMutableDatabaseByName(ctx, p.txn, string(n.DatabaseName), - tree.DatabaseLookupFlags{Required: true}, - ) + dbDesc, err := p.Descriptors().MutableByName(p.txn).Database(ctx, string(n.DatabaseName)) if err != nil { return nil, err } @@ -1617,7 +1590,7 @@ func (n *alterDatabaseAlterSuperRegion) startExec(params runParams) error { if err != nil { return err } - typeDesc, err := params.p.Descriptors().GetMutableTypeVersionByID(params.ctx, params.p.txn, typeID) + typeDesc, err := params.p.Descriptors().MutableByID(params.p.txn).Type(params.ctx, typeID) if err != nil { return err } @@ -1739,9 +1712,7 @@ func (p *planner) AlterDatabaseSecondaryRegion( return nil, err } - dbDesc, err := p.Descriptors().GetMutableDatabaseByName(ctx, p.txn, string(n.DatabaseName), - tree.DatabaseLookupFlags{Required: true}, - ) + dbDesc, err := p.Descriptors().MutableByName(p.txn).Database(ctx, string(n.DatabaseName)) if err != nil { return nil, err } @@ -1809,10 +1780,7 @@ func (n *alterDatabaseSecondaryRegion) startExec(params runParams) error { } // Get the type descriptor for the multi-region enum. - typeDesc, err := params.p.Descriptors().GetMutableTypeVersionByID( - params.ctx, - params.p.txn, - n.desc.RegionConfig.RegionEnumID) + typeDesc, err := params.p.Descriptors().MutableByID(params.p.txn).Type(params.ctx, n.desc.RegionConfig.RegionEnumID) if err != nil { return err } @@ -1882,9 +1850,7 @@ func (p *planner) AlterDatabaseDropSecondaryRegion( return nil, err } - dbDesc, err := p.Descriptors().GetMutableDatabaseByName(ctx, p.txn, string(n.DatabaseName), - tree.DatabaseLookupFlags{Required: true}, - ) + dbDesc, err := p.Descriptors().MutableByName(p.txn).Database(ctx, string(n.DatabaseName)) if err != nil { return nil, err } @@ -1922,7 +1888,7 @@ func (n *alterDatabaseDropSecondaryRegion) startExec(params runParams) error { if err != nil { return err } - typeDesc, err := params.p.Descriptors().GetMutableTypeVersionByID(params.ctx, params.p.txn, typeID) + typeDesc, err := params.p.Descriptors().MutableByID(params.p.txn).Type(params.ctx, typeID) if err != nil { return err } @@ -2034,9 +2000,7 @@ func (p *planner) AlterDatabaseSetZoneConfigExtension( return nil, err } - dbDesc, err := p.Descriptors().GetMutableDatabaseByName(ctx, p.txn, string(n.DatabaseName), - tree.DatabaseLookupFlags{Required: true}, - ) + dbDesc, err := p.Descriptors().MutableByName(p.txn).Database(ctx, string(n.DatabaseName)) if err != nil { return nil, err } @@ -2099,7 +2063,7 @@ func (n *alterDatabaseSetZoneConfigExtensionNode) startExec(params runParams) er if err != nil { return err } - typeDesc, err := params.p.Descriptors().GetMutableTypeVersionByID(params.ctx, params.p.txn, typeID) + typeDesc, err := params.p.Descriptors().MutableByID(params.p.txn).Type(params.ctx, typeID) if err != nil { return err } diff --git a/pkg/sql/alter_default_privileges.go b/pkg/sql/alter_default_privileges.go index d29fa3d0b7db..d5450b725f8c 100644 --- a/pkg/sql/alter_default_privileges.go +++ b/pkg/sql/alter_default_privileges.go @@ -58,8 +58,7 @@ func (p *planner) alterDefaultPrivileges( if n.Database != nil { database = n.Database.Normalize() } - dbDesc, err := p.Descriptors().GetMutableDatabaseByName(ctx, p.txn, database, - tree.DatabaseLookupFlags{Required: true}) + dbDesc, err := p.Descriptors().MutableByName(p.txn).Database(ctx, database) if err != nil { return nil, err } @@ -81,16 +80,14 @@ func (p *planner) alterDefaultPrivileges( var schemaDescs []*schemadesc.Mutable for _, sc := range n.Schemas { - immFlags := tree.SchemaLookupFlags{Required: true, AvoidLeased: true} - immSchema, err := p.Descriptors().GetImmutableSchemaByName(ctx, p.txn, dbDesc, sc.Schema(), immFlags) + immSchema, err := p.Descriptors().ByName(p.txn).Get().Schema(ctx, dbDesc, sc.Schema()) if err != nil { return nil, err } if immSchema.SchemaKind() != catalog.SchemaUserDefined { return nil, pgerror.Newf(pgcode.InvalidParameterValue, "%s is not a physical schema", immSchema.GetName()) } - mutFlags := tree.SchemaLookupFlags{Required: true} - mutableSchemaDesc, err := p.Descriptors().GetMutableSchemaByID(ctx, p.txn, immSchema.GetID(), mutFlags) + mutableSchemaDesc, err := p.Descriptors().MutableByID(p.txn).Schema(ctx, immSchema.GetID()) if err != nil { return nil, err } diff --git a/pkg/sql/alter_function.go b/pkg/sql/alter_function.go index c2760dfd3bc5..f102af458071 100644 --- a/pkg/sql/alter_function.go +++ b/pkg/sql/alter_function.go @@ -133,9 +133,7 @@ func (n *alterFunctionRenameNode) startExec(params runParams) error { return err } - scDesc, err := params.p.Descriptors().GetMutableSchemaByID( - params.ctx, params.p.txn, fnDesc.GetParentSchemaID(), tree.SchemaLookupFlags{Required: true}, - ) + scDesc, err := params.p.Descriptors().MutableByID(params.p.txn).Schema(params.ctx, fnDesc.GetParentSchemaID()) if err != nil { return err } @@ -269,17 +267,11 @@ func (n *alterFunctionSetSchemaNode) startExec(params runParams) error { } // Functions cannot be resolved across db, so just use current db name to get // the descriptor. - db, err := params.p.Descriptors().GetMutableDatabaseByName( - params.ctx, params.p.txn, params.p.CurrentDatabase(), tree.DatabaseLookupFlags{Required: true}, - ) + db, err := params.p.Descriptors().MutableByName(params.p.txn).Database(params.ctx, params.p.CurrentDatabase()) if err != nil { return err } - - scFlags := tree.SchemaLookupFlags{Required: true, AvoidLeased: true} - sc, err := params.p.Descriptors().GetImmutableSchemaByName( - params.ctx, params.p.txn, db, string(n.n.NewSchemaName), scFlags, - ) + sc, err := params.p.Descriptors().ByName(params.p.txn).Get().Schema(params.ctx, db, string(n.n.NewSchemaName)) if err != nil { return err } @@ -304,9 +296,7 @@ func (n *alterFunctionSetSchemaNode) startExec(params runParams) error { // No-op if moving to the same schema. return nil } - targetSc, err := params.p.Descriptors().GetMutableSchemaByID( - params.ctx, params.p.txn, sc.GetID(), params.p.CommonLookupFlagsRequired(), - ) + targetSc, err := params.p.Descriptors().MutableByID(params.p.txn).Schema(params.ctx, sc.GetID()) if err != nil { return err } @@ -326,9 +316,7 @@ func (n *alterFunctionSetSchemaNode) startExec(params runParams) error { ) } - sourceSc, err := params.p.Descriptors().GetMutableSchemaByID( - params.ctx, params.p.txn, fnDesc.GetParentSchemaID(), tree.SchemaLookupFlags{Required: true}, - ) + sourceSc, err := params.p.Descriptors().MutableByID(params.p.txn).Schema(params.ctx, fnDesc.GetParentSchemaID()) if err != nil { return err } diff --git a/pkg/sql/alter_role.go b/pkg/sql/alter_role.go index 91a9c5a27f08..dbe6f1d9b18d 100644 --- a/pkg/sql/alter_role.go +++ b/pkg/sql/alter_role.go @@ -301,8 +301,7 @@ func (p *planner) AlterRoleSet(ctx context.Context, n *tree.AlterRoleSet) (planN dbDescID := descpb.ID(0) if n.DatabaseName != "" { - dbDesc, err := p.Descriptors().GetImmutableDatabaseByName(ctx, p.txn, string(n.DatabaseName), - tree.DatabaseLookupFlags{Required: true}) + dbDesc, err := p.Descriptors().ByNameWithLeased(p.txn).Get().Database(ctx, string(n.DatabaseName)) if err != nil { return nil, err } diff --git a/pkg/sql/alter_schema.go b/pkg/sql/alter_schema.go index 8b593ea5d82f..cf3af7c9136a 100644 --- a/pkg/sql/alter_schema.go +++ b/pkg/sql/alter_schema.go @@ -52,16 +52,11 @@ func (p *planner) AlterSchema(ctx context.Context, n *tree.AlterSchema) (planNod if n.Schema.ExplicitCatalog { dbName = n.Schema.Catalog() } - db, err := p.Descriptors().GetMutableDatabaseByName(ctx, p.txn, dbName, - tree.DatabaseLookupFlags{Required: true}) + db, err := p.Descriptors().MutableByName(p.txn).Database(ctx, dbName) if err != nil { return nil, err } - schema, err := p.Descriptors().GetImmutableSchemaByName(ctx, p.txn, db, - string(n.Schema.SchemaName), tree.SchemaLookupFlags{ - Required: true, - AvoidLeased: true, - }) + schema, err := p.Descriptors().ByName(p.txn).Get().Schema(ctx, db, string(n.Schema.SchemaName)) if err != nil { return nil, err } @@ -73,8 +68,7 @@ func (p *planner) AlterSchema(ctx context.Context, n *tree.AlterSchema) (planNod case catalog.SchemaPublic, catalog.SchemaVirtual, catalog.SchemaTemporary: return nil, pgerror.Newf(pgcode.InvalidSchemaName, "cannot modify schema %q", n.Schema.String()) case catalog.SchemaUserDefined: - flags := p.CommonLookupFlagsRequired() - desc, err := p.Descriptors().GetMutableSchemaByID(ctx, p.txn, schema.GetID(), flags) + desc, err := p.Descriptors().MutableByID(p.txn).Schema(ctx, schema.GetID()) if err != nil { return nil, err } @@ -123,9 +117,8 @@ func (n *alterSchemaNode) startExec(params runParams) error { return sqlerrors.NewSchemaAlreadyExistsError(newName) } - lookupFlags := tree.CommonLookupFlags{Required: true, AvoidLeased: true} if err := maybeFailOnDependentDescInRename( - params.ctx, params.p, n.db, n.desc, lookupFlags, catalog.Schema, + params.ctx, params.p, n.db, n.desc, false /* withLeased */, catalog.Schema, ); err != nil { return err } @@ -173,7 +166,7 @@ func (p *planner) alterSchemaOwner( } // The user must also have CREATE privilege on the schema's database. - parentDBDesc, err := p.Descriptors().GetMutableDescriptorByID(ctx, p.txn, scDesc.GetParentID()) + parentDBDesc, err := p.Descriptors().MutableByID(p.txn).Desc(ctx, scDesc.GetParentID()) if err != nil { return err } diff --git a/pkg/sql/alter_table.go b/pkg/sql/alter_table.go index c37d3d07cfb2..d4d089b04773 100644 --- a/pkg/sql/alter_table.go +++ b/pkg/sql/alter_table.go @@ -1493,7 +1493,7 @@ func (p *planner) updateFKBackReferenceName( if tableDesc.ID == ref.ReferencedTableID { referencedTableDesc = tableDesc } else { - lookup, err := p.Descriptors().GetMutableTableVersionByID(ctx, ref.ReferencedTableID, p.txn) + lookup, err := p.Descriptors().MutableByID(p.txn).Table(ctx, ref.ReferencedTableID) if err != nil { return errors.Wrapf(err, "error resolving referenced table ID %d", ref.ReferencedTableID) } diff --git a/pkg/sql/alter_table_locality.go b/pkg/sql/alter_table_locality.go index c770ddc69d74..63883ef7cadf 100644 --- a/pkg/sql/alter_table_locality.go +++ b/pkg/sql/alter_table_locality.go @@ -70,15 +70,7 @@ func (p *planner) AlterTableLocality( } // Ensure that the database is multi-region enabled. - _, dbDesc, err := p.Descriptors().GetImmutableDatabaseByID( - ctx, - p.txn, - tableDesc.GetParentID(), - tree.DatabaseLookupFlags{ - AvoidLeased: true, - Required: true, - }, - ) + dbDesc, err := p.Descriptors().ByID(p.txn).WithoutNonPublic().Get().Database(ctx, tableDesc.GetParentID()) if err != nil { return nil, err } @@ -122,12 +114,7 @@ func (n *alterTableSetLocalityNode) alterTableLocalityGlobalToRegionalByTable( ) } - _, dbDesc, err := params.p.Descriptors().GetImmutableDatabaseByID( - params.ctx, params.p.txn, n.tableDesc.ParentID, - tree.DatabaseLookupFlags{ - Required: true, - AvoidLeased: true, - }) + dbDesc, err := params.p.Descriptors().ByID(params.p.txn).WithoutNonPublic().Get().Database(params.ctx, n.tableDesc.ParentID) if err != nil { return err } @@ -189,12 +176,7 @@ func (n *alterTableSetLocalityNode) alterTableLocalityRegionalByTableToRegionalB ) } - _, dbDesc, err := params.p.Descriptors().GetImmutableDatabaseByID( - params.ctx, params.p.txn, n.tableDesc.ParentID, - tree.DatabaseLookupFlags{ - Required: true, - AvoidLeased: true, - }) + dbDesc, err := params.p.Descriptors().ByID(params.p.txn).WithoutNonPublic().Get().Database(params.ctx, n.tableDesc.ParentID) if err != nil { return err } @@ -671,11 +653,7 @@ func setNewLocalityConfig( descsCol *descs.Collection, ) error { getMultiRegionTypeDesc := func() (*typedesc.Mutable, error) { - _, dbDesc, err := descsCol.GetImmutableDatabaseByID( - ctx, txn, desc.GetParentID(), tree.DatabaseLookupFlags{ - Required: true, - AvoidLeased: true, - }) + dbDesc, err := descsCol.ByID(txn).WithoutNonPublic().Get().Database(ctx, desc.GetParentID()) if err != nil { return nil, err } @@ -684,7 +662,7 @@ func setNewLocalityConfig( if err != nil { return nil, err } - return descsCol.GetMutableTypeVersionByID(ctx, txn, regionEnumID) + return descsCol.MutableByID(txn).Type(ctx, regionEnumID) } // If there was a dependency before on the multi-region enum before the // new locality is set, we must unlink the dependency. diff --git a/pkg/sql/alter_type.go b/pkg/sql/alter_type.go index 0b10122113c6..9faf3a4cc3ae 100644 --- a/pkg/sql/alter_type.go +++ b/pkg/sql/alter_type.go @@ -253,7 +253,7 @@ func (p *planner) renameType(ctx context.Context, n *alterTypeNode, newName stri if err != nil { return err } - arrayDesc, err := p.Descriptors().GetMutableTypeVersionByID(ctx, p.txn, n.desc.ArrayTypeID) + arrayDesc, err := p.Descriptors().MutableByID(p.txn).Type(ctx, n.desc.ArrayTypeID) if err != nil { return err } @@ -373,7 +373,7 @@ func (p *planner) setTypeSchema(ctx context.Context, n *alterTypeNode, schema st return err } - arrayDesc, err := p.Descriptors().GetMutableTypeVersionByID(ctx, p.txn, n.desc.ArrayTypeID) + arrayDesc, err := p.Descriptors().MutableByID(p.txn).Type(ctx, n.desc.ArrayTypeID) if err != nil { return err } @@ -405,7 +405,7 @@ func (p *planner) alterTypeOwner( typeDesc := n.desc oldOwner := typeDesc.GetPrivileges().Owner() - arrayDesc, err := p.Descriptors().GetMutableTypeVersionByID(ctx, p.txn, typeDesc.ArrayTypeID) + arrayDesc, err := p.Descriptors().MutableByID(p.txn).Type(ctx, typeDesc.ArrayTypeID) if err != nil { return err } diff --git a/pkg/sql/authorization.go b/pkg/sql/authorization.go index e2311d61582f..f72101f8727e 100644 --- a/pkg/sql/authorization.go +++ b/pkg/sql/authorization.go @@ -465,12 +465,7 @@ func MemberOfWithAdminOption( roleMembersCache := execCfg.RoleMemberCache // Lookup table version. - _, tableDesc, err := descsCol.GetImmutableTableByName( - ctx, - txn, - &roleMembersTableName, - tree.ObjectLookupFlagsWithRequired(), - ) + _, tableDesc, err := descs.PrefixAndTable(ctx, descsCol.ByNameWithLeased(txn).Get(), &roleMembersTableName) if err != nil { return nil, err } @@ -746,8 +741,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().ByIDWithLeased(p.Txn()).WithoutNonPublic().Get().Schema(ctx, schemaID) if err != nil { return err } @@ -759,8 +753,7 @@ func (p *planner) canCreateOnSchema( // The caller wishes to skip this check. return nil } - _, dbDesc, err := p.Descriptors().GetImmutableDatabaseByID( - ctx, p.Txn(), dbID, tree.DatabaseLookupFlags{Required: true}) + dbDesc, err := p.Descriptors().ByIDWithLeased(p.Txn()).WithoutNonPublic().Get().Database(ctx, dbID) if err != nil { return err } @@ -852,9 +845,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().ByIDWithLeased(p.Txn()).WithoutNonPublic().Get().Schema(ctx, schemaID) if err != nil { return false, err } diff --git a/pkg/sql/backfill.go b/pkg/sql/backfill.go index 9e598eb8aa40..8b017976f1e8 100644 --- a/pkg/sql/backfill.go +++ b/pkg/sql/backfill.go @@ -450,7 +450,7 @@ func (sc *SchemaChanger) dropConstraints( // Create update closure for the table and all other tables with backreferences. if err := sc.txn(ctx, func(ctx context.Context, txn *kv.Txn, descsCol *descs.Collection) error { - scTable, err := descsCol.GetMutableTableVersionByID(ctx, sc.descID, txn) + scTable, err := descsCol.MutableByID(txn).Table(ctx, sc.descID) if err != nil { return err } @@ -480,7 +480,7 @@ func (sc *SchemaChanger) dropConstraints( if def.Name != constraint.GetName() { continue } - backrefTable, err := descsCol.GetMutableTableVersionByID(ctx, fk.GetReferencedTableID(), txn) + backrefTable, err := descsCol.MutableByID(txn).Table(ctx, fk.GetReferencedTableID()) if err != nil { return err } @@ -543,19 +543,11 @@ func (sc *SchemaChanger) dropConstraints( if err := sc.txn(ctx, func( ctx context.Context, txn *kv.Txn, descsCol *descs.Collection, ) (err error) { - if tableDescs[sc.descID], err = descsCol.GetImmutableTableByID( - ctx, txn, sc.descID, tree.ObjectLookupFlags{}, - ); err != nil { + if tableDescs[sc.descID], err = descsCol.ByIDWithLeased(txn).WithoutNonPublic().Get().Table(ctx, sc.descID); err != nil { return err } for id := range fksByBackrefTable { - desc, err := descsCol.GetImmutableTableByID( - ctx, txn, id, tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - IncludeDropped: true, - }, - }, - ) + desc, err := descsCol.ByIDWithLeased(txn).WithoutOffline().Get().Table(ctx, id) if err != nil { return err } @@ -594,7 +586,7 @@ func (sc *SchemaChanger) addConstraints( if err := sc.txn(ctx, func( ctx context.Context, txn *kv.Txn, descsCol *descs.Collection, ) error { - scTable, err := descsCol.GetMutableTableVersionByID(ctx, sc.descID, txn) + scTable, err := descsCol.MutableByID(txn).Table(ctx, sc.descID) if err != nil { return err } @@ -643,7 +635,7 @@ func (sc *SchemaChanger) addConstraints( } if !foundExisting { scTable.OutboundFKs = append(scTable.OutboundFKs, *fk.ForeignKeyDesc()) - backrefTable, err := descsCol.GetMutableTableVersionByID(ctx, fk.GetReferencedTableID(), txn) + backrefTable, err := descsCol.MutableByID(txn).Table(ctx, fk.GetReferencedTableID()) if err != nil { return err } @@ -740,9 +732,7 @@ func (sc *SchemaChanger) validateConstraints( if err := sc.fixedTimestampTxn(ctx, readAsOf, func( ctx context.Context, txn *kv.Txn, descriptors *descs.Collection, ) error { - flags := tree.ObjectLookupFlagsWithRequired() - flags.AvoidLeased = true - tableDesc, err = descriptors.GetImmutableTableByID(ctx, txn, sc.descID, flags) + tableDesc, err = descriptors.ByID(txn).WithoutNonPublic().Get().Table(ctx, sc.descID) return err }); err != nil { return err @@ -826,7 +816,7 @@ func (sc *SchemaChanger) validateConstraints( func (sc *SchemaChanger) getTableVersion( ctx context.Context, txn *kv.Txn, tc *descs.Collection, version descpb.DescriptorVersion, ) (catalog.TableDescriptor, error) { - tableDesc, err := tc.GetImmutableTableByID(ctx, txn, sc.descID, tree.ObjectLookupFlags{}) + tableDesc, err := tc.ByIDWithLeased(txn).WithoutNonPublic().Get().Table(ctx, sc.descID) if err != nil { return nil, err } @@ -1379,13 +1369,7 @@ func (sc *SchemaChanger) updateJobRunningStatus( ) (tableDesc catalog.TableDescriptor, err error) { err = DescsTxn(ctx, sc.execCfg, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) (err error) { // Read table descriptor without holding a lease. - tableDesc, err = col.GetImmutableTableByID(ctx, txn, sc.descID, tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - }, - }) + tableDesc, err = col.ByID(txn).Get().Table(ctx, sc.descID) if err != nil { return err } @@ -1443,9 +1427,7 @@ func (sc *SchemaChanger) validateIndexes(ctx context.Context) error { if err := sc.fixedTimestampTxn(ctx, readAsOf, func( ctx context.Context, txn *kv.Txn, descriptors *descs.Collection, ) (err error) { - flags := tree.ObjectLookupFlagsWithRequired() - flags.AvoidLeased = true - tableDesc, err = descriptors.GetImmutableTableByID(ctx, txn, sc.descID, flags) + tableDesc, err = descriptors.ByID(txn).WithoutNonPublic().Get().Table(ctx, sc.descID) return err }); err != nil { return err @@ -2226,7 +2208,7 @@ func (sc *SchemaChanger) mergeFromTemporaryIndex( ctx context.Context, txn *kv.Txn, descsCol *descs.Collection, ) error { var err error - tbl, err = descsCol.GetMutableTableVersionByID(ctx, sc.descID, txn) + tbl, err = descsCol.MutableByID(txn).Table(ctx, sc.descID) return err }); err != nil { return err @@ -2247,7 +2229,7 @@ func (sc *SchemaChanger) runStateMachineAfterTempIndexMerge(ctx context.Context) return sc.txn(ctx, func( ctx context.Context, txn *kv.Txn, descsCol *descs.Collection, ) error { - tbl, err := descsCol.GetMutableTableVersionByID(ctx, sc.descID, txn) + tbl, err := descsCol.MutableByID(txn).Table(ctx, sc.descID) if err != nil { return err } @@ -2552,7 +2534,7 @@ func runSchemaChangesInTxn( if selfReference { referencedTableDesc = tableDesc } else { - lookup, err := planner.Descriptors().GetMutableTableVersionByID(ctx, fk.GetReferencedTableID(), planner.Txn()) + lookup, err := planner.Descriptors().MutableByID(planner.Txn()).Table(ctx, fk.GetReferencedTableID()) if err != nil { return errors.Wrapf(err, "error resolving referenced table ID %d", fk.GetReferencedTableID()) } @@ -2641,13 +2623,7 @@ func getTargetTablesAndFk( if fk == nil { return nil, nil, nil, errors.AssertionFailedf("foreign key %s does not exist", fkName) } - targetTable, err = descsCol.GetImmutableTableByID(ctx, txn, fk.ReferencedTableID, tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - }, - }) + targetTable, err = descsCol.ByID(txn).Get().Table(ctx, fk.ReferencedTableID) if err != nil { return nil, nil, nil, err } diff --git a/pkg/sql/catalog/descriptor.go b/pkg/sql/catalog/descriptor.go index 1de753eb48b8..4ee814d2d588 100644 --- a/pkg/sql/catalog/descriptor.go +++ b/pkg/sql/catalog/descriptor.go @@ -591,13 +591,13 @@ type TableDescriptor interface { // HasColumnBackfillMutation returns whether the table has any queued column // mutations that require a backfill. HasColumnBackfillMutation() bool - // MakeFirstMutationPublic creates a Mutable from the - // immutable by making the first mutation public. + // MakeFirstMutationPublic creates a descriptor by making the first + // mutation public. // This is super valuable when trying to run SQL over data associated // with a schema mutation that is still not yet public: Data validation, // error reporting. MakeFirstMutationPublic(...MutationPublicationFilter) (TableDescriptor, error) - // MakePublic creates a Mutable from the immutable by making the it public. + // MakePublic creates a descriptor by making the state public. MakePublic() TableDescriptor // AllMutations returns all of the table descriptor's mutations. AllMutations() []Mutation diff --git a/pkg/sql/catalog/descs/BUILD.bazel b/pkg/sql/catalog/descs/BUILD.bazel index 0a014bb7ba6e..933c21930773 100644 --- a/pkg/sql/catalog/descs/BUILD.bazel +++ b/pkg/sql/catalog/descs/BUILD.bazel @@ -5,12 +5,10 @@ go_library( name = "descs", srcs = [ "collection.go", - "database.go", "descriptor.go", "dist_sql_type_resolver.go", "errors.go", "factory.go", - "function.go", "getters.go", "helpers.go", "hydrate.go", @@ -22,7 +20,6 @@ go_library( "table.go", "temporary_descriptors.go", "txn.go", - "type.go", "uncommitted_descriptors.go", "uncommitted_metadata.go", "validate.go", diff --git a/pkg/sql/catalog/descs/collection_test.go b/pkg/sql/catalog/descs/collection_test.go index 97de4464131a..580451b0357e 100644 --- a/pkg/sql/catalog/descs/collection_test.go +++ b/pkg/sql/catalog/descs/collection_test.go @@ -79,9 +79,7 @@ func TestCollectionWriteDescToBatch(t *testing.T) { require.NoError(t, db.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error { defer descriptors.ReleaseAll(ctx) tn := tree.MakeTableNameWithSchema("db", "schema", "table") - flags := tree.ObjectLookupFlagsWithRequired() - flags.RequireMutable = true - _, mut, err := descriptors.GetMutableTableByName(ctx, txn, &tn, flags) + _, mut, err := descs.PrefixAndMutableTable(ctx, descriptors.MutableByName(txn), &tn) require.NoError(t, err) require.NotNil(t, mut) // We want to create some descriptors and then ensure that writing them to a @@ -131,12 +129,12 @@ func TestCollectionWriteDescToBatch(t *testing.T) { require.Equal(t, descpb.DescriptorVersion(1), newTable.Version) // Ensure that the descriptor has been added to the collection. - _, mut2, err := descriptors.GetMutableTableByName(ctx, txn, &tn, flags) + _, mut2, err := descs.PrefixAndMutableTable(ctx, descriptors.MutableByName(txn), &tn) require.NoError(t, err) require.Equal(t, mut, mut2) t2n := tree.MakeTableNameWithSchema("db", "schema", "table2") - _, newTableResolved, err := descriptors.GetMutableTableByName(ctx, txn, &t2n, flags) + _, newTableResolved, err := descs.PrefixAndMutableTable(ctx, descriptors.MutableByName(txn), &t2n) require.NoError(t, err) require.Equal(t, newTable, newTableResolved) return txn.Run(ctx, b) @@ -188,10 +186,7 @@ func TestTxnClearsCollectionOnRetry(t *testing.T) { ctx context.Context, txn *kv.Txn, descriptors *descs.Collection, ) error { txn.SetDebugName(txnName) - - flags := tree.ObjectLookupFlagsWithRequired() - flags.RequireMutable = true - _, mut, err := descriptors.GetMutableTableByName(ctx, txn, &tn, flags) + _, mut, err := descs.PrefixAndMutableTable(ctx, descriptors.MutableByName(txn), &tn) require.NoError(t, err) // Verify that the descriptor version is always 1 prior to the write and 2 // after the write even after a retry. @@ -229,30 +224,25 @@ func TestAddUncommittedDescriptorAndMutableResolution(t *testing.T) { require.NoError(t, sql.DescsTxn(ctx, &execCfg, func( ctx context.Context, txn *kv.Txn, descriptors *descs.Collection, ) error { - flags := tree.DatabaseLookupFlags{} - flags.RequireMutable = true - flags.Required = true - - mut, err := descriptors.GetMutableDatabaseByName(ctx, txn, "db", flags) + mut, err := descriptors.MutableByName(txn).Database(ctx, "db") require.NoError(t, err) dbID := mut.GetID() - byID, err := descriptors.GetMutableDescriptorByID(ctx, txn, dbID) + byID, err := descriptors.MutableByID(txn).Desc(ctx, dbID) require.NoError(t, err) require.Same(t, mut, byID) mut.MaybeIncrementVersion() mut.Name = "new_name" - flags.RequireMutable = false // Check that changes to the mutable descriptor don't impact the // collection until they're added as uncommitted - immByName, err := descriptors.GetImmutableDatabaseByName(ctx, txn, "db", flags) + immByName, err := descriptors.ByNameWithLeased(txn).Get().Database(ctx, "db") require.NoError(t, err) require.Equal(t, dbID, immByName.GetID()) require.Equal(t, mut.OriginalVersion(), immByName.GetVersion()) - _, immByID, err := descriptors.GetImmutableDatabaseByID(ctx, txn, dbID, flags) + immByID, err := descriptors.ByIDWithLeased(txn).WithoutNonPublic().Get().Database(ctx, dbID) require.NoError(t, err) require.Same(t, immByName, immByID) @@ -264,25 +254,25 @@ func TestAddUncommittedDescriptorAndMutableResolution(t *testing.T) { require.NoError(t, err) // Should be able to get the database descriptor by the new name. - resolved, err := descriptors.GetImmutableDatabaseByName(ctx, txn, "new_name", flags) + resolved, err := descriptors.ByNameWithLeased(txn).Get().Database(ctx, "new_name") require.Nil(t, err) require.Equal(t, dbID, resolved.GetID()) // Try to get the database descriptor by the old name and succeed but get // the old version with the old name because the new version has not yet // been written. - immResolvedWithNewNameButHasOldName, err := descriptors.GetImmutableDatabaseByName(ctx, txn, "db", flags) + immResolvedWithNewNameButHasOldName, err := descriptors.ByNameWithLeased(txn).Get().Database(ctx, "db") require.NoError(t, err) require.Same(t, immByID, immResolvedWithNewNameButHasOldName) require.NoError(t, descriptors.AddUncommittedDescriptor(ctx, mut)) - immByNameAfter, err := descriptors.GetImmutableDatabaseByName(ctx, txn, "new_name", flags) + immByNameAfter, err := descriptors.ByNameWithLeased(txn).Get().Database(ctx, "new_name") require.NoError(t, err) require.Equal(t, mut.GetVersion(), immByNameAfter.GetVersion()) require.Equal(t, mut.ImmutableCopy().DescriptorProto(), immByNameAfter.DescriptorProto()) - _, immByIDAfter, err := descriptors.GetImmutableDatabaseByID(ctx, txn, dbID, flags) + immByIDAfter, err := descriptors.ByIDWithLeased(txn).WithoutNonPublic().Get().Database(ctx, dbID) require.NoError(t, err) require.Same(t, immByNameAfter, immByIDAfter) @@ -293,24 +283,20 @@ func TestAddUncommittedDescriptorAndMutableResolution(t *testing.T) { require.NoError(t, sql.DescsTxn(ctx, &execCfg, func( ctx context.Context, txn *kv.Txn, descriptors *descs.Collection, ) error { - flags := tree.SchemaLookupFlags{} - flags.RequireMutable = true - flags.Required = true - - db, err := descriptors.GetMutableDatabaseByName(ctx, txn, "db", flags) + db, err := descriptors.MutableByName(txn).Database(ctx, "db") require.NoError(t, err) - schema, err := descriptors.GetMutableSchemaByName(ctx, txn, db, "sc", flags) + schema, err := descriptors.MutableByName(txn).Schema(ctx, db, "sc") require.NoError(t, err) require.NotNil(t, schema) - resolved, err := descriptors.GetMutableSchemaByName(ctx, txn, db, "sc", flags) + resolved, err := descriptors.MutableByName(txn).Schema(ctx, db, "sc") require.NoError(t, err) require.NotNil(t, schema) require.Same(t, schema, resolved) - byID, err := descriptors.GetMutableDescriptorByID(ctx, txn, schema.GetID()) + byID, err := descriptors.MutableByID(txn).Desc(ctx, schema.GetID()) require.NoError(t, err) require.Same(t, schema, byID) @@ -321,20 +307,17 @@ func TestAddUncommittedDescriptorAndMutableResolution(t *testing.T) { require.NoError(t, sql.DescsTxn(ctx, &execCfg, func( ctx context.Context, txn *kv.Txn, descriptors *descs.Collection, ) error { - flags := tree.ObjectLookupFlags{} - flags.RequireMutable = true - flags.Required = true tn := tree.MakeTableNameWithSchema("db", "sc", "tab") - _, tab, err := descriptors.GetMutableTableByName(ctx, txn, &tn, flags) + _, tab, err := descs.PrefixAndMutableTable(ctx, descriptors.MutableByName(txn), &tn) require.NoError(t, err) - _, resolved, err := descriptors.GetMutableTableByName(ctx, txn, &tn, flags) + _, resolved, err := descs.PrefixAndMutableTable(ctx, descriptors.MutableByName(txn), &tn) require.NoError(t, err) require.Same(t, tab, resolved) - byID, err := descriptors.GetMutableDescriptorByID(ctx, txn, tab.GetID()) + byID, err := descriptors.MutableByID(txn).Desc(ctx, tab.GetID()) require.NoError(t, err) require.Same(t, tab, byID) @@ -345,19 +328,16 @@ func TestAddUncommittedDescriptorAndMutableResolution(t *testing.T) { require.NoError(t, sql.DescsTxn(ctx, &execCfg, func( ctx context.Context, txn *kv.Txn, descriptors *descs.Collection, ) error { - flags := tree.ObjectLookupFlags{} - flags.RequireMutable = true - flags.Required = true tn := tree.MakeQualifiedTypeName("db", "sc", "typ") - _, typ, err := descriptors.GetMutableTypeByName(ctx, txn, &tn, flags) + _, typ, err := descs.PrefixAndMutableType(ctx, descriptors.MutableByName(txn), &tn) require.NoError(t, err) - _, resolved, err := descriptors.GetMutableTypeByName(ctx, txn, &tn, flags) + _, resolved, err := descs.PrefixAndMutableType(ctx, descriptors.MutableByName(txn), &tn) require.NoError(t, err) require.Same(t, typ, resolved) - byID, err := descriptors.GetMutableTypeVersionByID(ctx, txn, typ.GetID()) + byID, err := descriptors.MutableByID(txn).Type(ctx, typ.GetID()) require.NoError(t, err) require.Same(t, typ, byID) @@ -393,8 +373,8 @@ func TestSyntheticDescriptorResolution(t *testing.T) { ) error { // Resolve the descriptor so we can mutate it. tn := tree.MakeTableNameWithSchema("db", tree.PublicSchemaName, "tbl") - found, desc, err := descriptors.GetImmutableTableByName(ctx, txn, &tn, tree.ObjectLookupFlags{}) - require.True(t, found) + _, desc, err := descs.PrefixAndTable(ctx, descriptors.ByNameWithLeased(txn).MaybeGet(), &tn) + require.NotNil(t, desc) require.NoError(t, err) // Modify the column name. @@ -402,23 +382,23 @@ func TestSyntheticDescriptorResolution(t *testing.T) { descriptors.SetSyntheticDescriptors([]catalog.Descriptor{desc}) // Resolve the table by name again. - found, desc, err = descriptors.GetImmutableTableByName(ctx, txn, &tn, tree.ObjectLookupFlags{}) - require.True(t, found) + _, desc, err = descs.PrefixAndTable(ctx, descriptors.ByNameWithLeased(txn).MaybeGet(), &tn) + require.NotNil(t, desc) require.NoError(t, err) require.Equal(t, "bar", desc.PublicColumns()[0].GetName()) // Attempting to resolve the table mutably is not allowed. - _, _, err = descriptors.GetMutableTableByName(ctx, txn, &tn, tree.ObjectLookupFlags{}) + _, _, err = descs.PrefixAndMutableTable(ctx, descriptors.MutableByName(txn), &tn) require.EqualError(t, err, fmt.Sprintf("attempted mutable access of synthetic descriptor %d", tableID)) // Resolution by ID. - desc, err = descriptors.GetImmutableTableByID(ctx, txn, tableID, tree.ObjectLookupFlags{}) + desc, err = descriptors.ByIDWithLeased(txn).WithoutNonPublic().Get().Table(ctx, tableID) require.NoError(t, err) require.Equal(t, "bar", desc.PublicColumns()[0].GetName()) // Attempting to resolve the table mutably is not allowed. - _, err = descriptors.GetMutableTableByID(ctx, txn, tableID, tree.ObjectLookupFlags{}) + _, err = descriptors.MutableByID(txn).Table(ctx, tableID) require.EqualError(t, err, fmt.Sprintf("attempted mutable access of synthetic descriptor %d", tableID)) return nil @@ -494,13 +474,11 @@ CREATE TABLE test.schema.t(x INT); require.NoError(t, sql.DescsTxn(ctx, &execCfg, func( ctx context.Context, txn *kv.Txn, descsCol *descs.Collection, ) error { - dbDesc, err := descsCol.GetImmutableDatabaseByName( - ctx, txn, "test", tree.DatabaseLookupFlags{Required: true}, - ) + dbDesc, err := descsCol.ByNameWithLeased(txn).Get().Database(ctx, "test") if err != nil { return err } - schemaDesc, err := descsCol.GetMutableSchemaByName(ctx, txn, dbDesc, "schema", tree.SchemaLookupFlags{Required: true}) + schemaDesc, err := descsCol.MutableByName(txn).Schema(ctx, dbDesc, "schema") if err != nil { return err } @@ -558,7 +536,7 @@ func TestCollectionPreservesPostDeserializationChanges(t *testing.T) { require.NoError(t, sql.DescsTxn(ctx, &execCfg, func( ctx context.Context, txn *kv.Txn, col *descs.Collection, ) error { - descs, err := col.GetMutableDescriptorsByID(ctx, txn, dbID, scID, typID, tabID) + descs, err := col.MutableByID(txn).Descs(ctx, []descpb.ID{dbID, scID, typID, tabID}) if err != nil { return err } @@ -576,10 +554,7 @@ func TestCollectionPreservesPostDeserializationChanges(t *testing.T) { require.NoError(t, sql.DescsTxn(ctx, &execCfg, func( ctx context.Context, txn *kv.Txn, col *descs.Collection, ) error { - immuts, err := col.GetImmutableDescriptorsByID(ctx, txn, tree.CommonLookupFlags{ - Required: true, - AvoidLeased: true, - }, dbID, scID, typID, tabID) + immuts, err := col.ByID(txn).WithoutNonPublic().Get().Descs(ctx, []descpb.ID{dbID, scID, typID, tabID}) if err != nil { return err } @@ -596,7 +571,7 @@ func TestCollectionPreservesPostDeserializationChanges(t *testing.T) { require.NoError(t, sql.DescsTxn(ctx, &execCfg, func( ctx context.Context, txn *kv.Txn, col *descs.Collection, ) error { - muts, err := col.GetMutableDescriptorsByID(ctx, txn, dbID, scID, typID, tabID) + muts, err := col.MutableByID(txn).Descs(ctx, []descpb.ID{dbID, scID, typID, tabID}) if err != nil { return err } @@ -711,9 +686,7 @@ func TestDescriptorCache(t *testing.T) { } // Modify table descriptor. tn := tree.MakeTableNameWithSchema("db", "schema", "table") - flags := tree.ObjectLookupFlagsWithRequired() - flags.RequireMutable = true - _, mut, err := descriptors.GetMutableTableByName(ctx, txn, &tn, flags) + _, mut, err := descs.PrefixAndMutableTable(ctx, descriptors.MutableByName(txn), &tn) if err != nil { return err } @@ -747,9 +720,7 @@ func TestDescriptorCache(t *testing.T) { } require.Len(t, dbDescs, 4) // Modify database descriptor. - flags := tree.DatabaseLookupFlags{} - flags.RequireMutable = true - mut, err := descriptors.GetMutableDatabaseByName(ctx, txn, "db", flags) + mut, err := descriptors.MutableByName(txn).Database(ctx, "db") if err != nil { return err } @@ -776,7 +747,7 @@ func TestDescriptorCache(t *testing.T) { ) error { descriptors.SkipValidationOnWrite() // Warm up cache. - dbDesc, err := descriptors.GetMutableDatabaseByName(ctx, txn, "db", tree.DatabaseLookupFlags{}) + dbDesc, err := descriptors.MutableByName(txn).Database(ctx, "db") if err != nil { return err } @@ -785,7 +756,8 @@ func TestDescriptorCache(t *testing.T) { return err } // Modify schema name. - schemaDesc, err := descriptors.GetMutableSchemaByName(ctx, txn, dbDesc, "schema", tree.SchemaLookupFlags{Required: true}) + var db catalog.DatabaseDescriptor = dbDesc + schemaDesc, err := descriptors.MutableByName(txn).Schema(ctx, db, "schema") if err != nil { return err } @@ -839,7 +811,7 @@ func TestGetAllDescriptorsInDatabase(t *testing.T) { require.NoError(t, tm.DescsTxnWithExecutor(ctx, s0.DB(), sd, func( ctx context.Context, txn *kv.Txn, descriptors *descs.Collection, ie sqlutil.InternalExecutor, ) error { - dbDesc, err := descriptors.GetImmutableDatabaseByName(ctx, txn, "db", tree.DatabaseLookupFlags{AvoidLeased: true}) + dbDesc, err := descriptors.ByName(txn).Get().Database(ctx, "db") if err != nil { return err } @@ -1146,11 +1118,7 @@ SELECT id ctx context.Context, txn *kv.Txn, descriptors *descs.Collection, ie sqlutil.InternalExecutor, ) error { checkImmutableDescriptor := func(id descpb.ID, expName string, f func(t *testing.T, desc catalog.Descriptor)) error { - flags := tree.ObjectLookupFlagsWithRequired() - flags.IncludeDropped = true - tabImm, err := descriptors.GetImmutableTableByID( - ctx, txn, id, flags, - ) + tabImm, err := descriptors.ByIDWithLeased(txn).WithoutOffline().Get().Table(ctx, id) require.NoError(t, err) require.Equal(t, expName, tabImm.GetName()) f(t, tabImm) @@ -1164,9 +1132,7 @@ SELECT id // Modify the table to have the name "bar", synthetically { - tab, err := descriptors.GetMutableTableByID( - ctx, txn, tabID, tree.ObjectLookupFlagsWithRequired(), - ) + tab, err := descriptors.MutableByID(txn).Table(ctx, tabID) if err != nil { return err } @@ -1183,17 +1149,13 @@ SELECT id return err } // Attempt to retrieve the mutable descriptor, validate the error. - _, err := descriptors.GetMutableTableByID( - ctx, txn, tabID, tree.ObjectLookupFlagsWithRequired(), - ) + _, err := descriptors.MutableByID(txn).Table(ctx, tabID) require.Regexp(t, `attempted mutable access of synthetic descriptor \d+`, err) descriptors.ResetSyntheticDescriptors() // Retrieve the mutable descriptor, find the unmodified "foo". // Then modify the name to "baz" and write it. { - tabMut, err := descriptors.GetMutableTableByID( - ctx, txn, tabID, tree.ObjectLookupFlagsWithRequired(), - ) + tabMut, err := descriptors.MutableByID(txn).Table(ctx, tabID) require.NoError(t, err) require.Equal(t, "foo", tabMut.GetName()) tabMut.Name = "baz" @@ -1226,7 +1188,7 @@ SELECT id newDB := dbdesc.NewInitial(newDBID, "newDB", username.RootUserName()) descriptors.AddSyntheticDescriptor(newDB) - _, curDatabase, err := descriptors.GetImmutableDatabaseByID(ctx, txn, curDatabaseID, tree.DatabaseLookupFlags{}) + curDatabase, err := descriptors.ByIDWithLeased(txn).WithoutNonPublic().Get().Database(ctx, curDatabaseID) if err != nil { return err } @@ -1250,7 +1212,7 @@ SELECT id } // Rename a schema synthetically, make sure that that propagates. - scDesc, err := descriptors.GetMutableSchemaByID(ctx, txn, scID, tree.SchemaLookupFlags{}) + scDesc, err := descriptors.MutableByID(txn).Schema(ctx, scID) if err != nil { return err } diff --git a/pkg/sql/catalog/descs/database.go b/pkg/sql/catalog/descs/database.go deleted file mode 100644 index 77645f533ada..000000000000 --- a/pkg/sql/catalog/descs/database.go +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright 2021 The Cockroach Authors. -// -// Use of this software is governed by the Business Source License -// included in the file licenses/BSL.txt. -// -// As of the Change Date specified in that file, in accordance with -// the Business Source License, use of this software will be governed -// by the Apache License, Version 2.0, included in the file -// licenses/APL.txt. - -package descs - -import ( - "context" - - "github.com/cockroachdb/cockroach/pkg/kv" - "github.com/cockroachdb/cockroach/pkg/sql/catalog" - "github.com/cockroachdb/cockroach/pkg/sql/catalog/dbdesc" - "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" - "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" -) - -// GetMutableDatabaseByName returns a mutable database descriptor with -// properties according to the provided lookup flags. RequireMutable is ignored. -func (tc *Collection) GetMutableDatabaseByName( - ctx context.Context, txn *kv.Txn, name string, flags tree.DatabaseLookupFlags, -) (*dbdesc.Mutable, error) { - return tc.ByName(txn).WithFlags(flags).Mutable().Database(ctx, name) -} - -// GetImmutableDatabaseByName returns an immutable database descriptor with -// properties according to the provided lookup flags. RequireMutable is ignored. -func (tc *Collection) GetImmutableDatabaseByName( - ctx context.Context, txn *kv.Txn, name string, flags tree.DatabaseLookupFlags, -) (catalog.DatabaseDescriptor, error) { - return tc.ByName(txn).WithFlags(flags).Immutable().Database(ctx, name) -} - -// GetImmutableDatabaseByID returns an immutable database descriptor with -// properties according to the provided lookup flags. RequireMutable is ignored. -func (tc *Collection) GetImmutableDatabaseByID( - ctx context.Context, txn *kv.Txn, dbID descpb.ID, flags tree.DatabaseLookupFlags, -) (bool, catalog.DatabaseDescriptor, error) { - db, err := tc.ByID(txn).WithFlags(flags).Immutable().Database(ctx, dbID) - return db != nil, db, err -} diff --git a/pkg/sql/catalog/descs/descriptor.go b/pkg/sql/catalog/descs/descriptor.go index 160dd4c5d6a0..64163b3c19de 100644 --- a/pkg/sql/catalog/descs/descriptor.go +++ b/pkg/sql/catalog/descs/descriptor.go @@ -29,37 +29,6 @@ import ( "github.com/cockroachdb/errors" ) -// GetMutableDescriptorsByID returns a mutable implementation of the descriptors -// with the requested ids. An error is returned if no descriptor exists. -func (tc *Collection) GetMutableDescriptorsByID( - ctx context.Context, txn *kv.Txn, ids ...descpb.ID, -) ([]catalog.MutableDescriptor, error) { - return tc.ByID(txn).Mutable().Descs(ctx, ids) -} - -// GetMutableDescriptorByID delegates to GetMutableDescriptorsByID. -func (tc *Collection) GetMutableDescriptorByID( - ctx context.Context, txn *kv.Txn, id descpb.ID, -) (catalog.MutableDescriptor, error) { - return tc.ByID(txn).Mutable().Desc(ctx, id) -} - -// GetImmutableDescriptorsByID returns an immutable implementation of the -// descriptors with the requested ids. An error is returned if no descriptor -// exists, regardless of whether the Required flag is set or not. -func (tc *Collection) GetImmutableDescriptorsByID( - ctx context.Context, txn *kv.Txn, flags tree.CommonLookupFlags, ids ...descpb.ID, -) ([]catalog.Descriptor, error) { - return tc.ByID(txn).WithFlags(flags).Immutable().Descs(ctx, ids) -} - -// GetImmutableDescriptorByID delegates to GetImmutableDescriptorsByID. -func (tc *Collection) GetImmutableDescriptorByID( - ctx context.Context, txn *kv.Txn, id descpb.ID, flags tree.CommonLookupFlags, -) (catalog.Descriptor, error) { - return tc.ByID(txn).WithFlags(flags).Immutable().Desc(ctx, id) -} - // GetComment fetches comment from uncommitted cache if it exists, otherwise from storage. func (tc *Collection) GetComment(key catalogkeys.CommentKey) (string, bool) { if cmt, hasCmt, cached := tc.uncommittedComments.getUncommitted(key); cached { diff --git a/pkg/sql/catalog/descs/dist_sql_type_resolver.go b/pkg/sql/catalog/descs/dist_sql_type_resolver.go index 72e5c93df503..1e5676bceafd 100644 --- a/pkg/sql/catalog/descs/dist_sql_type_resolver.go +++ b/pkg/sql/catalog/descs/dist_sql_type_resolver.go @@ -34,7 +34,7 @@ type DistSQLTypeResolver struct { // NewDistSQLTypeResolver creates a new DistSQLTypeResolver. func NewDistSQLTypeResolver(descs *Collection, txn *kv.Txn) DistSQLTypeResolver { return DistSQLTypeResolver{ - g: descs.ByID(txn).Immutable(), + g: descs.ByIDWithLeased(txn).Get(), } } diff --git a/pkg/sql/catalog/descs/function.go b/pkg/sql/catalog/descs/function.go deleted file mode 100644 index 919cc6169b6e..000000000000 --- a/pkg/sql/catalog/descs/function.go +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright 2022 The Cockroach Authors. -// -// Use of this software is governed by the Business Source License -// included in the file licenses/BSL.txt. -// -// As of the Change Date specified in that file, in accordance with -// the Business Source License, use of this software will be governed -// by the Apache License, Version 2.0, included in the file -// licenses/APL.txt. - -package descs - -import ( - "context" - - "github.com/cockroachdb/cockroach/pkg/kv" - "github.com/cockroachdb/cockroach/pkg/sql/catalog" - "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" - "github.com/cockroachdb/cockroach/pkg/sql/catalog/funcdesc" - "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" -) - -// GetImmutableFunctionByID returns a immutable function descriptor. -func (tc *Collection) GetImmutableFunctionByID( - ctx context.Context, txn *kv.Txn, fnID descpb.ID, flags tree.ObjectLookupFlags, -) (catalog.FunctionDescriptor, error) { - return tc.ByID(txn).WithObjFlags(flags).Immutable().Function(ctx, fnID) -} - -// GetMutableFunctionByID returns a mutable function descriptor. -func (tc *Collection) GetMutableFunctionByID( - ctx context.Context, txn *kv.Txn, fnID descpb.ID, flags tree.ObjectLookupFlags, -) (*funcdesc.Mutable, error) { - return tc.ByID(txn).WithObjFlags(flags).Mutable().Function(ctx, fnID) -} diff --git a/pkg/sql/catalog/descs/getters.go b/pkg/sql/catalog/descs/getters.go index 44f43d68298e..80386a692a6f 100644 --- a/pkg/sql/catalog/descs/getters.go +++ b/pkg/sql/catalog/descs/getters.go @@ -24,6 +24,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog/typedesc" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" + "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/errors" @@ -57,9 +58,6 @@ func (g ByIDGetter) Database( desc, err := g.Desc(ctx, id) if err != nil { if errors.Is(err, catalog.ErrDescriptorNotFound) { - if g.flags.isOptional { - return nil, nil - } return nil, sqlerrors.NewUndefinedDatabaseError(fmt.Sprintf("[%d]", id)) } return nil, err @@ -76,9 +74,6 @@ func (g ByIDGetter) Schema(ctx context.Context, id descpb.ID) (catalog.SchemaDes desc, err := g.Desc(ctx, id) if err != nil { if errors.Is(err, catalog.ErrDescriptorNotFound) { - if g.flags.isOptional { - return nil, nil - } return nil, sqlerrors.NewUndefinedSchemaError(fmt.Sprintf("[%d]", id)) } return nil, err @@ -136,6 +131,10 @@ func (g ByIDGetter) Type(ctx context.Context, id descpb.ID) (catalog.TypeDescrip pgcode.UndefinedObject, "type with ID %d does not exist", id) } +// ErrMutableTableImplicitType indicates that a table implicit type was fetched +// as a mutable, which is not allowed. +var ErrMutableTableImplicitType = pgerror.Newf(pgcode.DependentObjectsStillExist, "table implicit type not mutable") + // Function looks up an immutable function descriptor by ID. func (g ByIDGetter) Function( ctx context.Context, id descpb.ID, @@ -444,10 +443,6 @@ type getterFlags struct { type contextFlags struct { // isOptional specifies that the descriptor is being looked up on // a best-effort basis. - // - // Presently, for historical reasons, this is overridden to true for - // all mutable by-ID lookups, and for all immutable by-ID object lookups. - // TODO(postamar): clean this up isOptional bool // isMutable specifies that a mutable descriptor is to be returned. isMutable bool @@ -472,18 +467,9 @@ type layerFilters struct { type descFilters struct { // withoutDropped specifies to raise an error if the looked-up descriptor // is in the DROP state. - // - // Presently, for historical reasons, this is overridden everywhere except - // for immutable by-ID lookups: to true for by-name lookups and to false for - // mutable by-ID lookups. - // TODO(postamar): clean this up withoutDropped bool // withoutOffline specifies to raise an error if the looked-up descriptor // is in the OFFLINE state. - // - // Presently, for historical reasons, this is overridden to true for mutable - // by-ID lookups. - // TODO(postamar): clean this up withoutOffline bool // withoutCommittedAdding specifies if committed descriptors in the // adding state will be ignored. @@ -493,69 +479,86 @@ type descFilters struct { maybeParentID descpb.ID } -func fromCommonFlags(flags tree.CommonLookupFlags) (f getterFlags) { - return getterFlags{ - contextFlags: contextFlags{ - isOptional: !flags.Required, - isMutable: flags.RequireMutable, - }, +func defaultUnleasedFlags() (f getterFlags) { + f.layerFilters.withoutLeased = true + return f +} + +// ByID returns a ByIDGetterBuilder set up to look up descriptors by ID +// in all layers except the leased descriptors layer. To opt in to the +// leased descriptors, use ByIDWithLeased instead. +func (tc *Collection) ByID(txn *kv.Txn) ByIDGetterBuilder { + return ByIDGetterBuilder(makeGetterBase(txn, tc, getterFlags{ layerFilters: layerFilters{ - withoutSynthetic: flags.AvoidSynthetic, - withoutLeased: flags.AvoidLeased, - }, - descFilters: descFilters{ - withoutDropped: !flags.IncludeDropped, - withoutOffline: !flags.IncludeOffline, - maybeParentID: flags.ParentID, + withoutLeased: true, }, - } + })) } -func fromObjectFlags(flags tree.ObjectLookupFlags) getterFlags { - return fromCommonFlags(flags.CommonLookupFlags) +// ByIDWithLeased is like ByID but also looks up in the leased descriptors +// layer. This may save a round-trip to KV at the expense of the descriptor +// being slightly stale (one version off). +func (tc *Collection) ByIDWithLeased(txn *kv.Txn) ByIDGetterBuilder { + return ByIDGetterBuilder(makeGetterBase(txn, tc, getterFlags{})) } -func defaultFlags() getterFlags { - return fromCommonFlags(tree.CommonLookupFlags{}) +// MutableByID returns a MutableByIDGetter. +// This convenience method exists because mutable lookups never require +// any customization. +func (tc *Collection) MutableByID(txn *kv.Txn) MutableByIDGetter { + return MutableByIDGetter(makeGetterBase(txn, tc, getterFlags{ + contextFlags: contextFlags{ + isMutable: true, + }, + layerFilters: layerFilters{ + withoutLeased: true, + }, + })) } -func defaultUnleasedFlags() (f getterFlags) { - f.layerFilters.withoutLeased = true - return f -} +// ByIDGetterBuilder is a builder object for ByIDGetter and MutableByIDGetter. +type ByIDGetterBuilder getterBase -// ByID returns a ByIDGetterBuilder. -func (tc *Collection) ByID(txn *kv.Txn) ByIDGetterBuilder { - return ByIDGetterBuilder(makeGetterBase(txn, tc, defaultFlags())) +// WithoutSynthetic configures the ByIDGetterBuilder to bypass the synthetic +// layer. This is useful mainly for the declarative schema changer, which is +// the main client of this layer. +func (b ByIDGetterBuilder) WithoutSynthetic() ByIDGetterBuilder { + b.flags.layerFilters.withoutSynthetic = true + return b } -// ByIDGetterBuilder is a builder object for ByIDGetter and MutableByIDGetter. -type ByIDGetterBuilder getterBase +// WithoutDropped configures the ByIDGetterBuilder to error on descriptors +// which are in a dropped state. +func (b ByIDGetterBuilder) WithoutDropped() ByIDGetterBuilder { + b.flags.descFilters.withoutDropped = true + return b +} -// WithFlags configures the ByIDGetterBuilder with the given flags. -func (b ByIDGetterBuilder) WithFlags(flags tree.CommonLookupFlags) ByIDGetterBuilder { - b.flags = fromCommonFlags(flags) +// WithoutOffline configures the ByIDGetterBuilder to error on descriptors +// which are in an offline state. +func (b ByIDGetterBuilder) WithoutOffline() ByIDGetterBuilder { + b.flags.descFilters.withoutOffline = true return b } -// WithObjFlags configures the ByIDGetterBuilder with the given object flags. -func (b ByIDGetterBuilder) WithObjFlags(flags tree.ObjectLookupFlags) ByIDGetterBuilder { - b.flags = fromObjectFlags(flags) +// WithoutNonPublic configures the ByIDGetterBuilder to error on descriptors +// which are not in a public state: dropped, offline, etc. +func (b ByIDGetterBuilder) WithoutNonPublic() ByIDGetterBuilder { + b.flags.descFilters.withoutDropped = true + b.flags.descFilters.withoutOffline = true return b } -// Mutable builds a MutableByIDGetter. -func (b ByIDGetterBuilder) Mutable() MutableByIDGetter { - b.flags.isOptional = false - b.flags.isMutable = true - b.flags.layerFilters.withoutLeased = true - b.flags.descFilters.withoutDropped = false - b.flags.descFilters.withoutOffline = false - return MutableByIDGetter(b) +// WithoutOtherParent configures the ByIDGetterBuilder to error on descriptors +// which, if they have a parent ID, have one different than the argument. +// The argument must be non-zero for this filter to be effective. +func (b ByIDGetterBuilder) WithoutOtherParent(parentID catid.DescID) ByIDGetterBuilder { + b.flags.descFilters.maybeParentID = parentID + return b } -// Immutable builds a ByIDGetter. -func (b ByIDGetterBuilder) Immutable() ByIDGetter { +// Get builds a ByIDGetter. +func (b ByIDGetterBuilder) Get() ByIDGetter { if b.flags.isMutable { b.flags.layerFilters.withoutLeased = true b.flags.isMutable = false @@ -563,40 +566,69 @@ func (b ByIDGetterBuilder) Immutable() ByIDGetter { return ByIDGetter(b) } -// ByName returns a ByNameGetterBuilder. +// ByName returns a ByNameGetterBuilder set up to look up +// descriptors by name in all layers except the leased descriptors layer. +// To opt in to the leased descriptors, use ByNameWithLeased instead. func (tc *Collection) ByName(txn *kv.Txn) ByNameGetterBuilder { - return ByNameGetterBuilder(makeGetterBase(txn, tc, defaultFlags())) + return ByNameGetterBuilder(makeGetterBase(txn, tc, getterFlags{ + layerFilters: layerFilters{ + withoutLeased: true, + }, + descFilters: descFilters{ + withoutOffline: true, + withoutDropped: true, + }, + })) } -// ByNameGetterBuilder is a builder object for ByNameGetter and MutableByNameGetter. -type ByNameGetterBuilder getterBase +// ByNameWithLeased is like ByName but also looks up in the +// leased descriptors layer. This may save a round-trip to KV at the expense +// of the descriptor being slightly stale (one version off). +func (tc *Collection) ByNameWithLeased(txn *kv.Txn) ByNameGetterBuilder { + return ByNameGetterBuilder(makeGetterBase(txn, tc, getterFlags{ + descFilters: descFilters{ + withoutOffline: true, + withoutDropped: true, + }, + })) +} -// WithFlags configures the ByIDGetterBuilder with the given flags. -func (b ByNameGetterBuilder) WithFlags(flags tree.CommonLookupFlags) ByNameGetterBuilder { - b.flags = fromCommonFlags(flags) - return b +// MutableByName returns a MutableByNameGetter. +// This convenience method exists because mutable lookups never require +// any customization. +func (tc *Collection) MutableByName(txn *kv.Txn) MutableByNameGetter { + return MutableByNameGetter(makeGetterBase(txn, tc, getterFlags{ + contextFlags: contextFlags{ + isMutable: true, + }, + layerFilters: layerFilters{ + withoutLeased: true, + }, + descFilters: descFilters{ + withoutOffline: true, + withoutDropped: true, + }, + })) } -// WithObjFlags configures the ByNameGetterBuilder with the given object flags. -func (b ByNameGetterBuilder) WithObjFlags(flags tree.ObjectLookupFlags) ByNameGetterBuilder { - b.flags = fromObjectFlags(flags) +// ByNameGetterBuilder is a builder object for ByNameGetter and MutableByNameGetter. +type ByNameGetterBuilder getterBase + +// WithOffline configures the ByNameGetterBuilder to allow lookups +// of offline descriptors. +func (b ByNameGetterBuilder) WithOffline() ByNameGetterBuilder { + b.flags.descFilters.withoutOffline = false return b } -// Mutable builds a MutableByNameGetter. -func (b ByNameGetterBuilder) Mutable() MutableByNameGetter { - b.flags.isMutable = true - b.flags.layerFilters.withoutLeased = true - b.flags.descFilters.withoutDropped = true - return MutableByNameGetter(b) +// Get builds a ByNameGetter. +func (b ByNameGetterBuilder) Get() ByNameGetter { + return ByNameGetter(b) } -// Immutable builds a ByNameGetter. -func (b ByNameGetterBuilder) Immutable() ByNameGetter { - if b.flags.isMutable { - b.flags.layerFilters.withoutLeased = true - b.flags.isMutable = false - } - b.flags.descFilters.withoutDropped = true +// MaybeGet builds a ByNameGetter which returns a nil descriptor instead of +// an error when the descriptor is not found. +func (b ByNameGetterBuilder) MaybeGet() ByNameGetter { + b.flags.contextFlags.isOptional = true return ByNameGetter(b) } diff --git a/pkg/sql/catalog/descs/helpers.go b/pkg/sql/catalog/descs/helpers.go index d11a04c2d4ee..809c79cb2654 100644 --- a/pkg/sql/catalog/descs/helpers.go +++ b/pkg/sql/catalog/descs/helpers.go @@ -17,6 +17,8 @@ import ( "github.com/cockroachdb/cockroach/pkg/kv" "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/typedesc" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sqlerrors" "github.com/cockroachdb/errors" @@ -26,7 +28,7 @@ import ( func GetObjectName( ctx context.Context, txn *kv.Txn, tc *Collection, obj catalog.Descriptor, ) (tree.ObjectName, error) { - g := tc.ByID(txn).Immutable() + g := tc.ByIDWithLeased(txn).Get() sc, err := g.Schema(ctx, obj.GetParentSchemaID()) if err != nil { return nil, err @@ -83,6 +85,10 @@ func CheckObjectNameCollision( func getObjectPrefix( ctx context.Context, g ByNameGetter, dbName, scName string, ) (prefix catalog.ResolvedObjectPrefix, err error) { + if g.flags.isMutable { + g.flags.isMutable = false + g.flags.layerFilters.withoutLeased = true + } // If we're reading the object descriptor from the store, // we should read its parents from the store too to ensure // that subsequent name resolution finds the latest name @@ -97,6 +103,54 @@ func getObjectPrefix( return prefix, err } +// PrefixAndType looks up an immutable type descriptor by its full name. +func PrefixAndType( + ctx context.Context, g ByNameGetter, name tree.ObjectName, +) (catalog.ResolvedObjectPrefix, catalog.TypeDescriptor, error) { + p, err := getObjectPrefix(ctx, g, name.Catalog(), name.Schema()) + if err != nil || p.Schema == nil { + return p, nil, err + } + typ, err := g.Type(ctx, p.Database, p.Schema, name.Object()) + return p, typ, err +} + +// PrefixAndMutableType looks up a mutable type descriptor by its full name. +func PrefixAndMutableType( + ctx context.Context, g MutableByNameGetter, name tree.ObjectName, +) (catalog.ResolvedObjectPrefix, *typedesc.Mutable, error) { + p, err := getObjectPrefix(ctx, ByNameGetter(g), name.Catalog(), name.Schema()) + if err != nil || p.Schema == nil { + return p, nil, err + } + typ, err := g.Type(ctx, p.Database, p.Schema, name.Object()) + return p, typ, err +} + +// PrefixAndTable looks up an immutable table descriptor by its full name. +func PrefixAndTable( + ctx context.Context, g ByNameGetter, name tree.ObjectName, +) (catalog.ResolvedObjectPrefix, catalog.TableDescriptor, error) { + p, err := getObjectPrefix(ctx, g, name.Catalog(), name.Schema()) + if err != nil || p.Schema == nil { + return p, nil, err + } + tbl, err := g.Table(ctx, p.Database, p.Schema, name.Object()) + return p, tbl, err +} + +// PrefixAndMutableTable looks up a mutable table descriptor by its full name. +func PrefixAndMutableTable( + ctx context.Context, g MutableByNameGetter, name tree.ObjectName, +) (catalog.ResolvedObjectPrefix, *tabledesc.Mutable, error) { + p, err := getObjectPrefix(ctx, ByNameGetter(g), name.Catalog(), name.Schema()) + if err != nil || p.Schema == nil { + return p, nil, err + } + tbl, err := g.Table(ctx, p.Database, p.Schema, name.Object()) + return p, tbl, err +} + // AsZoneConfigHydrationHelper returns the collection as a // catalog.ZoneConfigHydrationHelper func AsZoneConfigHydrationHelper(tc *Collection) catalog.ZoneConfigHydrationHelper { diff --git a/pkg/sql/catalog/descs/schema.go b/pkg/sql/catalog/descs/schema.go index 29b14ece45b6..85e8fbfb1fb1 100644 --- a/pkg/sql/catalog/descs/schema.go +++ b/pkg/sql/catalog/descs/schema.go @@ -17,60 +17,9 @@ import ( "github.com/cockroachdb/cockroach/pkg/kv" "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" - "github.com/cockroachdb/cockroach/pkg/sql/catalog/schemadesc" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" ) -// GetMutableSchemaByName resolves the schema and returns a mutable descriptor -// usable by the transaction. RequireMutable is ignored. -// -// TODO(ajwerner): Change this to take database by name to avoid any weirdness -// due to the descriptor being passed in having been cached and causing -// problems. -func (tc *Collection) GetMutableSchemaByName( - ctx context.Context, - txn *kv.Txn, - db catalog.DatabaseDescriptor, - schemaName string, - flags tree.SchemaLookupFlags, -) (*schemadesc.Mutable, error) { - return tc.ByName(txn).WithFlags(flags).Mutable().Schema(ctx, db, schemaName) -} - -// GetImmutableSchemaByName returns a catalog.SchemaDescriptor object if the -// target schema exists under the target database. RequireMutable is ignored. -// -// TODO(ajwerner): Change this to take database by name to avoid any weirdness -// due to the descriptor being passed in having been cached and causing -// problems. -func (tc *Collection) GetImmutableSchemaByName( - ctx context.Context, - txn *kv.Txn, - db catalog.DatabaseDescriptor, - scName string, - flags tree.SchemaLookupFlags, -) (catalog.SchemaDescriptor, error) { - 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( - ctx context.Context, txn *kv.Txn, schemaID descpb.ID, flags tree.SchemaLookupFlags, -) (*schemadesc.Mutable, error) { - return tc.ByID(txn).WithFlags(flags).Mutable().Schema(ctx, schemaID) -} - // InsertDescriptorlessPublicSchemaToBatch adds the creation of a new descriptorless public // schema to the batch. func (tc *Collection) InsertDescriptorlessPublicSchemaToBatch( diff --git a/pkg/sql/catalog/descs/table.go b/pkg/sql/catalog/descs/table.go index a9f04b2ed51a..d8938aded6fe 100644 --- a/pkg/sql/catalog/descs/table.go +++ b/pkg/sql/catalog/descs/table.go @@ -17,37 +17,8 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc" - "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" ) -// GetMutableTableByName returns a mutable table descriptor with properties -// according to the provided lookup flags. RequireMutable is ignored. -func (tc *Collection) GetMutableTableByName( - ctx context.Context, txn *kv.Txn, name tree.ObjectName, objectFlags tree.ObjectLookupFlags, -) (found bool, _ *tabledesc.Mutable, _ error) { - b := tc.ByName(txn).WithObjFlags(objectFlags) - p, err := getObjectPrefix(ctx, b.Immutable(), name.Catalog(), name.Schema()) - if err != nil || p.Schema == nil { - return false, nil, err - } - tbl, err := b.Mutable().Table(ctx, p.Database, p.Schema, name.Object()) - return tbl != nil, tbl, err -} - -// GetImmutableTableByName returns a immutable table descriptor with properties -// according to the provided lookup flags. RequireMutable is ignored. -func (tc *Collection) GetImmutableTableByName( - ctx context.Context, txn *kv.Txn, name tree.ObjectName, objectFlags tree.ObjectLookupFlags, -) (found bool, _ catalog.TableDescriptor, _ error) { - g := tc.ByName(txn).WithObjFlags(objectFlags).Immutable() - p, err := getObjectPrefix(ctx, g, name.Catalog(), name.Schema()) - if err != nil || p.Schema == nil { - return false, nil, err - } - tbl, err := g.Table(ctx, p.Database, p.Schema, name.Object()) - return tbl != nil, tbl, err -} - // GetLeasedImmutableTableByID returns a leased immutable table descriptor by // its ID. func (tc *Collection) GetLeasedImmutableTableByID( @@ -58,7 +29,13 @@ func (tc *Collection) GetLeasedImmutableTableByID( return nil, err } descs := []catalog.Descriptor{desc} - err = tc.hydrateDescriptors(ctx, txn, defaultFlags(), descs) + hydrateFlags := getterFlags{ + descFilters: descFilters{ + withoutDropped: true, + withoutOffline: true, + }, + } + err = tc.hydrateDescriptors(ctx, txn, hydrateFlags, descs) if err != nil { return nil, err } @@ -86,33 +63,3 @@ func (tc *Collection) GetUncommittedMutableTableByID( } return original.(catalog.TableDescriptor), mut.(*tabledesc.Mutable), nil } - -// GetMutableTableByID returns a mutable table descriptor with -// properties according to the provided lookup flags. RequireMutable is ignored. -// Required is ignored, and an error is always returned if no descriptor with -// the ID exists. -func (tc *Collection) GetMutableTableByID( - ctx context.Context, txn *kv.Txn, tableID descpb.ID, flags tree.ObjectLookupFlags, -) (*tabledesc.Mutable, error) { - return tc.ByID(txn).WithObjFlags(flags).Mutable().Table(ctx, tableID) -} - -// GetMutableTableVersionByID is a variant of sqlbase.getTableDescFromID which returns a mutable -// table descriptor of the table modified in the same transaction. -// TODO (lucy): Usages should be replaced with GetMutableTableByID, but this -// needs a careful look at what flags should be passed in at each call site. -func (tc *Collection) GetMutableTableVersionByID( - ctx context.Context, tableID descpb.ID, txn *kv.Txn, -) (*tabledesc.Mutable, error) { - return tc.ByID(txn).Mutable().Table(ctx, tableID) -} - -// GetImmutableTableByID returns an immutable table descriptor with -// properties according to the provided lookup flags. RequireMutable is ignored. -// Required is ignored, and an error is always returned if no descriptor with -// the ID exists. -func (tc *Collection) GetImmutableTableByID( - ctx context.Context, txn *kv.Txn, tableID descpb.ID, flags tree.ObjectLookupFlags, -) (catalog.TableDescriptor, error) { - return tc.ByID(txn).WithObjFlags(flags).Immutable().Table(ctx, tableID) -} diff --git a/pkg/sql/catalog/descs/type.go b/pkg/sql/catalog/descs/type.go deleted file mode 100644 index 86ac02040ed6..000000000000 --- a/pkg/sql/catalog/descs/type.go +++ /dev/null @@ -1,85 +0,0 @@ -// Copyright 2021 The Cockroach Authors. -// -// Use of this software is governed by the Business Source License -// included in the file licenses/BSL.txt. -// -// As of the Change Date specified in that file, in accordance with -// the Business Source License, use of this software will be governed -// by the Apache License, Version 2.0, included in the file -// licenses/APL.txt. - -package descs - -import ( - "context" - - "github.com/cockroachdb/cockroach/pkg/kv" - "github.com/cockroachdb/cockroach/pkg/sql/catalog" - "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" - "github.com/cockroachdb/cockroach/pkg/sql/catalog/typedesc" - "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" - "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" - "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" -) - -// ErrMutableTableImplicitType indicates that a table implicit type was fetched -// as a mutable, which is not allowed. -var ErrMutableTableImplicitType = pgerror.Newf(pgcode.DependentObjectsStillExist, "table implicit type not mutable") - -// GetMutableTypeByName returns a mutable type descriptor with properties -// according to the provided lookup flags. RequireMutable is ignored. -func (tc *Collection) GetMutableTypeByName( - ctx context.Context, txn *kv.Txn, name tree.ObjectName, objectFlags tree.ObjectLookupFlags, -) (found bool, _ *typedesc.Mutable, _ error) { - b := tc.ByName(txn).WithObjFlags(objectFlags) - p, err := getObjectPrefix(ctx, b.Immutable(), name.Catalog(), name.Schema()) - if err != nil || p.Schema == nil { - return false, nil, err - } - typ, err := b.Mutable().Type(ctx, p.Database, p.Schema, name.Object()) - return typ != nil, typ, err -} - -// GetImmutableTypeByName returns a mutable type descriptor with properties -// according to the provided lookup flags. RequireMutable is ignored. -func (tc *Collection) GetImmutableTypeByName( - ctx context.Context, txn *kv.Txn, name tree.ObjectName, objectFlags tree.ObjectLookupFlags, -) (found bool, _ catalog.TypeDescriptor, _ error) { - g := tc.ByName(txn).WithObjFlags(objectFlags).Immutable() - p, err := getObjectPrefix(ctx, g, name.Catalog(), name.Schema()) - if err != nil || p.Schema == nil { - return false, nil, err - } - typ, err := g.Type(ctx, p.Database, p.Schema, name.Object()) - return typ != nil, typ, err -} - -// GetMutableTypeVersionByID is the equivalent of GetMutableTableDescriptorByID -// but for accessing types. -// TODO (lucy): Usages should be replaced with GetMutableTypeByID, but this -// needs a careful look at what flags should be passed in at each call site. -func (tc *Collection) GetMutableTypeVersionByID( - ctx context.Context, txn *kv.Txn, typeID descpb.ID, -) (*typedesc.Mutable, error) { - return tc.ByID(txn).Mutable().Type(ctx, typeID) -} - -// GetMutableTypeByID returns a mutable type descriptor with -// properties according to the provided lookup flags. RequireMutable is ignored. -// Required is ignored, and an error is always returned if no descriptor with -// the ID exists. -func (tc *Collection) GetMutableTypeByID( - ctx context.Context, txn *kv.Txn, typeID descpb.ID, flags tree.ObjectLookupFlags, -) (*typedesc.Mutable, error) { - return tc.ByID(txn).WithObjFlags(flags).Mutable().Type(ctx, typeID) -} - -// GetImmutableTypeByID returns an immutable type descriptor with -// properties according to the provided lookup flags. RequireMutable is ignored. -// Required is ignored, and an error is always returned if no descriptor with -// the ID exists. -func (tc *Collection) GetImmutableTypeByID( - ctx context.Context, txn *kv.Txn, typeID descpb.ID, flags tree.ObjectLookupFlags, -) (catalog.TypeDescriptor, error) { - return tc.ByID(txn).WithObjFlags(flags).Immutable().Type(ctx, typeID) -} diff --git a/pkg/sql/catalog/hydrateddesccache/hydratedcache.go b/pkg/sql/catalog/hydrateddesccache/hydratedcache.go index f974ec78cf2b..61dcb1a549de 100644 --- a/pkg/sql/catalog/hydrateddesccache/hydratedcache.go +++ b/pkg/sql/catalog/hydrateddesccache/hydratedcache.go @@ -242,7 +242,7 @@ func (c *Cache) GetHydratedSchemaDescriptor( return hydratedDesc.(catalog.SchemaDescriptor), nil } -// GetHydratedDescriptor returns an Immutable copy with the types hydrated. It +// GetHydratedDescriptor returns a descriptor copy with the types hydrated. It // may use a cached copy but all of the relevant type descriptors will be // retrieved via the TypeDescriptorResolver. Note that if the given descriptor // is modified, nil will be returned. If any of the types used by the descriptor diff --git a/pkg/sql/catalog/ingesting/privileges.go b/pkg/sql/catalog/ingesting/privileges.go index f0ab40534501..0e4758090cec 100644 --- a/pkg/sql/catalog/ingesting/privileges.go +++ b/pkg/sql/catalog/ingesting/privileges.go @@ -115,11 +115,7 @@ func getIngestingPrivilegesForTableOrSchema( privilege.ListFromBitField(u.Privileges, privilegeType).ToBitField() } } else if descCoverage == tree.RequestedDescriptors { - _, parentDB, err := descsCol.GetImmutableDatabaseByID(ctx, txn, desc.GetParentID(), tree.DatabaseLookupFlags{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - }) + parentDB, err := descsCol.ByID(txn).Get().Database(ctx, desc.GetParentID()) if err != nil { return nil, errors.Wrapf(err, "failed to lookup parent DB %d", errors.Safe(desc.GetParentID())) } @@ -145,11 +141,7 @@ 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{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - }) + parentSchema, err := descsCol.ByID(txn).Get().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/catalog/ingesting/write_descs.go b/pkg/sql/catalog/ingesting/write_descs.go index 7af499bed3d0..f3da1865f58c 100644 --- a/pkg/sql/catalog/ingesting/write_descs.go +++ b/pkg/sql/catalog/ingesting/write_descs.go @@ -213,7 +213,7 @@ func WriteDescriptors( if mut, ok := fn.(*funcdesc.Mutable); ok { mut.Privileges = updatedPrivileges } else { - log.Fatalf(ctx, "wrong type for function %d, %T, expected funcdesc.Mutable", fn.GetID(), fn) + log.Fatalf(ctx, "wrong type for function %d, %T, expected Mutable", fn.GetID(), fn) } } if err := descsCol.WriteDescToBatch( @@ -239,12 +239,7 @@ func WriteDescriptors( func processTableForMultiRegion( ctx context.Context, txn *kv.Txn, descsCol *descs.Collection, table catalog.TableDescriptor, ) error { - _, dbDesc, err := descsCol.GetImmutableDatabaseByID( - ctx, txn, table.GetParentID(), tree.DatabaseLookupFlags{ - Required: true, - AvoidLeased: true, - IncludeOffline: true, - }) + dbDesc, err := descsCol.ByID(txn).WithoutDropped().Get().Database(ctx, table.GetParentID()) if err != nil { return err } diff --git a/pkg/sql/catalog/lease/lease_test.go b/pkg/sql/catalog/lease/lease_test.go index d168d9fc90c3..d7378a1d7ad5 100644 --- a/pkg/sql/catalog/lease/lease_test.go +++ b/pkg/sql/catalog/lease/lease_test.go @@ -86,9 +86,7 @@ func init() { ctx context.Context, t *testing.T, s serverutils.TestServerInterface, id descpb.ID, ) { require.NoError(t, sql.TestingDescsTxn(ctx, s, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) error { - t, err := col.GetMutableTableByID( - ctx, txn, id, tree.ObjectLookupFlagsWithRequired(), - ) + t, err := col.MutableByID(txn).Table(ctx, id) if err != nil { return err } @@ -2436,10 +2434,7 @@ func TestLeaseWithOfflineTables(t *testing.T) { require.NoError(t, sql.DescsTxn(ctx, &execCfg, func( ctx context.Context, txn *kv.Txn, descsCol *descs.Collection, ) error { - flags := tree.ObjectLookupFlagsWithRequiredTableKind(tree.ResolveRequireTableDesc) - flags.CommonLookupFlags.IncludeOffline = true - flags.CommonLookupFlags.IncludeDropped = true - desc, err := descsCol.GetMutableTableByID(ctx, txn, testTableID(), flags) + desc, err := descsCol.MutableByID(txn).Table(ctx, testTableID()) require.NoError(t, err) require.Equal(t, desc.State, expected) desc.State = next @@ -2809,13 +2804,16 @@ CREATE TABLE d1.t2 (name int); // Force the table descriptor into a offline state cfg := s.ExecutorConfig().(sql.ExecutorConfig) + var tableID descpb.ID require.NoError(t, sql.DescsTxn(ctx, &cfg, func( ctx context.Context, txn *kv.Txn, descriptors *descs.Collection, ) error { - _, tableDesc, err := descriptors.GetMutableTableByName(ctx, txn, tree.NewTableNameWithSchema("d1", "public", "t1"), tree.ObjectLookupFlagsWithRequired()) + tn := tree.NewTableNameWithSchema("d1", "public", "t1") + _, tableDesc, err := descs.PrefixAndMutableTable(ctx, descriptors.MutableByName(txn), tn) if err != nil { return err } + tableID = tableDesc.GetID() tableDesc.SetOffline("For unit test") err = descriptors.WriteDesc(ctx, false, tableDesc, txn) if err != nil { @@ -2835,14 +2833,7 @@ CREATE TABLE d1.t2 (name int); mu.Unlock() // Online the descriptor by making it public - _, tableDesc, err := descriptors.GetMutableTableByName(ctx, txn, - tree.NewTableNameWithSchema("d1", "public", "t1"), - tree.ObjectLookupFlags{CommonLookupFlags: tree.CommonLookupFlags{ - Required: true, - RequireMutable: true, - IncludeOffline: true, - AvoidLeased: true, - }}) + tableDesc, err := descriptors.MutableByID(txn).Table(ctx, tableID) if err != nil { return err } diff --git a/pkg/sql/catalog/randgen/templates.go b/pkg/sql/catalog/randgen/templates.go index 37fe738ef46a..c32ff73c7b33 100644 --- a/pkg/sql/catalog/randgen/templates.go +++ b/pkg/sql/catalog/randgen/templates.go @@ -20,7 +20,6 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/parser" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" - "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/types" "github.com/cockroachdb/errors" ) @@ -87,9 +86,7 @@ outer: sort.Slice(sobjIDs, func(i, j int) bool { return sobjIDs[i] < sobjIDs[j] }) // Look up the descriptors from the IDs. - descs, err := g.ext.coll.GetImmutableDescriptorsByID(ctx, g.ext.txn, - tree.CommonLookupFlags{Required: true}, - sobjIDs...) + descs, err := g.ext.coll.ByID(g.ext.txn).WithoutNonPublic().Get().Descs(ctx, sobjIDs) if err != nil { panic(genError{errors.Wrap(err, "retrieving template descriptors")}) } diff --git a/pkg/sql/catalog/randgen/util.go b/pkg/sql/catalog/randgen/util.go index 78c9160e83a2..2c1aa6de327a 100644 --- a/pkg/sql/catalog/randgen/util.go +++ b/pkg/sql/catalog/randgen/util.go @@ -16,9 +16,9 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/dbdesc" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/descs" "github.com/cockroachdb/cockroach/pkg/sql/catalog/nstree" "github.com/cockroachdb/cockroach/pkg/sql/sem/catid" - "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/errors" ) @@ -70,8 +70,14 @@ func (g *testSchemaGenerator) objNamesInSchema( func (g *testSchemaGenerator) lookupSchema( ctx context.Context, db catalog.DatabaseDescriptor, scName string, required bool, ) catalog.SchemaDescriptor { - sc, err := g.ext.coll.GetImmutableSchemaByName(ctx, - g.ext.txn, db, scName, tree.SchemaLookupFlags{Required: required}) + b := g.ext.coll.ByName(g.ext.txn) + var bng descs.ByNameGetter + if required { + bng = b.Get() + } else { + bng = b.MaybeGet() + } + sc, err := bng.Schema(ctx, db, scName) if err != nil { panic(genError{err}) } @@ -80,8 +86,7 @@ func (g *testSchemaGenerator) lookupSchema( // lookupSchema looks up the descriptor for the given database. func (g *testSchemaGenerator) lookupDatabase(ctx context.Context, dbName string) *dbdesc.Mutable { - db, err := g.ext.coll.GetMutableDatabaseByName(ctx, - g.ext.txn, dbName, tree.DatabaseLookupFlags{Required: true}) + db, err := g.ext.coll.MutableByName(g.ext.txn).Database(ctx, dbName) if err != nil { panic(genError{err}) } diff --git a/pkg/sql/catalog/resolver/resolver.go b/pkg/sql/catalog/resolver/resolver.go index fbf201e9dca3..e1aefd595488 100644 --- a/pkg/sql/catalog/resolver/resolver.go +++ b/pkg/sql/catalog/resolver/resolver.go @@ -128,7 +128,8 @@ func ResolveMutableExistingTableObject( requiredType tree.RequiredTableKind, ) (prefix catalog.ResolvedObjectPrefix, res *tabledesc.Mutable, err error) { lookupFlags := tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{Required: required, RequireMutable: true}, + Required: required, + RequireMutable: true, DesiredObjectKind: tree.TableObject, DesiredTableDescKind: requiredType, } @@ -151,7 +152,8 @@ func ResolveMutableType( ctx context.Context, sc SchemaResolver, un *tree.UnresolvedObjectName, required bool, ) (catalog.ResolvedObjectPrefix, *typedesc.Mutable, error) { lookupFlags := tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{Required: required, RequireMutable: true}, + Required: required, + RequireMutable: true, DesiredObjectKind: tree.TypeObject, } desc, prefix, err := ResolveExistingObject(ctx, sc, un, lookupFlags) @@ -198,7 +200,14 @@ func ResolveExistingObject( return nil, prefix, sqlerrors.NewUndefinedSchemaError(un.Schema()) } } - return nil, prefix, sqlerrors.NewUndefinedObjectError(un, lookupFlags.DesiredObjectKind) + switch lookupFlags.DesiredObjectKind { + case tree.TableObject: + return nil, prefix, sqlerrors.NewUndefinedRelationError(un) + case tree.TypeObject: + return nil, prefix, sqlerrors.NewUndefinedTypeError(un) + default: + return nil, prefix, errors.AssertionFailedf("unknown object kind %d", lookupFlags.DesiredObjectKind) + } } return nil, prefix, nil } @@ -493,10 +502,8 @@ func ResolveIndex( ) { if tableIndexName.Table.ObjectName != "" { lflags := tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - Required: flag.Required, - IncludeOffline: flag.IncludeOfflineTable, - }, + Required: flag.Required, + IncludeOffline: flag.IncludeOfflineTable, DesiredObjectKind: tree.TableObject, DesiredTableDescKind: tree.ResolveRequireTableOrViewDesc, } @@ -715,9 +722,7 @@ func findTableContainingIndex( } lflags := tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - IncludeOffline: true, - }, + IncludeOffline: true, DesiredObjectKind: tree.TableObject, DesiredTableDescKind: tree.ResolveAnyTableKind, } diff --git a/pkg/sql/catalog/resolver/resolver_bench_test.go b/pkg/sql/catalog/resolver/resolver_bench_test.go index 6bdbecab5cfe..4c78ecacff16 100644 --- a/pkg/sql/catalog/resolver/resolver_bench_test.go +++ b/pkg/sql/catalog/resolver/resolver_bench_test.go @@ -47,7 +47,7 @@ func BenchmarkResolveExistingObject(b *testing.B) { "CREATE TABLE foo ()", }, name: tree.MakeUnresolvedName("foo"), - flags: tree.ObjectLookupFlagsWithRequired(), + flags: tree.ObjectLookupFlags{Required: true}, }, { testName: "in schema, explicit", @@ -56,7 +56,7 @@ func BenchmarkResolveExistingObject(b *testing.B) { "CREATE TABLE sc.foo ()", }, name: tree.MakeUnresolvedName("sc", "foo"), - flags: tree.ObjectLookupFlagsWithRequired(), + flags: tree.ObjectLookupFlags{Required: true}, }, { testName: "in schema, implicit", @@ -65,7 +65,7 @@ func BenchmarkResolveExistingObject(b *testing.B) { "CREATE TABLE sc.foo ()", }, name: tree.MakeUnresolvedName("foo"), - flags: tree.ObjectLookupFlagsWithRequired(), + flags: tree.ObjectLookupFlags{Required: true}, searchPath: "public,$user,sc", }, } { @@ -136,7 +136,7 @@ func BenchmarkResolveFunction(b *testing.B) { "CREATE FUNCTION foo() RETURNS int IMMUTABLE LANGUAGE SQL AS $$ SELECT 1 $$", }, name: tree.MakeUnresolvedName("foo"), - flags: tree.ObjectLookupFlagsWithRequired(), + flags: tree.ObjectLookupFlags{Required: true}, }, { testName: "in schema, explicit", @@ -145,7 +145,7 @@ func BenchmarkResolveFunction(b *testing.B) { "CREATE FUNCTION sc.foo() RETURNS int IMMUTABLE LANGUAGE SQL AS $$ SELECT 1 $$", }, name: tree.MakeUnresolvedName("sc", "foo"), - flags: tree.ObjectLookupFlagsWithRequired(), + flags: tree.ObjectLookupFlags{Required: true}, }, { testName: "in schema, implicit", @@ -154,7 +154,7 @@ func BenchmarkResolveFunction(b *testing.B) { "CREATE FUNCTION sc.foo() RETURNS int IMMUTABLE LANGUAGE SQL AS $$ SELECT 1 $$", }, name: tree.MakeUnresolvedName("foo"), - flags: tree.ObjectLookupFlagsWithRequired(), + flags: tree.ObjectLookupFlags{Required: true}, searchPath: "public,$user,sc", }, } { diff --git a/pkg/sql/catalog/resolver/resolver_test.go b/pkg/sql/catalog/resolver/resolver_test.go index 5726abe3e996..650bbaba1c00 100644 --- a/pkg/sql/catalog/resolver/resolver_test.go +++ b/pkg/sql/catalog/resolver/resolver_test.go @@ -677,12 +677,8 @@ CREATE INDEX baz_idx ON baz (s); `) err := sql.TestingDescsTxn(ctx, s, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) error { - found, tbl, err := col.GetMutableTableByName( - ctx, txn, - tree.NewTableNameWithSchema("defaultdb", "public", "baz"), - tree.ObjectLookupFlagsWithRequiredTableKind(tree.ResolveRequireTableDesc), - ) - require.True(t, found) + tn := tree.NewTableNameWithSchema("defaultdb", "public", "baz") + _, tbl, err := descs.PrefixAndMutableTable(ctx, col.MutableByName(txn), tn) require.NoError(t, err) tbl.SetOffline("testing-index-resolving") err = col.WriteDesc(ctx, false, tbl, txn) diff --git a/pkg/sql/catalog/schemadesc/schema_desc.go b/pkg/sql/catalog/schemadesc/schema_desc.go index 4d8604bb07cc..05f6cacb0246 100644 --- a/pkg/sql/catalog/schemadesc/schema_desc.go +++ b/pkg/sql/catalog/schemadesc/schema_desc.go @@ -117,7 +117,7 @@ func formatSafeMessage(typeName string, desc catalog.SchemaDescriptor) string { // // Note: Today this isn't actually ever mutated but rather exists for a future // where we anticipate having a mutable copy of Schema descriptors. There's a -// large amount of space to question this `Mutable|ImmutableCopy` version of each +// large amount of space to question this version of each // descriptor type. Maybe it makes no sense but we're running with it for the // moment. This is an intermediate state on the road to descriptors being // handled outside of the catalog entirely as interfaces. diff --git a/pkg/sql/check.go b/pkg/sql/check.go index b83e6076d0b3..1ef3f5ea8835 100644 --- a/pkg/sql/check.go +++ b/pkg/sql/check.go @@ -388,9 +388,7 @@ func duplicateRowQuery( func (p *planner) RevalidateUniqueConstraintsInCurrentDB(ctx context.Context) error { dbName := p.CurrentDatabase() log.Infof(ctx, "validating unique constraints in database %s", dbName) - db, err := p.Descriptors().GetImmutableDatabaseByName( - ctx, p.Txn(), dbName, tree.DatabaseLookupFlags{Required: true}, - ) + db, err := p.Descriptors().ByNameWithLeased(p.Txn()).Get().Database(ctx, dbName) if err != nil { return err } @@ -414,12 +412,7 @@ func (p *planner) RevalidateUniqueConstraintsInCurrentDB(ctx context.Context) er // rows in the table have unique values for every unique constraint defined on // the table. func (p *planner) RevalidateUniqueConstraintsInTable(ctx context.Context, tableID int) error { - tableDesc, err := p.Descriptors().GetImmutableTableByID( - ctx, - p.Txn(), - descpb.ID(tableID), - tree.ObjectLookupFlagsWithRequired(), - ) + tableDesc, err := p.Descriptors().ByIDWithLeased(p.Txn()).WithoutNonPublic().Get().Table(ctx, descpb.ID(tableID)) if err != nil { return err } @@ -436,12 +429,7 @@ func (p *planner) RevalidateUniqueConstraintsInTable(ctx context.Context, tableI func (p *planner) RevalidateUniqueConstraint( ctx context.Context, tableID int, constraintName string, ) error { - tableDesc, err := p.Descriptors().GetImmutableTableByID( - ctx, - p.Txn(), - descpb.ID(tableID), - tree.ObjectLookupFlagsWithRequired(), - ) + tableDesc, err := p.Descriptors().ByIDWithLeased(p.Txn()).WithoutNonPublic().Get().Table(ctx, descpb.ID(tableID)) if err != nil { return err } @@ -495,12 +483,7 @@ func (p *planner) RevalidateUniqueConstraint( func (p *planner) IsConstraintActive( ctx context.Context, tableID int, constraintName string, ) (bool, error) { - tableDesc, err := p.Descriptors().GetImmutableTableByID( - ctx, - p.Txn(), - descpb.ID(tableID), - tree.ObjectLookupFlagsWithRequired(), - ) + tableDesc, err := p.Descriptors().ByIDWithLeased(p.Txn()).WithoutNonPublic().Get().Table(ctx, descpb.ID(tableID)) if err != nil { return false, err } @@ -678,9 +661,7 @@ func validateUniqueConstraint( func (p *planner) ValidateTTLScheduledJobsInCurrentDB(ctx context.Context) error { dbName := p.CurrentDatabase() log.Infof(ctx, "validating scheduled jobs in database %s", dbName) - db, err := p.Descriptors().GetImmutableDatabaseByName( - ctx, p.Txn(), dbName, tree.DatabaseLookupFlags{Required: true}, - ) + db, err := p.Descriptors().ByNameWithLeased(p.Txn()).Get().Database(ctx, dbName) if err != nil { return err } @@ -770,7 +751,7 @@ func (p *planner) validateTTLScheduledJobInTable( // RepairTTLScheduledJobForTable is part of the EvalPlanner interface. func (p *planner) RepairTTLScheduledJobForTable(ctx context.Context, tableID int64) error { - tableDesc, err := p.Descriptors().GetMutableTableByID(ctx, p.txn, descpb.ID(tableID), tree.ObjectLookupFlagsWithRequired()) + tableDesc, err := p.Descriptors().MutableByID(p.txn).Table(ctx, descpb.ID(tableID)) if err != nil { return err } diff --git a/pkg/sql/check_test.go b/pkg/sql/check_test.go index c264605acd78..42d601330640 100644 --- a/pkg/sql/check_test.go +++ b/pkg/sql/check_test.go @@ -56,7 +56,7 @@ func TestValidateTTLScheduledJobs(t *testing.T) { require.NoError(t, sql.TestingDescsTxn(ctx, s, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) (err error) { // We need the collection to read the descriptor from storage for // the subsequent write to succeed. - tableDesc, err = col.GetMutableTableByID(ctx, txn, tableDesc.GetID(), tree.ObjectLookupFlagsWithRequired()) + tableDesc, err = col.MutableByID(txn).Table(ctx, tableDesc.GetID()) tableDesc.RowLevelTTL.ScheduleID = 0 tableDesc.Version++ if err != nil { diff --git a/pkg/sql/comment_on_database.go b/pkg/sql/comment_on_database.go index 257eaf9a6515..aac4612b4ae3 100644 --- a/pkg/sql/comment_on_database.go +++ b/pkg/sql/comment_on_database.go @@ -40,8 +40,7 @@ func (p *planner) CommentOnDatabase( return nil, err } - dbDesc, err := p.Descriptors().GetMutableDatabaseByName(ctx, p.txn, - string(n.Name), tree.DatabaseLookupFlags{Required: true}) + dbDesc, err := p.Descriptors().MutableByName(p.txn).Database(ctx, string(n.Name)) if err != nil { return nil, err } diff --git a/pkg/sql/comment_on_schema.go b/pkg/sql/comment_on_schema.go index 085de13820a9..f0cd6dcfc443 100644 --- a/pkg/sql/comment_on_schema.go +++ b/pkg/sql/comment_on_schema.go @@ -51,14 +51,12 @@ func (p *planner) CommentOnSchema(ctx context.Context, n *tree.CommentOnSchema) "cannot comment schema without being connected to a database") } - db, err := p.Descriptors().GetImmutableDatabaseByName(ctx, p.txn, - dbName, tree.DatabaseLookupFlags{Required: true}) + db, err := p.Descriptors().ByNameWithLeased(p.txn).Get().Database(ctx, dbName) if err != nil { return nil, err } - schemaDesc, err := p.Descriptors().GetMutableSchemaByID(ctx, p.txn, - db.GetSchemaID(n.Name.Schema()), tree.SchemaLookupFlags{Required: true}) + schemaDesc, err := p.Descriptors().MutableByID(p.txn).Schema(ctx, db.GetSchemaID(n.Name.Schema())) if err != nil { return nil, err } diff --git a/pkg/sql/copy.go b/pkg/sql/copy.go index ab8fca256b4c..655c51658ef5 100644 --- a/pkg/sql/copy.go +++ b/pkg/sql/copy.go @@ -246,7 +246,11 @@ func newCopyMachine( ) } - flags := tree.ObjectLookupFlagsWithRequiredTableKind(tree.ResolveRequireTableDesc) + flags := tree.ObjectLookupFlags{ + Required: true, + DesiredObjectKind: tree.TableObject, + DesiredTableDescKind: tree.ResolveRequireTableDesc, + } _, tableDesc, err := resolver.ResolveExistingTableObject(ctx, c.p, &n.Table, flags) if err != nil { return nil, err diff --git a/pkg/sql/crdb_internal.go b/pkg/sql/crdb_internal.go index 437031f3408b..7acc6f5add92 100644 --- a/pkg/sql/crdb_internal.go +++ b/pkg/sql/crdb_internal.go @@ -394,11 +394,7 @@ CREATE TABLE crdb_internal.super_regions ( if err != nil { return err } - typeDesc, err := p.Descriptors().GetImmutableTypeByID(ctx, p.txn, typeID, - tree.ObjectLookupFlags{CommonLookupFlags: tree.CommonLookupFlags{ - Required: true, - }}, - ) + typeDesc, err := p.Descriptors().ByIDWithLeased(p.txn).WithoutNonPublic().Get().Type(ctx, typeID) if err != nil { return err } @@ -503,11 +499,8 @@ CREATE TABLE crdb_internal.tables ( // INDEX(parent_id) WHERE drop_time IS NULL populate: func(ctx context.Context, unwrappedConstraint tree.Datum, p *planner, _ catalog.DatabaseDescriptor, addRow func(...tree.Datum) error) (matched bool, err error) { dbID := descpb.ID(tree.MustBeDInt(unwrappedConstraint)) - flags := p.CommonLookupFlagsRequired() - flags.Required = false - flags.IncludeOffline = true - ok, db, err := p.Descriptors().GetImmutableDatabaseByID(ctx, p.Txn(), dbID, flags) - if !ok || err != nil { + db, err := p.byIDGetterBuilder().WithoutDropped().Get().Database(ctx, dbID) + if err != nil { return false, err } return crdbInternalTablesDatabaseLookupFunc(ctx, p, db, addRow) @@ -517,10 +510,7 @@ CREATE TABLE crdb_internal.tables ( // INDEX(database_name) WHERE drop_time IS NULL populate: func(ctx context.Context, unwrappedConstraint tree.Datum, p *planner, _ catalog.DatabaseDescriptor, addRow func(...tree.Datum) error) (matched bool, err error) { dbName := string(tree.MustBeDString(unwrappedConstraint)) - flags := p.CommonLookupFlagsRequired() - flags.Required = false - flags.IncludeOffline = true - db, err := p.Descriptors().GetImmutableDatabaseByName(ctx, p.Txn(), dbName, flags) + db, err := p.byNameGetterBuilder().WithOffline().MaybeGet().Database(ctx, dbName) if db == nil || err != nil { return false, err } @@ -2965,9 +2955,7 @@ CREATE TABLE crdb_internal.create_function_statements ( } } - fnDescs, err := p.Descriptors().GetImmutableDescriptorsByID( - ctx, p.txn, tree.CommonLookupFlags{Required: true, AvoidLeased: true}, fnIDs..., - ) + fnDescs, err := p.Descriptors().ByID(p.txn).WithoutNonPublic().Get().Descs(ctx, fnIDs) if err != nil { return err } @@ -4041,10 +4029,7 @@ CREATE TABLE crdb_internal.zones ( var table catalog.TableDescriptor if zs.Database != "" { - _, database, err := p.Descriptors().GetImmutableDatabaseByID(ctx, p.txn, descpb.ID(id), tree.DatabaseLookupFlags{ - Required: true, - AvoidLeased: true, - }) + database, err := p.Descriptors().ByID(p.txn).WithoutNonPublic().Get().Database(ctx, descpb.ID(id)) if err != nil { return err } @@ -5446,10 +5431,7 @@ CREATE TABLE crdb_internal.cluster_database_privileges ( return false, nil } - flags := tree.CommonLookupFlags{ - AvoidLeased: true, - } - dbDesc, err := p.Descriptors().GetImmutableDatabaseByName(ctx, p.Txn(), dbName, flags) + dbDesc, err := p.Descriptors().ByName(p.Txn()).MaybeGet().Database(ctx, dbName) if err != nil || dbDesc == nil { return false, err } @@ -6474,11 +6456,7 @@ CREATE TABLE crdb_internal.index_spans ( // forEachTableDescAll() below. So we can't use p.LookupByID() // which only considers online tables. p.runWithOptions(resolveFlags{skipCache: true}, func() { - cflags := p.CommonLookupFlagsRequired() - cflags.IncludeOffline = true - cflags.IncludeDropped = true - flags := tree.ObjectLookupFlags{CommonLookupFlags: cflags} - table, err = p.Descriptors().GetImmutableTableByID(ctx, p.txn, descID, flags) + table, err = p.byIDGetterBuilder().Get().Table(ctx, descID) }) if err != nil { return false, err @@ -6530,11 +6508,7 @@ CREATE TABLE crdb_internal.table_spans ( // forEachTableDescAll() below. So we can't use p.LookupByID() // which only considers online tables. p.runWithOptions(resolveFlags{skipCache: true}, func() { - cflags := p.CommonLookupFlagsRequired() - cflags.IncludeOffline = true - cflags.IncludeDropped = true - flags := tree.ObjectLookupFlags{CommonLookupFlags: cflags} - table, err = p.Descriptors().GetImmutableTableByID(ctx, p.txn, descID, flags) + table, err = p.byIDGetterBuilder().Get().Table(ctx, descID) }) if err != nil { return false, err @@ -7082,13 +7056,9 @@ func convertContentionEventsToJSON( return nil, err } - flags := tree.ObjectLookupFlags{CommonLookupFlags: tree.CommonLookupFlags{ - Required: true, - }} - desc := p.Descriptors() var tableDesc catalog.TableDescriptor - tableDesc, err = desc.GetImmutableTableByID(ctx, p.txn, descpb.ID(tableID), flags) + tableDesc, err = desc.ByIDWithLeased(p.txn).WithoutNonPublic().Get().Table(ctx, descpb.ID(tableID)) if err != nil { return nil, err } @@ -7098,12 +7068,12 @@ func convertContentionEventsToJSON( return nil, err } - ok, dbDesc, err := desc.GetImmutableDatabaseByID(ctx, p.txn, tableDesc.GetParentID(), tree.DatabaseLookupFlags{}) - if err != nil || !ok { + dbDesc, err := desc.ByIDWithLeased(p.txn).WithoutNonPublic().Get().Database(ctx, tableDesc.GetParentID()) + if err != nil { return nil, err } - schemaDesc, err := desc.GetImmutableSchemaByID(ctx, p.txn, tableDesc.GetParentSchemaID(), tree.SchemaLookupFlags{}) + schemaDesc, err := desc.ByIDWithLeased(p.txn).WithoutNonPublic().Get().Schema(ctx, tableDesc.GetParentSchemaID()) if err != nil { return nil, err } diff --git a/pkg/sql/create_function.go b/pkg/sql/create_function.go index 910a3eae769d..cfdc05693099 100644 --- a/pkg/sql/create_function.go +++ b/pkg/sql/create_function.go @@ -71,10 +71,7 @@ func (n *createFunctionNode) startExec(params runParams) error { } } - mutFlags := tree.SchemaLookupFlags{Required: true, RequireMutable: true} - mutScDesc, err := params.p.descCollection.GetMutableSchemaByName( - params.ctx, params.p.Txn(), n.dbDesc, n.scDesc.GetName(), mutFlags, - ) + mutScDesc, err := params.p.descCollection.MutableByName(params.p.Txn()).Schema(params.ctx, n.dbDesc, n.scDesc.GetName()) if err != nil { return err } @@ -197,9 +194,7 @@ func (n *createFunctionNode) replaceFunction(udfDesc *funcdesc.Mutable, params r // Removing all existing references before adding new references. for _, id := range udfDesc.DependsOn { - backRefMutable, err := params.p.Descriptors().GetMutableTableByID( - params.ctx, params.p.txn, id, tree.ObjectLookupFlagsWithRequired(), - ) + backRefMutable, err := params.p.Descriptors().MutableByID(params.p.txn).Table(params.ctx, id) if err != nil { return err } @@ -328,9 +323,7 @@ func (n *createFunctionNode) addUDFReferences(udfDesc *funcdesc.Mutable, params // Read all referenced tables and update their dependencies. backRefMutables := make(map[descpb.ID]*tabledesc.Mutable) for _, id := range backrefTblIDs.Ordered() { - backRefMutable, err := params.p.Descriptors().GetMutableTableByID( - params.ctx, params.p.txn, id, tree.ObjectLookupFlagsWithRequired(), - ) + backRefMutable, err := params.p.Descriptors().MutableByID(params.p.txn).Table(params.ctx, id) if err != nil { return err } @@ -471,9 +464,7 @@ func makeFunctionParam( } func (p *planner) descIsTable(ctx context.Context, id descpb.ID) (bool, error) { - desc, err := p.Descriptors().GetImmutableDescriptorByID( - ctx, p.Txn(), id, tree.ObjectLookupFlagsWithRequired().CommonLookupFlags, - ) + desc, err := p.Descriptors().ByIDWithLeased(p.Txn()).WithoutNonPublic().Get().Desc(ctx, id) if err != nil { return false, err } diff --git a/pkg/sql/create_function_test.go b/pkg/sql/create_function_test.go index 82e36f10b266..ecd428a95c67 100644 --- a/pkg/sql/create_function_test.go +++ b/pkg/sql/create_function_test.go @@ -65,7 +65,7 @@ CREATE SCHEMA test_sc; ) err := sql.TestingDescsTxn(ctx, s, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) error { - funcDesc, err := col.GetImmutableFunctionByID(ctx, txn, 110, tree.ObjectLookupFlagsWithRequired()) + funcDesc, err := col.ByIDWithLeased(txn).WithoutNonPublic().Get().Function(ctx, 110) require.NoError(t, err) require.Equal(t, funcDesc.GetName(), "f") @@ -94,7 +94,7 @@ SELECT nextval(105:::REGCLASS);`, // Make sure columns and indexes has correct back references. tn := tree.MakeTableNameWithSchema("defaultdb", "public", "t") - _, tbl, err := col.GetImmutableTableByName(ctx, txn, &tn, tree.ObjectLookupFlagsWithRequired()) + _, tbl, err := descs.PrefixAndTable(ctx, col.ByNameWithLeased(txn).Get(), &tn) require.NoError(t, err) require.Equal(t, "t", tbl.GetName()) require.Equal(t, @@ -108,7 +108,7 @@ SELECT nextval(105:::REGCLASS);`, // Make sure sequence has correct back references. sqn := tree.MakeTableNameWithSchema("defaultdb", "public", "sq1") - _, seq, err := col.GetImmutableTableByName(ctx, txn, &sqn, tree.ObjectLookupFlagsWithRequired()) + _, seq, err := descs.PrefixAndTable(ctx, col.ByNameWithLeased(txn).Get(), &sqn) require.NoError(t, err) require.Equal(t, "sq1", seq.GetName()) require.Equal(t, @@ -120,7 +120,7 @@ SELECT nextval(105:::REGCLASS);`, // Make sure view has correct back references. vn := tree.MakeTableNameWithSchema("defaultdb", "public", "v") - _, view, err := col.GetImmutableTableByName(ctx, txn, &vn, tree.ObjectLookupFlagsWithRequired()) + _, view, err := descs.PrefixAndTable(ctx, col.ByNameWithLeased(txn).Get(), &vn) require.NoError(t, err) require.Equal(t, "v", view.GetName()) require.Equal(t, @@ -132,7 +132,7 @@ SELECT nextval(105:::REGCLASS);`, // Make sure type has correct back references. typn := tree.MakeQualifiedTypeName("defaultdb", "public", "notmyworkday") - _, typ, err := col.GetImmutableTypeByName(ctx, txn, &typn, tree.ObjectLookupFlagsWithRequired()) + _, typ, err := descs.PrefixAndType(ctx, col.ByNameWithLeased(txn).Get(), &typn) require.NoError(t, err) require.Equal(t, "notmyworkday", typ.GetName()) require.Equal(t, @@ -194,7 +194,7 @@ func TestCreateOrReplaceFunctionUpdateReferences(t *testing.T) { ) { // Make sure columns and indexes has correct back references. tn := tree.MakeTableNameWithSchema("defaultdb", "public", tree.Name("t"+nonEmptyRelID)) - _, tbl, err := col.GetImmutableTableByName(ctx, txn, &tn, tree.ObjectLookupFlagsWithRequired()) + _, tbl, err := descs.PrefixAndTable(ctx, col.ByNameWithLeased(txn).Get(), &tn) require.NoError(t, err) require.Equal(t, []descpb.TableDescriptor_Reference{{ID: 112, IndexID: 2, ColumnIDs: []catid.ColumnID{2}}}, @@ -202,13 +202,13 @@ func TestCreateOrReplaceFunctionUpdateReferences(t *testing.T) { // Make sure sequence has correct back references. sqn := tree.MakeTableNameWithSchema("defaultdb", "public", tree.Name("sq"+nonEmptyRelID)) - _, seq, err := col.GetImmutableTableByName(ctx, txn, &sqn, tree.ObjectLookupFlagsWithRequired()) + _, seq, err := descs.PrefixAndTable(ctx, col.ByNameWithLeased(txn).Get(), &sqn) require.NoError(t, err) require.Equal(t, []descpb.TableDescriptor_Reference{{ID: 112, ByID: true}}, seq.GetDependedOnBy()) // Make sure view has empty back references. vn := tree.MakeTableNameWithSchema("defaultdb", "public", tree.Name("v"+nonEmptyRelID)) - _, view, err := col.GetImmutableTableByName(ctx, txn, &vn, tree.ObjectLookupFlagsWithRequired()) + _, view, err := descs.PrefixAndTable(ctx, col.ByNameWithLeased(txn).Get(), &vn) require.NoError(t, err) require.Equal(t, []descpb.TableDescriptor_Reference{{ID: 112, ColumnIDs: []catid.ColumnID{1}}}, @@ -216,19 +216,19 @@ func TestCreateOrReplaceFunctionUpdateReferences(t *testing.T) { // Make sure columns and indexes has empty back references. tn = tree.MakeTableNameWithSchema("defaultdb", "public", tree.Name("t"+emptyRelID)) - _, tbl, err = col.GetImmutableTableByName(ctx, txn, &tn, tree.ObjectLookupFlagsWithRequired()) + _, tbl, err = descs.PrefixAndTable(ctx, col.ByNameWithLeased(txn).Get(), &tn) require.NoError(t, err) require.Nil(t, tbl.GetDependedOnBy()) // Make sure sequence has empty back references. sqn = tree.MakeTableNameWithSchema("defaultdb", "public", tree.Name("sq"+emptyRelID)) - _, seq, err = col.GetImmutableTableByName(ctx, txn, &sqn, tree.ObjectLookupFlagsWithRequired()) + _, seq, err = descs.PrefixAndTable(ctx, col.ByNameWithLeased(txn).Get(), &sqn) require.NoError(t, err) require.Nil(t, seq.GetDependedOnBy()) // Make sure view has emtpy back references. vn = tree.MakeTableNameWithSchema("defaultdb", "public", tree.Name("v"+emptyRelID)) - _, view, err = col.GetImmutableTableByName(ctx, txn, &vn, tree.ObjectLookupFlagsWithRequired()) + _, view, err = descs.PrefixAndTable(ctx, col.ByNameWithLeased(txn).Get(), &vn) require.NoError(t, err) require.Nil(t, view.GetDependedOnBy()) } @@ -250,7 +250,7 @@ $$; ) err := sql.TestingDescsTxn(ctx, s, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) error { - funcDesc, err := col.GetImmutableFunctionByID(ctx, txn, 112, tree.ObjectLookupFlagsWithRequired()) + funcDesc, err := col.ByIDWithLeased(txn).WithoutNonPublic().Get().Function(ctx, 112) require.NoError(t, err) require.Equal(t, funcDesc.GetName(), "f") @@ -271,7 +271,7 @@ SELECT nextval(106:::REGCLASS);`, // Make sure type has correct back references. typn := tree.MakeQualifiedTypeName("defaultdb", "public", "notmyworkday") - _, typ, err := col.GetImmutableTypeByName(ctx, txn, &typn, tree.ObjectLookupFlagsWithRequired()) + _, typ, err := descs.PrefixAndType(ctx, col.ByNameWithLeased(txn).Get(), &typn) require.NoError(t, err) require.Equal(t, []descpb.ID{112}, typ.GetReferencingDescriptorIDs()) @@ -293,13 +293,7 @@ $$; `) err = sql.TestingDescsTxn(ctx, s, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) error { - flags := tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - Required: true, - AvoidLeased: true, - }, - } - funcDesc, err := col.GetImmutableFunctionByID(ctx, txn, 112, flags) + funcDesc, err := col.ByID(txn).WithoutNonPublic().Get().Function(ctx, 112) require.NoError(t, err) require.Equal(t, funcDesc.GetName(), "f") @@ -320,7 +314,7 @@ SELECT nextval(107:::REGCLASS);`, // Make sure type has correct back references. typn := tree.MakeQualifiedTypeName("defaultdb", "public", "notmyworkday") - _, typ, err := col.GetImmutableTypeByName(ctx, txn, &typn, tree.ObjectLookupFlagsWithRequired()) + _, typ, err := descs.PrefixAndType(ctx, col.ByNameWithLeased(txn).Get(), &typn) require.NoError(t, err) require.Equal(t, []descpb.ID{112}, typ.GetReferencingDescriptorIDs()) diff --git a/pkg/sql/create_schema.go b/pkg/sql/create_schema.go index fabac362123b..8ab6383830ce 100644 --- a/pkg/sql/create_schema.go +++ b/pkg/sql/create_schema.go @@ -82,12 +82,7 @@ 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, - AvoidLeased: true, - IncludeOffline: true, - IncludeDropped: true, - }) + sc, err := descriptors.ByID(txn).Get().Schema(ctx, schemaID) if err != nil || sc.SchemaKind() != catalog.SchemaUserDefined { return nil, nil, err } @@ -192,8 +187,7 @@ func (p *planner) createUserDefinedSchema(params runParams, n *tree.CreateSchema dbName = n.Schema.Catalog() } - db, err := p.Descriptors().GetMutableDatabaseByName(params.ctx, p.txn, dbName, - tree.DatabaseLookupFlags{Required: true}) + db, err := p.Descriptors().MutableByName(p.txn).Database(params.ctx, dbName) if err != nil { return err } diff --git a/pkg/sql/create_stats.go b/pkg/sql/create_stats.go index 1afde90b44d1..f5e1c3af181a 100644 --- a/pkg/sql/create_stats.go +++ b/pkg/sql/create_stats.go @@ -204,10 +204,7 @@ func (n *createStatsNode) makeJobRecord(ctx context.Context) (*jobs.Record, erro fqTableName = n.p.ResolvedName(t).FQString() case *tree.TableRef: - flags := tree.ObjectLookupFlags{CommonLookupFlags: tree.CommonLookupFlags{ - AvoidLeased: n.p.skipDescriptorCache, - }} - tableDesc, err = n.p.Descriptors().GetImmutableTableByID(ctx, n.p.txn, descpb.ID(t.TableID), flags) + tableDesc, err = n.p.byIDGetterBuilder().WithoutNonPublic().Get().Table(ctx, descpb.ID(t.TableID)) if err != nil { return nil, err } diff --git a/pkg/sql/create_table.go b/pkg/sql/create_table.go index c228cfc35218..09b87ce96bf3 100644 --- a/pkg/sql/create_table.go +++ b/pkg/sql/create_table.go @@ -83,8 +83,7 @@ func (n *createTableNode) ReadingOwnWrites() {} func (p *planner) getNonTemporarySchemaForCreate( ctx context.Context, db catalog.DatabaseDescriptor, scName string, ) (catalog.SchemaDescriptor, error) { - flags := tree.SchemaLookupFlags{Required: true, AvoidLeased: true} - sc, err := p.Descriptors().GetImmutableSchemaByName(ctx, p.txn, db, scName, flags) + sc, err := p.Descriptors().ByName(p.txn).Get().Schema(ctx, db, scName) if err != nil { return nil, err } @@ -92,7 +91,7 @@ func (p *planner) getNonTemporarySchemaForCreate( case catalog.SchemaPublic: return sc, nil case catalog.SchemaUserDefined: - return p.Descriptors().GetMutableSchemaByID(ctx, p.txn, sc.GetID(), flags) + return p.Descriptors().MutableByID(p.txn).Schema(ctx, sc.GetID()) case catalog.SchemaVirtual: return nil, pgerror.Newf(pgcode.InsufficientPrivilege, "schema cannot be modified: %q", scName) default: @@ -445,15 +444,7 @@ func (n *createTableNode) startExec(params runParams) error { } if desc.LocalityConfig != nil { - _, dbDesc, err := params.p.Descriptors().GetImmutableDatabaseByID( - params.ctx, - params.p.txn, - desc.ParentID, - tree.DatabaseLookupFlags{ - Required: true, - AvoidLeased: true, - }, - ) + dbDesc, err := params.p.Descriptors().ByID(params.p.txn).WithoutNonPublic().Get().Database(params.ctx, desc.ParentID) if err != nil { return errors.Wrap(err, "error resolving database for multi-region") } @@ -482,11 +473,7 @@ func (n *createTableNode) startExec(params runParams) error { if err != nil { return err } - typeDesc, err := params.p.Descriptors().GetMutableTypeVersionByID( - params.ctx, - params.p.txn, - regionEnumID, - ) + typeDesc, err := params.p.Descriptors().MutableByID(params.p.txn).Type(params.ctx, regionEnumID) if err != nil { return errors.Wrap(err, "error resolving multi-region enum") } diff --git a/pkg/sql/create_type.go b/pkg/sql/create_type.go index c452ba62adaf..ba5d61d6692b 100644 --- a/pkg/sql/create_type.go +++ b/pkg/sql/create_type.go @@ -80,11 +80,8 @@ func (p *planner) CreateType(ctx context.Context, n *tree.CreateType) (planNode, func (n *createTypeNode) startExec(params runParams) error { // Check if a type with the same name exists already. - flags := tree.ObjectLookupFlags{CommonLookupFlags: tree.CommonLookupFlags{ - Required: false, - AvoidLeased: true, - }} - found, _, err := params.p.Descriptors().GetImmutableTypeByName(params.ctx, params.p.Txn(), n.typeName, flags) + g := params.p.Descriptors().ByName(params.p.Txn()).MaybeGet() + _, typ, err := descs.PrefixAndType(params.ctx, g, n.typeName) if err != nil { return err } @@ -96,7 +93,7 @@ func (n *createTypeNode) startExec(params runParams) error { // handle this case in CREATE TABLE IF NOT EXISTS by checking the return code // (pgcode.DuplicateRelation) of getCreateTableParams. However, there isn't // a pgcode for duplicate types, only the more general pgcode.DuplicateObject. - if found && n.n.IfNotExists { + if typ != nil && n.n.IfNotExists { params.p.BufferClientNotice( params.ctx, pgnotice.Newf("type %q already exists, skipping", n.typeName), diff --git a/pkg/sql/create_view.go b/pkg/sql/create_view.go index cbfaaf948986..0030fe29f60e 100644 --- a/pkg/sql/create_view.go +++ b/pkg/sql/create_view.go @@ -111,22 +111,12 @@ func (n *createViewNode) startExec(params runParams) error { for id := range n.planDeps { ids.Add(id) } - flags := tree.CommonLookupFlags{ - Required: true, - RequireMutable: true, - AvoidLeased: true, - AvoidSynthetic: true, - } // Lookup the dependent tables in bulk to minimize round-trips to KV. - if _, err := params.p.Descriptors().GetImmutableDescriptorsByID( - params.ctx, params.p.Txn(), flags, ids.Ordered()..., - ); err != nil { + if _, err := params.p.Descriptors().ByID(params.p.Txn()).WithoutNonPublic().WithoutSynthetic().Get().Descs(params.ctx, ids.Ordered()); err != nil { return err } for id := range n.planDeps { - backRefMutable, err := params.p.Descriptors().GetMutableTableByID( - params.ctx, params.p.Txn(), id, tree.ObjectLookupFlagsWithRequired(), - ) + backRefMutable, err := params.p.Descriptors().MutableByID(params.p.Txn()).Table(params.ctx, id) if err != nil { return err } @@ -168,7 +158,7 @@ func (n *createViewNode) startExec(params runParams) error { if err != nil { return err } - desc, err := params.p.Descriptors().GetMutableTableVersionByID(params.ctx, id, params.p.txn) + desc, err := params.p.Descriptors().MutableByID(params.p.txn).Table(params.ctx, id) if err != nil { return err } @@ -627,7 +617,7 @@ func (p *planner) replaceViewDesc( desc, ok := backRefMutables[id] if !ok { var err error - desc, err = p.Descriptors().GetMutableTableVersionByID(ctx, id, p.txn) + desc, err = p.Descriptors().MutableByID(p.txn).Table(ctx, id) if err != nil { return nil, err } diff --git a/pkg/sql/database.go b/pkg/sql/database.go index 041028c5b70d..6bf506ee155e 100644 --- a/pkg/sql/database.go +++ b/pkg/sql/database.go @@ -120,7 +120,7 @@ func (p *planner) forEachMutableTableInDatabase( } droppedRemoved = append(droppedRemoved, tbID) } - descs, err := p.Descriptors().GetMutableDescriptorsByID(ctx, p.Txn(), droppedRemoved...) + descs, err := p.Descriptors().MutableByID(p.Txn()).Descs(ctx, droppedRemoved) if err != nil { return err } diff --git a/pkg/sql/database_region_change_finalizer.go b/pkg/sql/database_region_change_finalizer.go index 7bcba483368d..2c048fa9eb59 100644 --- a/pkg/sql/database_region_change_finalizer.go +++ b/pkg/sql/database_region_change_finalizer.go @@ -20,7 +20,6 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog/descs" "github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc" "github.com/cockroachdb/cockroach/pkg/sql/catalog/typedesc" - "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sessiondatapb" "github.com/cockroachdb/cockroach/pkg/sql/types" ) @@ -61,15 +60,7 @@ func newDatabaseRegionChangeFinalizer( var regionalByRowTables []*tabledesc.Mutable if err := func() error { - _, dbDesc, err := descsCol.GetImmutableDatabaseByID( - ctx, - txn, - dbID, - tree.DatabaseLookupFlags{ - Required: true, - AvoidLeased: true, - }, - ) + dbDesc, err := descsCol.ByID(txn).WithoutNonPublic().Get().Database(ctx, dbID) if err != nil { return err } @@ -173,15 +164,7 @@ func (r *databaseRegionChangeFinalizer) updateGlobalTablesZoneConfig( descsCol := r.localPlanner.Descriptors() - _, dbDesc, err := descsCol.GetImmutableDatabaseByID( - ctx, - txn, - r.dbID, - tree.DatabaseLookupFlags{ - Required: true, - AvoidLeased: true, - }, - ) + dbDesc, err := descsCol.ByID(txn).WithoutNonPublic().Get().Database(ctx, r.dbID) if err != nil { return err } diff --git a/pkg/sql/database_test.go b/pkg/sql/database_test.go index 55d59a43c5ee..40b7e013b2f8 100644 --- a/pkg/sql/database_test.go +++ b/pkg/sql/database_test.go @@ -20,7 +20,6 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descs" "github.com/cockroachdb/cockroach/pkg/sql/catalog/desctestutils" - "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/testutils/serverutils" "github.com/cockroachdb/cockroach/pkg/util/leaktest" "github.com/cockroachdb/cockroach/pkg/util/log" @@ -34,11 +33,7 @@ func TestDatabaseAccessors(t *testing.T) { defer s.Stopper().Stop(context.Background()) if err := TestingDescsTxn(context.Background(), s, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) error { - _, _, err := col.GetImmutableDatabaseByID(ctx, txn, keys.SystemDatabaseID, tree.CommonLookupFlags{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - }) + _, err := col.ByID(txn).Get().Database(ctx, keys.SystemDatabaseID) return err }); err != nil { t.Fatal(err) diff --git a/pkg/sql/delete_preserving_index_test.go b/pkg/sql/delete_preserving_index_test.go index 04f6935e47d7..8fbb66a7d4ce 100644 --- a/pkg/sql/delete_preserving_index_test.go +++ b/pkg/sql/delete_preserving_index_test.go @@ -677,7 +677,7 @@ func TestMergeProcessor(t *testing.T) { require.NoError(t, sql.DescsTxn(ctx, &execCfg, func( ctx context.Context, txn *kv.Txn, descriptors *descs.Collection) error { - mut, err := descriptors.GetMutableTableByID(ctx, txn, tableDesc.GetID(), tree.ObjectLookupFlags{}) + mut, err := descriptors.MutableByID(txn).Table(ctx, tableDesc.GetID()) if err != nil { return err } @@ -710,7 +710,7 @@ func TestMergeProcessor(t *testing.T) { require.NoError(t, sql.DescsTxn(ctx, &execCfg, func( ctx context.Context, txn *kv.Txn, descriptors *descs.Collection) error { - mut, err := descriptors.GetMutableTableByID(ctx, txn, tableDesc.GetID(), tree.ObjectLookupFlags{}) + mut, err := descriptors.MutableByID(txn).Table(ctx, tableDesc.GetID()) if err != nil { return err } diff --git a/pkg/sql/descmetadata/BUILD.bazel b/pkg/sql/descmetadata/BUILD.bazel index 71236b4bf601..4a4e9184d775 100644 --- a/pkg/sql/descmetadata/BUILD.bazel +++ b/pkg/sql/descmetadata/BUILD.bazel @@ -13,7 +13,6 @@ go_library( "//pkg/sql/catalog/descpb", "//pkg/sql/catalog/descs", "//pkg/sql/schemachanger/scexec", - "//pkg/sql/sem/tree", "//pkg/sql/sessiondata", "//pkg/sql/sessioninit", "//pkg/sql/sqlutil", diff --git a/pkg/sql/descmetadata/metadata_updater.go b/pkg/sql/descmetadata/metadata_updater.go index a6f127559726..c733e3c1b127 100644 --- a/pkg/sql/descmetadata/metadata_updater.go +++ b/pkg/sql/descmetadata/metadata_updater.go @@ -20,7 +20,6 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descs" "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scexec" - "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sessiondata" "github.com/cockroachdb/cockroach/pkg/sql/sessioninit" "github.com/cockroachdb/cockroach/pkg/sql/sqlutil" @@ -80,16 +79,7 @@ func (mu metadataUpdater) DeleteDatabaseRoleSettings(ctx context.Context, dbID d return nil } // Bump the table version for the role settings table when we modify it. - desc, err := mu.descriptors.GetMutableTableByID( - ctx, - mu.txn, - keys.DatabaseRoleSettingsTableID, - tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - Required: true, - RequireMutable: true, - }, - }) + desc, err := mu.descriptors.MutableByID(mu.txn).Table(ctx, keys.DatabaseRoleSettingsTableID) if err != nil { return err } diff --git a/pkg/sql/descriptor.go b/pkg/sql/descriptor.go index b825e2c1410c..8c2b3cf6dc57 100644 --- a/pkg/sql/descriptor.go +++ b/pkg/sql/descriptor.go @@ -70,12 +70,7 @@ func (p *planner) createDatabase( if dbID, err := p.Descriptors().LookupDatabaseID(ctx, p.txn, dbName); err == nil && dbID != descpb.InvalidID { if database.IfNotExists { // Check if the database is in a dropping state - flags := tree.DatabaseLookupFlags{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - } - _, desc, err := p.Descriptors().GetImmutableDatabaseByID(ctx, p.txn, dbID, flags) + desc, err := p.Descriptors().ByID(p.txn).Get().Database(ctx, dbID) if err != nil { return nil, false, err } @@ -368,12 +363,7 @@ func (p *planner) maybeInitializeMultiRegionMetadata( // GetImmutableTableInterfaceByID is part of the EvalPlanner interface. func (p *planner) GetImmutableTableInterfaceByID(ctx context.Context, id int) (interface{}, error) { - desc, err := p.Descriptors().GetImmutableTableByID( - ctx, - p.txn, - descpb.ID(id), - tree.ObjectLookupFlagsWithRequired(), - ) + desc, err := p.Descriptors().ByIDWithLeased(p.txn).WithoutNonPublic().Get().Table(ctx, descpb.ID(id)) if err != nil { return nil, err } diff --git a/pkg/sql/discard.go b/pkg/sql/discard.go index 980dc59ca56d..a51f0edd4746 100644 --- a/pkg/sql/discard.go +++ b/pkg/sql/discard.go @@ -93,10 +93,9 @@ func deleteTempTables(ctx context.Context, p *planner) error { if err != nil { return err } - flags := p.CommonLookupFlagsRequired() - flags.Required = false + g := p.byNameGetterBuilder().MaybeGet() for _, db := range allDbDescs { - sc, err := descCol.GetImmutableSchemaByName(ctx, p.Txn(), db, p.TemporarySchemaName(), flags) + sc, err := g.Schema(ctx, db, p.TemporarySchemaName()) if err != nil { return err } diff --git a/pkg/sql/distsql_plan_changefeed.go b/pkg/sql/distsql_plan_changefeed.go index 5b07d2e2ae96..d8bb51e3c21d 100644 --- a/pkg/sql/distsql_plan_changefeed.go +++ b/pkg/sql/distsql_plan_changefeed.go @@ -292,7 +292,11 @@ var _ cat.Catalog = (*cdcOptCatalog)(nil) func (c *cdcOptCatalog) ResolveDataSource( ctx context.Context, flags cat.Flags, name *cat.DataSourceName, ) (cat.DataSource, cat.DataSourceName, error) { - lflags := tree.ObjectLookupFlagsWithRequiredTableKind(tree.ResolveRequireTableDesc) + lflags := tree.ObjectLookupFlags{ + Required: true, + DesiredObjectKind: tree.TableObject, + DesiredTableDescKind: tree.ResolveRequireTableDesc, + } _, desc, err := resolver.ResolveExistingTableObject(ctx, c.planner, name, lflags) if err != nil { return nil, cat.DataSourceName{}, err diff --git a/pkg/sql/drop_cascade.go b/pkg/sql/drop_cascade.go index a23c47529265..71eca3b14a40 100644 --- a/pkg/sql/drop_cascade.go +++ b/pkg/sql/drop_cascade.go @@ -72,9 +72,7 @@ func (d *dropCascadeState) collectObjectsInSchema( // objectNamesToDelete. Instead, we need to go through each schema descriptor // to collect function descriptors by function ids. err = schema.ForEachFunctionOverload(func(overload descpb.SchemaDescriptor_FunctionOverload) error { - fnDesc, err := p.Descriptors().GetMutableFunctionByID( - ctx, p.txn, overload.ID, tree.ObjectLookupFlagsWithRequired(), - ) + fnDesc, err := p.Descriptors().MutableByID(p.txn).Function(ctx, overload.ID) if err != nil { return err } @@ -102,11 +100,9 @@ func (d *dropCascadeState) resolveCollectedObjects(ctx context.Context, p *plann tree.ObjectLookupFlags{ // Note we set required to be false here in order to not error out // if we don't find the object. - CommonLookupFlags: tree.CommonLookupFlags{ - Required: false, - RequireMutable: true, - IncludeOffline: true, - }, + Required: false, + RequireMutable: true, + IncludeOffline: true, DesiredObjectKind: tree.TableObject, }, objName.Catalog(), @@ -157,11 +153,9 @@ func (d *dropCascadeState) resolveCollectedObjects(ctx context.Context, p *plann found, _, desc, err := p.LookupObject( ctx, tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - Required: true, - RequireMutable: true, - IncludeOffline: true, - }, + Required: true, + RequireMutable: true, + IncludeOffline: true, DesiredObjectKind: tree.TypeObject, }, objName.Catalog(), diff --git a/pkg/sql/drop_database.go b/pkg/sql/drop_database.go index b587c5dc13c0..252eff0c9b08 100644 --- a/pkg/sql/drop_database.go +++ b/pkg/sql/drop_database.go @@ -61,15 +61,15 @@ func (p *planner) DropDatabase(ctx context.Context, n *tree.DropDatabase) (planN } // Check that the database exists. - dbDesc, err := p.Descriptors().GetMutableDatabaseByName(ctx, p.txn, string(n.Name), - tree.DatabaseLookupFlags{Required: !n.IfExists}) - if err != nil { + if db, err := p.Descriptors().ByName(p.txn).MaybeGet().Database(ctx, string(n.Name)); err != nil { return nil, err - } - if dbDesc == nil { - // IfExists was specified and database was not found. + } else if db == nil && n.IfExists { return newZeroNode(nil /* columns */), nil } + dbDesc, err := p.Descriptors().MutableByName(p.txn).Database(ctx, string(n.Name)) + if err != nil { + return nil, err + } if err := p.CheckPrivilege(ctx, dbDesc, privilege.DROP); err != nil { return nil, err @@ -83,12 +83,7 @@ func (p *planner) DropDatabase(ctx context.Context, n *tree.DropDatabase) (planN d := newDropCascadeState() for _, schema := range schemas { - res, err := p.Descriptors().GetImmutableSchemaByName( - ctx, p.txn, dbDesc, schema, tree.SchemaLookupFlags{ - Required: true, - AvoidLeased: true, - }, - ) + res, err := p.Descriptors().ByName(p.txn).Get().Schema(ctx, dbDesc, schema) if err != nil { return nil, err } @@ -167,9 +162,7 @@ func (n *dropDatabaseNode) startExec(params runParams) error { } case catalog.SchemaUserDefined: // For user defined schemas, we have to do a bit more work. - mutDesc, err := p.Descriptors().GetMutableSchemaByID( - ctx, p.txn, schemaToDelete.GetID(), p.CommonLookupFlagsRequired(), - ) + mutDesc, err := p.Descriptors().MutableByID(p.txn).Schema(ctx, schemaToDelete.GetID()) if err != nil { return err } @@ -296,7 +289,7 @@ func (p *planner) accumulateOwnedSequences( ) error { for colID := range desc.GetColumns() { for _, seqID := range desc.GetColumns()[colID].OwnsSequenceIds { - ownedSeqDesc, err := p.Descriptors().GetMutableTableVersionByID(ctx, seqID, p.txn) + ownedSeqDesc, err := p.Descriptors().MutableByID(p.txn).Table(ctx, seqID) if err != nil { // Special case error swallowing for #50711 and #50781, which can // cause columns to own sequences that have been dropped/do not @@ -323,7 +316,7 @@ func (p *planner) accumulateCascadingViews( ctx context.Context, dependentObjects map[descpb.ID]*tabledesc.Mutable, desc *tabledesc.Mutable, ) error { for _, ref := range desc.DependedOnBy { - desc, err := p.Descriptors().GetMutableDescriptorByID(ctx, p.txn, ref.ID) + desc, err := p.Descriptors().MutableByID(p.txn).Desc(ctx, ref.ID) if err != nil { return err } diff --git a/pkg/sql/drop_function.go b/pkg/sql/drop_function.go index 323c077a0391..943f6ec9fd83 100644 --- a/pkg/sql/drop_function.go +++ b/pkg/sql/drop_function.go @@ -134,7 +134,7 @@ func (p *planner) matchUDF( func (p *planner) checkPrivilegesForDropFunction( ctx context.Context, fnID descpb.ID, ) (*funcdesc.Mutable, error) { - mutable, err := p.Descriptors().GetMutableFunctionByID(ctx, p.Txn(), fnID, tree.ObjectLookupFlagsWithRequired()) + mutable, err := p.Descriptors().MutableByID(p.Txn()).Function(ctx, fnID) if err != nil { return nil, err } @@ -179,9 +179,7 @@ func (p *planner) dropFunctionImpl(ctx context.Context, fnMutable *funcdesc.Muta // TODO(chengxiong): remove backreference from UDFs that this UDF has // reference to. This is needed when we allow UDFs being referenced by // UDFs. - refMutable, err := p.Descriptors().GetMutableTableByID( - ctx, p.txn, id, tree.ObjectLookupFlagsWithRequired(), - ) + refMutable, err := p.Descriptors().MutableByID(p.txn).Table(ctx, id) if err != nil { return err } @@ -211,9 +209,7 @@ func (p *planner) dropFunctionImpl(ctx context.Context, fnMutable *funcdesc.Muta } // Remove function signature from schema. - scDesc, err := p.Descriptors().GetMutableSchemaByID( - ctx, p.Txn(), fnMutable.ParentSchemaID, tree.ObjectLookupFlagsWithRequired().CommonLookupFlags, - ) + scDesc, err := p.Descriptors().MutableByID(p.Txn()).Schema(ctx, fnMutable.ParentSchemaID) if err != nil { return err } diff --git a/pkg/sql/drop_function_test.go b/pkg/sql/drop_function_test.go index e6a63cd6322a..2972795fc0b6 100644 --- a/pkg/sql/drop_function_test.go +++ b/pkg/sql/drop_function_test.go @@ -62,7 +62,7 @@ CREATE SCHEMA test_sc; ) err := sql.TestingDescsTxn(ctx, s, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) error { - funcDesc, err := col.GetImmutableFunctionByID(ctx, txn, 109, tree.ObjectLookupFlagsWithRequired()) + funcDesc, err := col.ByIDWithLeased(txn).WithoutNonPublic().Get().Function(ctx, 109) require.NoError(t, err) require.Equal(t, funcDesc.GetName(), "f") @@ -91,7 +91,7 @@ SELECT nextval(105:::REGCLASS);`, // Make sure columns and indexes has correct back references. tn := tree.MakeTableNameWithSchema("defaultdb", "public", "t") - _, tbl, err := col.GetImmutableTableByName(ctx, txn, &tn, tree.ObjectLookupFlagsWithRequired()) + _, tbl, err := descs.PrefixAndTable(ctx, col.ByNameWithLeased(txn).Get(), &tn) require.NoError(t, err) require.Equal(t, "t", tbl.GetName()) require.Equal(t, @@ -106,7 +106,7 @@ SELECT nextval(105:::REGCLASS);`, // Make sure sequence has correct back references. sqn := tree.MakeTableNameWithSchema("defaultdb", "public", "sq1") - _, seq, err := col.GetImmutableTableByName(ctx, txn, &sqn, tree.ObjectLookupFlagsWithRequired()) + _, seq, err := descs.PrefixAndTable(ctx, col.ByNameWithLeased(txn).Get(), &sqn) require.NoError(t, err) require.Equal(t, "sq1", seq.GetName()) require.Equal(t, @@ -118,7 +118,7 @@ SELECT nextval(105:::REGCLASS);`, // Make sure view has correct back references. vn := tree.MakeTableNameWithSchema("defaultdb", "public", "v") - _, view, err := col.GetImmutableTableByName(ctx, txn, &vn, tree.ObjectLookupFlagsWithRequired()) + _, view, err := descs.PrefixAndTable(ctx, col.ByNameWithLeased(txn).Get(), &vn) require.NoError(t, err) require.Equal(t, "v", view.GetName()) require.Equal(t, @@ -130,7 +130,7 @@ SELECT nextval(105:::REGCLASS);`, // Make sure type has correct back references. typn := tree.MakeQualifiedTypeName("defaultdb", "public", "notmyworkday") - _, typ, err := col.GetImmutableTypeByName(ctx, txn, &typn, tree.ObjectLookupFlagsWithRequired()) + _, typ, err := descs.PrefixAndType(ctx, col.ByNameWithLeased(txn).Get(), &typn) require.NoError(t, err) require.Equal(t, "notmyworkday", typ.GetName()) require.Equal(t, @@ -145,13 +145,13 @@ SELECT nextval(105:::REGCLASS);`, // DROP the function and make sure dependencies are cleared. tDB.Exec(t, "DROP FUNCTION f") err = sql.TestingDescsTxn(ctx, s, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) error { - _, err := col.GetImmutableFunctionByID(ctx, txn, 109, tree.ObjectLookupFlagsWithRequired()) + _, err := col.ByIDWithLeased(txn).WithoutNonPublic().Get().Function(ctx, 109) require.Error(t, err) require.Regexp(t, "descriptor is being dropped", err.Error()) // Make sure columns and indexes has correct back references. tn := tree.MakeTableNameWithSchema("defaultdb", "public", "t") - _, tbl, err := col.GetImmutableTableByName(ctx, txn, &tn, tree.ObjectLookupFlagsWithRequired()) + _, tbl, err := descs.PrefixAndTable(ctx, col.ByNameWithLeased(txn).Get(), &tn) require.NoError(t, err) require.Equal(t, []descpb.TableDescriptor_Reference{ @@ -162,19 +162,19 @@ SELECT nextval(105:::REGCLASS);`, // Make sure sequence has correct back references. sqn := tree.MakeTableNameWithSchema("defaultdb", "public", "sq1") - _, seq, err := col.GetImmutableTableByName(ctx, txn, &sqn, tree.ObjectLookupFlagsWithRequired()) + _, seq, err := descs.PrefixAndTable(ctx, col.ByNameWithLeased(txn).Get(), &sqn) require.NoError(t, err) require.Nil(t, seq.GetDependedOnBy()) // Make sure view has correct back references. vn := tree.MakeTableNameWithSchema("defaultdb", "public", "v") - _, view, err := col.GetImmutableTableByName(ctx, txn, &vn, tree.ObjectLookupFlagsWithRequired()) + _, view, err := descs.PrefixAndTable(ctx, col.ByNameWithLeased(txn).Get(), &vn) require.NoError(t, err) require.Nil(t, view.GetDependedOnBy()) // Make sure type has correct back references. typn := tree.MakeQualifiedTypeName("defaultdb", "public", "notmyworkday") - _, typ, err := col.GetImmutableTypeByName(ctx, txn, &typn, tree.ObjectLookupFlagsWithRequired()) + _, typ, err := descs.PrefixAndType(ctx, col.ByNameWithLeased(txn).Get(), &typn) require.NoError(t, err) require.Nil(t, typ.GetReferencingDescriptorIDs()) @@ -374,7 +374,7 @@ $$; tDB.Exec(t, "SET use_declarative_schema_changer = off;") err := sql.TestingDescsTxn(ctx, s, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) error { - fnDesc, err := col.GetImmutableFunctionByID(ctx, txn, 113, tree.ObjectLookupFlagsWithRequired()) + fnDesc, err := col.ByIDWithLeased(txn).WithoutNonPublic().Get().Function(ctx, 113) require.NoError(t, err) require.Equal(t, "f", fnDesc.GetName()) require.True(t, fnDesc.Public()) @@ -385,7 +385,7 @@ $$; tDB.Exec(t, tc.stmt) err = sql.TestingDescsTxn(ctx, s, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) error { - _, err := col.GetImmutableFunctionByID(ctx, txn, 113, tree.ObjectLookupFlagsWithRequired()) + _, err := col.ByIDWithLeased(txn).WithoutNonPublic().Get().Function(ctx, 113) require.Error(t, err) require.Regexp(t, "descriptor is being dropped", err.Error()) return nil @@ -407,7 +407,7 @@ $$; tDB.Exec(t, "SET use_declarative_schema_changer = on;") err := sql.TestingDescsTxn(ctx, s, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) error { - fnDesc, err := col.GetImmutableFunctionByID(ctx, txn, 113, tree.ObjectLookupFlagsWithRequired()) + fnDesc, err := col.ByIDWithLeased(txn).WithoutNonPublic().Get().Function(ctx, 113) require.NoError(t, err) require.Equal(t, "f", fnDesc.GetName()) require.True(t, fnDesc.Public()) @@ -418,7 +418,7 @@ $$; tDB.Exec(t, tc.stmt) err = sql.TestingDescsTxn(ctx, s, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) error { - _, err := col.GetImmutableFunctionByID(ctx, txn, 113, tree.ObjectLookupFlagsWithRequired()) + _, err := col.ByIDWithLeased(txn).WithoutNonPublic().Get().Function(ctx, 113) require.Error(t, err) require.Regexp(t, "descriptor is being dropped", err.Error()) return nil diff --git a/pkg/sql/drop_schema.go b/pkg/sql/drop_schema.go index 6cbdf8a80d83..24e5fd0077fd 100644 --- a/pkg/sql/drop_schema.go +++ b/pkg/sql/drop_schema.go @@ -64,18 +64,11 @@ func (p *planner) DropSchema(ctx context.Context, n *tree.DropSchema) (planNode, } scName := schema.Schema() - db, err := p.Descriptors().GetMutableDatabaseByName(ctx, p.txn, dbName, - tree.DatabaseLookupFlags{Required: true}) + db, err := p.Descriptors().MutableByName(p.txn).Database(ctx, dbName) if err != nil { return nil, err } - - sc, err := p.Descriptors().GetImmutableSchemaByName( - ctx, p.txn, db, scName, tree.SchemaLookupFlags{ - Required: false, - AvoidLeased: true, - }, - ) + sc, err := p.Descriptors().ByName(p.txn).MaybeGet().Schema(ctx, db, scName) if err != nil { return nil, err } @@ -146,9 +139,7 @@ func (n *dropSchemaNode) startExec(params runParams) error { sc := n.d.schemasToDelete[i].schema schemaIDs[i] = sc.GetID() db := n.d.schemasToDelete[i].dbDesc - mutDesc, err := p.Descriptors().GetMutableSchemaByID( - ctx, p.txn, sc.GetID(), p.CommonLookupFlagsRequired(), - ) + mutDesc, err := p.Descriptors().MutableByID(p.txn).Schema(ctx, sc.GetID()) if err != nil { return err } diff --git a/pkg/sql/drop_sequence.go b/pkg/sql/drop_sequence.go index eb91006df1e5..8f2caf3efab7 100644 --- a/pkg/sql/drop_sequence.go +++ b/pkg/sql/drop_sequence.go @@ -237,7 +237,7 @@ func dropDependentOnSequence(ctx context.Context, p *planner, seqDesc *tabledesc numDependedOnByTablesToSkip := 0 for len(seqDesc.DependedOnBy) > numDependedOnByTablesToSkip { dependent := seqDesc.DependedOnBy[numDependedOnByTablesToSkip] - desc, err := p.Descriptors().GetMutableDescriptorByID(ctx, p.txn, dependent.ID) + desc, err := p.Descriptors().MutableByID(p.txn).Desc(ctx, dependent.ID) if err != nil { return err } diff --git a/pkg/sql/drop_table.go b/pkg/sql/drop_table.go index d36075294045..99cf7a3860c9 100644 --- a/pkg/sql/drop_table.go +++ b/pkg/sql/drop_table.go @@ -203,7 +203,7 @@ func (p *planner) canDropTable( func (p *planner) canRemoveFKBackreference( ctx context.Context, from string, ref catalog.ForeignKeyConstraint, behavior tree.DropBehavior, ) error { - table, err := p.Descriptors().GetMutableTableVersionByID(ctx, ref.GetOriginTableID(), p.txn) + table, err := p.Descriptors().MutableByID(p.txn).Table(ctx, ref.GetOriginTableID()) if err != nil { return err } @@ -459,7 +459,7 @@ func (p *planner) removeFKForBackReference( if tableDesc.ID == ref.GetOriginTableID() { originTableDesc = tableDesc } else { - lookup, err := p.Descriptors().GetMutableTableVersionByID(ctx, ref.GetOriginTableID(), p.txn) + lookup, err := p.Descriptors().MutableByID(p.txn).Table(ctx, ref.GetOriginTableID()) if err != nil { return errors.Wrapf(err, "error resolving origin table ID %d", ref.GetOriginTableID()) } @@ -523,7 +523,7 @@ func (p *planner) removeFKBackReference( if tableDesc.ID == ref.ReferencedTableID { referencedTableDesc = tableDesc } else { - lookup, err := p.Descriptors().GetMutableTableVersionByID(ctx, ref.ReferencedTableID, p.txn) + lookup, err := p.Descriptors().MutableByID(p.txn).Table(ctx, ref.ReferencedTableID) if err != nil { return errors.Wrapf(err, "error resolving referenced table ID %d", ref.ReferencedTableID) } diff --git a/pkg/sql/drop_test.go b/pkg/sql/drop_test.go index 003eaab36869..f43cd4ea435d 100644 --- a/pkg/sql/drop_test.go +++ b/pkg/sql/drop_test.go @@ -133,11 +133,7 @@ INSERT INTO t.kv VALUES ('c', 'e'), ('a', 'c'), ('b', 'd'); tbDesc := desctestutils.TestingGetPublicTableDescriptor(kvDB, keys.SystemSQLCodec, "t", "kv") var dbDesc catalog.DatabaseDescriptor require.NoError(t, sql.TestingDescsTxn(ctx, s, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) (err error) { - _, dbDesc, err = col.GetImmutableDatabaseByID(ctx, txn, tbDesc.GetParentID(), tree.CommonLookupFlags{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - }) + dbDesc, err = col.ByID(txn).Get().Database(ctx, tbDesc.GetParentID()) return err })) @@ -303,11 +299,7 @@ INSERT INTO t.kv2 VALUES ('c', 'd'), ('a', 'b'), ('e', 'a'); tb2Desc := desctestutils.TestingGetPublicTableDescriptor(kvDB, keys.SystemSQLCodec, "t", "kv2") var dbDesc catalog.DatabaseDescriptor require.NoError(t, sql.TestingDescsTxn(ctx, s, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) (err error) { - _, dbDesc, err = col.GetImmutableDatabaseByID(ctx, txn, tbDesc.GetParentID(), tree.CommonLookupFlags{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - }) + dbDesc, err = col.ByID(txn).Get().Database(ctx, tbDesc.GetParentID()) return err })) @@ -876,13 +868,7 @@ func TestDropTableWhileUpgradingFormat(t *testing.T) { // Simulate a migration upgrading the table descriptor's format version after // the table has been dropped but before the truncation has occurred. if err := sql.TestingDescsTxn(ctx, s, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) (err error) { - tbl, err := col.GetImmutableTableByID(ctx, txn, tableDesc.ID, tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - }, - }) + tbl, err := col.ByID(txn).Get().Table(ctx, tableDesc.ID) if err != nil { return err } @@ -1226,13 +1212,7 @@ func TestDropIndexOnHashShardedIndexWithStoredShardColumn(t *testing.T) { tdb.QueryRow(t, query).Scan(&tableID) require.NoError(t, sql.TestingDescsTxn(ctx, s, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) (err error) { - tableDesc, err = col.GetImmutableTableByID(ctx, txn, tableID, tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - }, - }) + tableDesc, err = col.ByID(txn).Get().Table(ctx, tableID) return err })) shardIdx, err := tableDesc.FindIndexWithName("idx") @@ -1250,13 +1230,7 @@ func TestDropIndexOnHashShardedIndexWithStoredShardColumn(t *testing.T) { // Assert that the index is dropped but the shard column remains after dropping the index. require.NoError(t, sql.TestingDescsTxn(ctx, s, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) (err error) { - tableDesc, err = col.GetImmutableTableByID(ctx, txn, tableID, tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - }, - }) + tableDesc, err = col.ByID(txn).Get().Table(ctx, tableID) return err })) _, err = tableDesc.FindIndexWithName("idx") diff --git a/pkg/sql/drop_type.go b/pkg/sql/drop_type.go index d1aa05fa909a..95f696b08bc2 100644 --- a/pkg/sql/drop_type.go +++ b/pkg/sql/drop_type.go @@ -99,7 +99,7 @@ func (p *planner) DropType(ctx context.Context, n *tree.DropType) (planNode, err } // Get the array type that needs to be dropped as well. - mutArrayDesc, err := p.Descriptors().GetMutableTypeVersionByID(ctx, p.txn, typeDesc.ArrayTypeID) + mutArrayDesc, err := p.Descriptors().MutableByID(p.txn).Type(ctx, typeDesc.ArrayTypeID) if err != nil { return nil, err } @@ -162,7 +162,7 @@ func (n *dropTypeNode) startExec(params runParams) error { func (p *planner) addTypeBackReference( ctx context.Context, typeID, ref descpb.ID, jobDesc string, ) error { - mutDesc, err := p.Descriptors().GetMutableTypeVersionByID(ctx, p.txn, typeID) + mutDesc, err := p.Descriptors().MutableByID(p.txn).Type(ctx, typeID) if err != nil { return err } @@ -182,7 +182,7 @@ func (p *planner) removeTypeBackReferences( ctx context.Context, typeIDs []descpb.ID, ref descpb.ID, jobDesc string, ) error { for _, typeID := range typeIDs { - mutDesc, err := p.Descriptors().GetMutableTypeVersionByID(ctx, p.txn, typeID) + mutDesc, err := p.Descriptors().MutableByID(p.txn).Type(ctx, typeID) if err != nil { return err } @@ -197,16 +197,12 @@ func (p *planner) removeTypeBackReferences( func (p *planner) addBackRefsFromAllTypesInTable( ctx context.Context, desc *tabledesc.Mutable, ) error { - _, dbDesc, err := p.Descriptors().GetImmutableDatabaseByID( - ctx, p.txn, desc.GetParentID(), tree.DatabaseLookupFlags{ - Required: true, - AvoidLeased: true, - }) + dbDesc, err := p.Descriptors().ByID(p.txn).WithoutNonPublic().Get().Database(ctx, desc.GetParentID()) if err != nil { return err } typeIDs, _, err := desc.GetAllReferencedTypeIDs(dbDesc, func(id descpb.ID) (catalog.TypeDescriptor, error) { - mutDesc, err := p.Descriptors().GetMutableTypeVersionByID(ctx, p.txn, id) + mutDesc, err := p.Descriptors().MutableByID(p.txn).Type(ctx, id) if err != nil { return nil, err } @@ -227,16 +223,12 @@ func (p *planner) addBackRefsFromAllTypesInTable( func (p *planner) removeBackRefsFromAllTypesInTable( ctx context.Context, desc *tabledesc.Mutable, ) error { - _, dbDesc, err := p.Descriptors().GetImmutableDatabaseByID( - ctx, p.txn, desc.GetParentID(), tree.DatabaseLookupFlags{ - Required: true, - AvoidLeased: true, - }) + dbDesc, err := p.Descriptors().ByID(p.txn).WithoutNonPublic().Get().Database(ctx, desc.GetParentID()) if err != nil { return err } typeIDs, _, err := desc.GetAllReferencedTypeIDs(dbDesc, func(id descpb.ID) (catalog.TypeDescriptor, error) { - mutDesc, err := p.Descriptors().GetMutableTypeVersionByID(ctx, p.txn, id) + mutDesc, err := p.Descriptors().MutableByID(p.txn).Type(ctx, id) if err != nil { return nil, err } diff --git a/pkg/sql/drop_view.go b/pkg/sql/drop_view.go index 221bed7b0c8b..dd0ea192ab4a 100644 --- a/pkg/sql/drop_view.go +++ b/pkg/sql/drop_view.go @@ -145,7 +145,7 @@ func (p *planner) canRemoveDependent( ref descpb.TableDescriptor_Reference, behavior tree.DropBehavior, ) error { - desc, err := p.Descriptors().GetMutableDescriptorByID(ctx, p.txn, ref.ID) + desc, err := p.Descriptors().MutableByID(p.txn).Desc(ctx, ref.ID) if err != nil { return err } @@ -239,7 +239,7 @@ func (p *planner) dropViewImpl( // Remove back-references from the tables/views this view depends on. dependedOn := append([]descpb.ID(nil), viewDesc.DependsOn...) for _, depID := range dependedOn { - dependencyDesc, err := p.Descriptors().GetMutableTableVersionByID(ctx, depID, p.txn) + dependencyDesc, err := p.Descriptors().MutableByID(p.txn).Table(ctx, depID) if err != nil { return cascadeDroppedViews, errors.Wrapf(err, "error resolving dependency relation ID %d", depID) @@ -323,7 +323,7 @@ func (p *planner) getDescForCascade( parentID, descID descpb.ID, behavior tree.DropBehavior, ) (catalog.MutableDescriptor, error) { - desc, err := p.Descriptors().GetMutableDescriptorByID(ctx, p.txn, descID) + desc, err := p.Descriptors().MutableByID(p.txn).Desc(ctx, descID) if err != nil { log.Warningf(ctx, "unable to retrieve descriptor for %d: %v", descID, err) return nil, errors.Wrapf(err, "error resolving dependent ID %d", descID) diff --git a/pkg/sql/evalcatalog/encode_table_index_key.go b/pkg/sql/evalcatalog/encode_table_index_key.go index 2c06b938e0bf..528d59ca9dae 100644 --- a/pkg/sql/evalcatalog/encode_table_index_key.go +++ b/pkg/sql/evalcatalog/encode_table_index_key.go @@ -33,9 +33,7 @@ func (ec *Builtins) EncodeTableIndexKey( performCast func(context.Context, tree.Datum, *types.T) (tree.Datum, error), ) ([]byte, error) { // Get the referenced table and index. - tableDesc, err := ec.dc.GetImmutableTableByID( - ctx, ec.txn, tableID, tree.ObjectLookupFlagsWithRequired(), - ) + tableDesc, err := ec.dc.ByIDWithLeased(ec.txn).WithoutNonPublic().Get().Table(ctx, tableID) if err != nil { return nil, err } diff --git a/pkg/sql/evalcatalog/geo_inverted_index_entries.go b/pkg/sql/evalcatalog/geo_inverted_index_entries.go index af78820aa1cc..f979857e05a2 100644 --- a/pkg/sql/evalcatalog/geo_inverted_index_entries.go +++ b/pkg/sql/evalcatalog/geo_inverted_index_entries.go @@ -74,9 +74,7 @@ func getIndexGeoConfig( tableID catid.DescID, indexID catid.IndexID, ) (geoindex.Config, error) { - tableDesc, err := dc.GetImmutableTableByID( - ctx, txn, tableID, tree.ObjectLookupFlagsWithRequired(), - ) + tableDesc, err := dc.ByIDWithLeased(txn).WithoutNonPublic().Get().Table(ctx, tableID) if err != nil { return geoindex.Config{}, err } diff --git a/pkg/sql/evalcatalog/pg_updatable.go b/pkg/sql/evalcatalog/pg_updatable.go index 42f4a945e70d..f7e7da8f1319 100644 --- a/pkg/sql/evalcatalog/pg_updatable.go +++ b/pkg/sql/evalcatalog/pg_updatable.go @@ -92,9 +92,7 @@ func (b *Builtins) DescriptorWithPostDeserializationChanges( func (b *Builtins) PGRelationIsUpdatable( ctx context.Context, oidArg *tree.DOid, ) (*tree.DInt, error) { - tableDesc, err := b.dc.GetImmutableTableByID( - ctx, b.txn, descpb.ID(oidArg.Oid), tree.ObjectLookupFlagsWithRequired(), - ) + tableDesc, err := b.dc.ByIDWithLeased(b.txn).WithoutNonPublic().Get().Table(ctx, descpb.ID(oidArg.Oid)) if err != nil { // For postgres compatibility, it is expected that rather returning // an error this return nonUpdatableEvents (Zero) because there could @@ -124,7 +122,7 @@ func (b *Builtins) PGColumnIsUpdatable( return tree.DBoolFalse, nil } attNum := descpb.PGAttributeNum(attNumArg) - tableDesc, err := b.dc.GetImmutableTableByID(ctx, b.txn, descpb.ID(oidArg.Oid), tree.ObjectLookupFlagsWithRequired()) + tableDesc, err := b.dc.ByIDWithLeased(b.txn).WithoutNonPublic().Get().Table(ctx, descpb.ID(oidArg.Oid)) if err != nil { if sqlerrors.IsUndefinedRelationError(err) { // For postgres compatibility, it is expected that rather returning diff --git a/pkg/sql/gcjob/BUILD.bazel b/pkg/sql/gcjob/BUILD.bazel index ecf11ad5a346..f40fd4ec8480 100644 --- a/pkg/sql/gcjob/BUILD.bazel +++ b/pkg/sql/gcjob/BUILD.bazel @@ -38,7 +38,6 @@ go_library( "//pkg/sql/oppurpose", "//pkg/sql/pgwire/pgcode", "//pkg/sql/pgwire/pgerror", - "//pkg/sql/sem/tree", "//pkg/sql/sqlerrors", "//pkg/storage", "//pkg/util/admission/admissionpb", diff --git a/pkg/sql/gcjob/gc_job.go b/pkg/sql/gcjob/gc_job.go index 26e769816c16..97676d6ef65c 100644 --- a/pkg/sql/gcjob/gc_job.go +++ b/pkg/sql/gcjob/gc_job.go @@ -28,7 +28,6 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descs" "github.com/cockroachdb/cockroach/pkg/sql/oppurpose" - "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/storage" "github.com/cockroachdb/cockroach/pkg/util/log" "github.com/cockroachdb/cockroach/pkg/util/timeutil" @@ -135,14 +134,7 @@ func deleteTableData( for _, droppedTable := range progress.Tables { var table catalog.TableDescriptor if err := sql.DescsTxn(ctx, cfg, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) (err error) { - flags := tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - }, - } - table, err = col.GetImmutableTableByID(ctx, txn, droppedTable.ID, flags) + table, err = col.ByID(txn).Get().Table(ctx, droppedTable.ID) return err }); err != nil { if errors.Is(err, catalog.ErrDescriptorNotFound) { diff --git a/pkg/sql/gcjob/index_garbage_collection.go b/pkg/sql/gcjob/index_garbage_collection.go index 460dfb8369e8..29311183bc97 100644 --- a/pkg/sql/gcjob/index_garbage_collection.go +++ b/pkg/sql/gcjob/index_garbage_collection.go @@ -22,7 +22,6 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descs" - "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sqlerrors" "github.com/cockroachdb/cockroach/pkg/util/log" "github.com/cockroachdb/errors" @@ -122,15 +121,7 @@ func gcIndexes( removeIndexZoneConfigs := func( ctx context.Context, txn *kv.Txn, descriptors *descs.Collection, ) error { - freshParentTableDesc, err := descriptors.GetMutableTableByID( - ctx, txn, parentID, tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - AvoidLeased: true, - Required: true, - IncludeDropped: true, - IncludeOffline: true, - }, - }) + freshParentTableDesc, err := descriptors.MutableByID(txn).Table(ctx, parentID) if err != nil { return err } @@ -211,15 +202,7 @@ func deleteIndexZoneConfigsAfterGC( removeIndexZoneConfigs := func( ctx context.Context, txn *kv.Txn, descriptors *descs.Collection, ) error { - freshParentTableDesc, err := descriptors.GetMutableTableByID( - ctx, txn, parentID, tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - AvoidLeased: true, - Required: true, - IncludeDropped: true, - IncludeOffline: true, - }, - }) + freshParentTableDesc, err := descriptors.MutableByID(txn).Table(ctx, parentID) if err != nil { return err } diff --git a/pkg/sql/gcjob/refresh_statuses.go b/pkg/sql/gcjob/refresh_statuses.go index 511b4cbb1a77..6886d9b48c3e 100644 --- a/pkg/sql/gcjob/refresh_statuses.go +++ b/pkg/sql/gcjob/refresh_statuses.go @@ -28,7 +28,6 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descs" - "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/util/hlc" "github.com/cockroachdb/cockroach/pkg/util/log" "github.com/cockroachdb/cockroach/pkg/util/timeutil" @@ -105,14 +104,7 @@ func updateStatusForGCElements( earliestDeadline := timeutil.Unix(0, int64(math.MaxInt64)) if err := sql.DescsTxn(ctx, execCfg, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) error { - flags := tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - }, - } - table, err := col.GetImmutableTableByID(ctx, txn, tableID, flags) + table, err := col.ByID(txn).Get().Table(ctx, tableID) if err != nil { return err } diff --git a/pkg/sql/gcjob/table_garbage_collection.go b/pkg/sql/gcjob/table_garbage_collection.go index 35222016bef8..c57aafcb1edf 100644 --- a/pkg/sql/gcjob/table_garbage_collection.go +++ b/pkg/sql/gcjob/table_garbage_collection.go @@ -25,7 +25,6 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descs" - "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/util/admission/admissionpb" "github.com/cockroachdb/cockroach/pkg/util/log" "github.com/cockroachdb/cockroach/pkg/util/timeutil" @@ -50,14 +49,7 @@ func gcTables( var table catalog.TableDescriptor if err := sql.DescsTxn(ctx, execCfg, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) (err error) { - flags := tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - }, - } - table, err = col.GetImmutableTableByID(ctx, txn, droppedTable.ID, flags) + table, err = col.ByID(txn).Get().Table(ctx, droppedTable.ID) return err }); err != nil { if errors.Is(err, catalog.ErrDescriptorNotFound) { @@ -295,14 +287,7 @@ func deleteTableDescriptorsAfterGC( var table catalog.TableDescriptor if err := sql.DescsTxn(ctx, execCfg, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) (err error) { - flags := tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - }, - } - table, err = col.GetImmutableTableByID(ctx, txn, droppedTable.ID, flags) + table, err = col.ByID(txn).Get().Table(ctx, droppedTable.ID) return err }); err != nil { if errors.Is(err, catalog.ErrDescriptorNotFound) { diff --git a/pkg/sql/gcjob_test/BUILD.bazel b/pkg/sql/gcjob_test/BUILD.bazel index eeb389fe8e95..dcc74794157d 100644 --- a/pkg/sql/gcjob_test/BUILD.bazel +++ b/pkg/sql/gcjob_test/BUILD.bazel @@ -34,7 +34,6 @@ go_test( "//pkg/sql/gcjob", "//pkg/sql/gcjob/gcjobnotifier", "//pkg/sql/sem/catid", - "//pkg/sql/sem/tree", "//pkg/sql/sqlutil", "//pkg/storage", "//pkg/testutils", diff --git a/pkg/sql/gcjob_test/gc_job_test.go b/pkg/sql/gcjob_test/gc_job_test.go index e52f14db5346..cf49bcf854a0 100644 --- a/pkg/sql/gcjob_test/gc_job_test.go +++ b/pkg/sql/gcjob_test/gc_job_test.go @@ -40,7 +40,6 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/gcjob" "github.com/cockroachdb/cockroach/pkg/sql/gcjob/gcjobnotifier" "github.com/cockroachdb/cockroach/pkg/sql/sem/catid" - "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sqlutil" "github.com/cockroachdb/cockroach/pkg/storage" "github.com/cockroachdb/cockroach/pkg/testutils" @@ -110,24 +109,12 @@ func TestSchemaChangeGCJob(t *testing.T) { var myTableDesc *tabledesc.Mutable var myOtherTableDesc *tabledesc.Mutable if err := sql.TestingDescsTxn(ctx, s, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) error { - myImm, err := col.GetImmutableTableByID(ctx, txn, myTableID, tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - }, - }) + myImm, err := col.ByID(txn).Get().Table(ctx, myTableID) if err != nil { return err } myTableDesc = tabledesc.NewBuilder(myImm.TableDesc()).BuildExistingMutableTable() - myOtherImm, err := col.GetImmutableTableByID(ctx, txn, myOtherTableID, tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - }, - }) + myOtherImm, err := col.ByID(txn).Get().Table(ctx, myOtherTableID) if err != nil { return err } @@ -251,13 +238,7 @@ func TestSchemaChangeGCJob(t *testing.T) { } if err := sql.TestingDescsTxn(ctx, s, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) error { - myImm, err := col.GetImmutableTableByID(ctx, txn, myTableID, tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - }, - }) + myImm, err := col.ByID(txn).Get().Table(ctx, myTableID) if err != nil { return err } @@ -267,13 +248,7 @@ func TestSchemaChangeGCJob(t *testing.T) { return nil } myTableDesc = tabledesc.NewBuilder(myImm.TableDesc()).BuildExistingMutableTable() - myOtherImm, err := col.GetImmutableTableByID(ctx, txn, myOtherTableID, tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - }, - }) + myOtherImm, err := col.ByID(txn).Get().Table(ctx, myOtherTableID) if err != nil { return err } diff --git a/pkg/sql/grant_revoke.go b/pkg/sql/grant_revoke.go index 5bc40933e18c..e4f6a554bde0 100644 --- a/pkg/sql/grant_revoke.go +++ b/pkg/sql/grant_revoke.go @@ -587,7 +587,7 @@ func (p *planner) getTablePatternsComposition( } // Note that part of the reason the code is structured this way is that // resolving mutable descriptors for virtual table IDs results in an error. - muts, err := p.Descriptors().GetMutableDescriptorsByID(ctx, p.txn, nonVirtualIDs...) + muts, err := p.Descriptors().MutableByID(p.txn).Descs(ctx, nonVirtualIDs) if err != nil { return unknownComposition, err } diff --git a/pkg/sql/importer/import_job.go b/pkg/sql/importer/import_job.go index 268fa3fde651..4c686c5d7759 100644 --- a/pkg/sql/importer/import_job.go +++ b/pkg/sql/importer/import_job.go @@ -134,7 +134,7 @@ func (r *importResumer) Resume(ctx context.Context, execCtx interface{}) error { } // The public schema is expected to always be present in the database for 22.2+. - _, dbDesc, err := descsCol.GetImmutableDatabaseByID(ctx, txn, details.ParentID, tree.DatabaseLookupFlags{Required: true}) + dbDesc, err := descsCol.ByIDWithLeased(txn).WithoutNonPublic().Get().Database(ctx, details.ParentID) if err != nil { return err } @@ -149,8 +149,7 @@ func (r *importResumer) Resume(ctx context.Context, execCtx interface{}) error { // Telemetry for multi-region. for _, table := range preparedDetails.Tables { - _, dbDesc, err := descsCol.GetImmutableDatabaseByID( - ctx, txn, table.Desc.GetParentID(), tree.DatabaseLookupFlags{Required: true}) + dbDesc, err := descsCol.ByIDWithLeased(txn).WithoutNonPublic().Get().Database(ctx, table.Desc.GetParentID()) if err != nil { return err } @@ -473,7 +472,7 @@ func prepareExistingTablesForIngestion( } // Note that desc is just used to verify that the version matches. - importing, err := descsCol.GetMutableTableVersionByID(ctx, desc.ID, txn) + importing, err := descsCol.MutableByID(txn).Table(ctx, desc.ID) if err != nil { return nil, err } @@ -625,7 +624,7 @@ func (r *importResumer) prepareSchemasForIngestion( schemaMetadata.schemaPreparedDetails.Schemas = make([]jobspb.ImportDetails_Schema, len(details.Schemas)) - desc, err := descsCol.GetMutableDescriptorByID(ctx, txn, details.ParentID) + desc, err := descsCol.MutableByID(txn).Desc(ctx, details.ParentID) if err != nil { return nil, err } @@ -701,7 +700,7 @@ func bindImportStartTime( if err := sql.DescsTxn(ctx, p.ExecCfg(), func( ctx context.Context, txn *kv.Txn, descsCol *descs.Collection, ) error { - mutableDesc, err := descsCol.GetMutableTableVersionByID(ctx, id, txn) + mutableDesc, err := descsCol.MutableByID(txn).Table(ctx, id) if err != nil { return err } @@ -753,10 +752,7 @@ func (r *importResumer) parseBundleSchemaIfNeeded(ctx context.Context, phs inter if err := sql.DescsTxn(ctx, p.ExecCfg(), func( ctx context.Context, txn *kv.Txn, descriptors *descs.Collection, ) (err error) { - _, dbDesc, err = descriptors.GetImmutableDatabaseByID(ctx, txn, parentID, tree.DatabaseLookupFlags{ - Required: true, - AvoidLeased: true, - }) + dbDesc, err = descriptors.ByID(txn).WithoutNonPublic().Get().Database(ctx, parentID) if err != nil { return err } @@ -820,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.ByIDWithLeased(txn).WithoutNonPublic().Get().Schema(ctx, publicSchemaID) return err }); err != nil { return nil, err @@ -946,7 +942,7 @@ func (r *importResumer) publishTables( ) error { b := txn.NewBatch() for _, tbl := range details.Tables { - newTableDesc, err := descsCol.GetMutableTableVersionByID(ctx, tbl.Desc.ID, txn) + newTableDesc, err := descsCol.MutableByID(txn).Table(ctx, tbl.Desc.ID) if err != nil { return err } @@ -1067,7 +1063,7 @@ func (r *importResumer) publishSchemas(ctx context.Context, execCfg *sql.Executo ) error { b := txn.NewBatch() for _, schema := range details.Schemas { - newDesc, err := descsCol.GetMutableDescriptorByID(ctx, txn, schema.Desc.GetID()) + newDesc, err := descsCol.MutableByID(txn).Desc(ctx, schema.Desc.GetID()) if err != nil { return err } @@ -1179,13 +1175,7 @@ func (r *importResumer) checkForUDTModification( ctx context.Context, txn *kv.Txn, col *descs.Collection, savedTypeDesc *descpb.TypeDescriptor, ) error { - typeDesc, err := col.GetImmutableTypeByID(ctx, txn, savedTypeDesc.GetID(), tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - }, - }) + typeDesc, err := col.ByID(txn).Get().Type(ctx, savedTypeDesc.GetID()) if err != nil { return errors.Wrap(err, "resolving type descriptor when checking version mismatch") } @@ -1459,7 +1449,7 @@ func (r *importResumer) dropTables( var intoTable catalog.TableDescriptor for _, tbl := range details.Tables { if !tbl.IsNew { - desc, err := descsCol.GetMutableTableVersionByID(ctx, tbl.Desc.ID, txn) + desc, err := descsCol.MutableByID(txn).Table(ctx, tbl.Desc.ID) if err != nil { return err } @@ -1536,7 +1526,7 @@ func (r *importResumer) dropTables( // Bring the IMPORT INTO table back online b := txn.NewBatch() - intoDesc, err := descsCol.GetMutableTableVersionByID(ctx, intoTable.GetID(), txn) + intoDesc, err := descsCol.MutableByID(txn).Table(ctx, intoTable.GetID()) if err != nil { return err } @@ -1561,7 +1551,7 @@ func (r *importResumer) dropNewTables( tablesToGC := make([]descpb.ID, 0, len(details.Tables)) toWrite := make([]*tabledesc.Mutable, 0, len(details.Tables)) for _, tbl := range details.Tables { - newTableDesc, err := descsCol.GetMutableTableVersionByID(ctx, tbl.Desc.ID, txn) + newTableDesc, err := descsCol.MutableByID(txn).Table(ctx, tbl.Desc.ID) if err != nil { return err } @@ -1630,7 +1620,7 @@ func (r *importResumer) dropSchemas( } // Resolve the database descriptor. - desc, err := descsCol.GetMutableDescriptorByID(ctx, txn, details.ParentID) + desc, err := descsCol.MutableByID(txn).Desc(ctx, details.ParentID) if err != nil { return nil, err } @@ -1643,7 +1633,7 @@ func (r *importResumer) dropSchemas( droppedSchemaIDs := make([]descpb.ID, 0) for _, schema := range details.Schemas { - desc, err := descsCol.GetMutableDescriptorByID(ctx, txn, schema.Desc.ID) + desc, err := descsCol.MutableByID(txn).Desc(ctx, schema.Desc.ID) if err != nil { return nil, err } diff --git a/pkg/sql/importer/import_planning.go b/pkg/sql/importer/import_planning.go index dada66c8f5b3..b737acf4bf02 100644 --- a/pkg/sql/importer/import_planning.go +++ b/pkg/sql/importer/import_planning.go @@ -297,22 +297,13 @@ func resolveUDTsUsedByImportInto( err := sql.DescsTxn(ctx, p.ExecCfg(), func( ctx context.Context, txn *kv.Txn, descriptors *descs.Collection, ) (err error) { - _, dbDesc, err = descriptors.GetImmutableDatabaseByID(ctx, txn, table.GetParentID(), - tree.DatabaseLookupFlags{ - Required: true, - AvoidLeased: true, - }) + dbDesc, err = descriptors.ByID(txn).WithoutNonPublic().Get().Database(ctx, table.GetParentID()) if err != nil { return err } typeIDs, _, err := table.GetAllReferencedTypeIDs(dbDesc, func(id descpb.ID) (catalog.TypeDescriptor, error) { - immutDesc, err := descriptors.GetImmutableTypeByID(ctx, txn, id, tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - Required: true, - AvoidLeased: true, - }, - }) + immutDesc, err := descriptors.ByID(txn).WithoutNonPublic().Get().Type(ctx, id) if err != nil { return nil, err } @@ -323,12 +314,7 @@ func resolveUDTsUsedByImportInto( } for _, typeID := range typeIDs { - immutDesc, err := descriptors.GetImmutableTypeByID(ctx, txn, typeID, tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - Required: true, - AvoidLeased: true, - }, - }) + immutDesc, err := descriptors.ByID(txn).WithoutNonPublic().Get().Type(ctx, typeID) if err != nil { return err } diff --git a/pkg/sql/importer/import_stmt_test.go b/pkg/sql/importer/import_stmt_test.go index e9869a2308eb..225695cb92bc 100644 --- a/pkg/sql/importer/import_stmt_test.go +++ b/pkg/sql/importer/import_stmt_test.go @@ -2061,13 +2061,7 @@ func TestFailedImportGC(t *testing.T) { tableID := descpb.ID(dbID + 2) var td catalog.TableDescriptor if err := sql.TestingDescsTxn(ctx, tc.Server(0), func(ctx context.Context, txn *kv.Txn, col *descs.Collection) (err error) { - td, err = col.GetImmutableTableByID(ctx, txn, tableID, tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - }, - }) + td, err = col.ByID(txn).Get().Table(ctx, tableID) return err }); err != nil { t.Fatal(err) @@ -6418,11 +6412,7 @@ 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{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - }) + _, err := col.ByID(txn).Get().Schema(ctx, schemaID) if pgerror.GetPGCode(err) == pgcode.InvalidSchemaName { return nil } @@ -6438,13 +6428,7 @@ func TestImportPgDumpSchemas(t *testing.T) { for _, tableID := range tableIDs { // Expect that the table descriptor is deleted. if err := sql.TestingDescsTxn(ctx, tc.Server(0), func(ctx context.Context, txn *kv.Txn, col *descs.Collection) error { - _, err := col.GetImmutableTableByID(ctx, txn, tableID, tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - }, - }) + _, err := col.ByID(txn).Get().Table(ctx, tableID) if !testutils.IsError(err, "descriptor not found") { return err } diff --git a/pkg/sql/importer/read_import_pgdump.go b/pkg/sql/importer/read_import_pgdump.go index a8828328c3b8..a18bdb027cca 100644 --- a/pkg/sql/importer/read_import_pgdump.go +++ b/pkg/sql/importer/read_import_pgdump.go @@ -278,10 +278,7 @@ func createPostgresSchemas( ctx context.Context, txn *kv.Txn, descriptors *descs.Collection, ) error { schemaDescs = nil // reset for retries - _, dbDesc, err := descriptors.GetImmutableDatabaseByID(ctx, txn, parentID, tree.DatabaseLookupFlags{ - Required: true, - AvoidLeased: true, - }) + dbDesc, err := descriptors.ByID(txn).WithoutNonPublic().Get().Database(ctx, parentID) if err != nil { return err } @@ -877,11 +874,7 @@ func readPostgresStmt( for _, name := range names { tableName := name.ToUnresolvedObjectName().String() if err := sql.DescsTxn(ctx, p.ExecCfg(), func(ctx context.Context, txn *kv.Txn, col *descs.Collection) error { - _, dbDesc, err := col.GetImmutableDatabaseByID(ctx, txn, parentID, tree.CommonLookupFlags{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - }) + dbDesc, err := col.ByID(txn).Get().Database(ctx, parentID) if err != nil { return err } diff --git a/pkg/sql/indexbackfiller_test.go b/pkg/sql/indexbackfiller_test.go index 0e63bf6c094f..8933a05d8e76 100644 --- a/pkg/sql/indexbackfiller_test.go +++ b/pkg/sql/indexbackfiller_test.go @@ -487,7 +487,7 @@ INSERT INTO foo VALUES (1), (10), (100); require.NoError(t, sql.DescsTxn(ctx, &execCfg, func( ctx context.Context, txn *kv.Txn, descriptors *descs.Collection, ) (err error) { - mut, err := descriptors.GetMutableTableByID(ctx, txn, tableID, tree.ObjectLookupFlags{}) + mut, err := descriptors.MutableByID(txn).Table(ctx, tableID) if err != nil { return err } @@ -539,7 +539,7 @@ INSERT INTO foo VALUES (1), (10), (100); require.NoError(t, sql.DescsTxn(ctx, &execCfg, func( ctx context.Context, txn *kv.Txn, descriptors *descs.Collection, ) error { - table, err := descriptors.GetMutableTableByID(ctx, txn, tableID, tree.ObjectLookupFlags{}) + table, err := descriptors.MutableByID(txn).Table(ctx, tableID) if err != nil { return err } diff --git a/pkg/sql/information_schema.go b/pkg/sql/information_schema.go index 5c4f7fa75caa..37ad14947143 100644 --- a/pkg/sql/information_schema.go +++ b/pkg/sql/information_schema.go @@ -1673,12 +1673,12 @@ 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().ByIDWithLeased(p.txn).WithoutNonPublic().Get().Schema(ctx, id) if err != nil { return err } return sc.ForEachFunctionOverload(func(overload descpb.SchemaDescriptor_FunctionOverload) error { - fn, err := p.Descriptors().GetMutableFunctionByID(ctx, p.txn, overload.ID, tree.ObjectLookupFlagsWithRequired()) + fn, err := p.Descriptors().MutableByID(p.txn).Function(ctx, overload.ID) if err != nil { return err } diff --git a/pkg/sql/logictest/testdata/logic_test/privileges_comments b/pkg/sql/logictest/testdata/logic_test/privileges_comments index 14b45a1591c4..0dc8b67504f5 100644 --- a/pkg/sql/logictest/testdata/logic_test/privileges_comments +++ b/pkg/sql/logictest/testdata/logic_test/privileges_comments @@ -81,8 +81,10 @@ p45707 user root statement ok -GRANT ALL ON DATABASE d45707 TO testuser; - GRANT ALL ON TABLE d45707.t45707 TO testuser +GRANT ALL ON DATABASE d45707 TO testuser + +statement ok +GRANT ALL ON TABLE d45707.t45707 TO testuser user testuser diff --git a/pkg/sql/opt_catalog.go b/pkg/sql/opt_catalog.go index 9f161b14aa99..3079a2c01817 100644 --- a/pkg/sql/opt_catalog.go +++ b/pkg/sql/opt_catalog.go @@ -232,7 +232,10 @@ func (oc *optCatalog) ResolveDataSource( } oc.tn = *name - lflags := tree.ObjectLookupFlagsWithRequiredTableKind(tree.ResolveAnyTableKind) + lflags := tree.ObjectLookupFlags{ + Required: true, + DesiredObjectKind: tree.TableObject, + } prefix, desc, err := resolver.ResolveExistingTableObject(ctx, oc.planner, &oc.tn, lflags) if err != nil { return nil, cat.DataSourceName{}, err @@ -438,9 +441,7 @@ func (oc *optCatalog) fullyQualifiedNameWithTxn( } dbID := desc.GetParentID() - _, dbDesc, err := oc.planner.Descriptors().GetImmutableDatabaseByID( - ctx, txn, dbID, tree.DatabaseLookupFlags{AvoidLeased: true}, - ) + dbDesc, err := oc.planner.Descriptors().ByID(txn).WithoutNonPublic().Get().Database(ctx, dbID) if err != nil { return cat.DataSourceName{}, err } @@ -450,9 +451,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).WithoutNonPublic().Get().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 aaf7339b1025..24654ea3ced4 100644 --- a/pkg/sql/pg_catalog.go +++ b/pkg/sql/pg_catalog.go @@ -1064,9 +1064,7 @@ type oneAtATimeSchemaResolver struct { func (r oneAtATimeSchemaResolver) getDatabaseByID( id descpb.ID, ) (catalog.DatabaseDescriptor, error) { - _, desc, err := r.p.Descriptors().GetImmutableDatabaseByID( - r.ctx, r.p.txn, id, tree.DatabaseLookupFlags{Required: true}, - ) + desc, err := r.p.Descriptors().ByIDWithLeased(r.p.txn).WithoutNonPublic().Get().Database(r.ctx, id) return desc, err } @@ -1079,11 +1077,7 @@ 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{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - }) + return r.p.Descriptors().ByID(r.p.txn).Get().Schema(r.ctx, id) } // makeAllRelationsVirtualTableWithDescriptorIDIndex creates a virtual table that searches through @@ -1151,10 +1145,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().ByIDWithLeased(p.txn).WithoutNonPublic().Get().Schema(ctx, table.GetParentSchemaID()) if err != nil { return false, err } @@ -1606,21 +1597,11 @@ https://www.postgresql.org/docs/9.5/catalog-pg-description.html`, objID = tree.NewDOid(oid.Oid(tree.MustBeDInt(objID))) classOid = tree.NewDOid(catconstants.PgCatalogClassTableID) case catalogkeys.ConstraintCommentType: - tableDesc, err := p.Descriptors().GetImmutableTableByID( - ctx, - p.txn, - descpb.ID(tree.MustBeDInt(objID)), - tree.ObjectLookupFlagsWithRequiredTableKind(tree.ResolveRequireTableDesc)) + tableDesc, err := p.Descriptors().ByIDWithLeased(p.txn).WithoutNonPublic().Get().Table(ctx, descpb.ID(tree.MustBeDInt(objID))) if err != nil { return err } - schema, err := p.Descriptors().GetImmutableSchemaByID( - ctx, - p.txn, - tableDesc.GetParentSchemaID(), - tree.CommonLookupFlags{ - Required: true, - }) + schema, err := p.Descriptors().ByIDWithLeased(p.txn).WithoutNonPublic().Get().Schema(ctx, tableDesc.GetParentSchemaID()) if err != nil { return err } @@ -2113,9 +2094,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().ByIDWithLeased(p.Txn()).WithoutNonPublic().Get().Schema(ctx, descpb.ID(ooid)); err == nil { return sc, true, nil } else if !sqlerrors.IsUndefinedSchemaError(err) { return nil, false, err @@ -2570,12 +2549,7 @@ https://www.postgresql.org/docs/9.5/catalog-pg-proc.html`, func(dbDesc catalog.DatabaseDescriptor) error { return forEachSchema(ctx, p, dbDesc, true /* requiresPrivileges */, func(scDesc catalog.SchemaDescriptor) error { return scDesc.ForEachFunctionOverload(func(overload descpb.SchemaDescriptor_FunctionOverload) error { - fnDesc, err := p.Descriptors().GetImmutableFunctionByID( - ctx, p.Txn(), overload.ID, - tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{AvoidLeased: true}, - }, - ) + fnDesc, err := p.Descriptors().ByID(p.Txn()).WithoutNonPublic().Get().Function(ctx, overload.ID) if err != nil { return err } @@ -2603,19 +2577,12 @@ https://www.postgresql.org/docs/9.5/catalog-pg-proc.html`, } if funcdesc.IsOIDUserDefinedFunc(ooid) { - fnDesc, err := p.Descriptors().GetImmutableFunctionByID( - ctx, p.Txn(), descpb.ID(overload.Oid), - tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{AvoidLeased: true}, - }, - ) + fnDesc, err := p.Descriptors().ByID(p.Txn()).WithoutNonPublic().Get().Function(ctx, descpb.ID(overload.Oid)) if err != nil { return false, err } - scDesc, err := p.Descriptors().GetImmutableSchemaByID( - ctx, p.Txn(), descpb.ID(ooid), tree.SchemaLookupFlags{Required: true}, - ) + scDesc, err := p.Descriptors().ByIDWithLeased(p.Txn()).WithoutNonPublic().Get().Schema(ctx, descpb.ID(ooid)) if err != nil { return false, err } @@ -3215,7 +3182,7 @@ func addPGTypeRow( func getSchemaAndTypeByTypeID( ctx context.Context, p *planner, id descpb.ID, ) (catalog.SchemaDescriptor, catalog.TypeDescriptor, error) { - typDesc, err := p.Descriptors().GetImmutableTypeByID(ctx, p.txn, id, tree.ObjectLookupFlags{}) + typDesc, err := p.Descriptors().ByIDWithLeased(p.txn).WithoutNonPublic().Get().Type(ctx, id) if err != nil { // If the type was not found, it may be a table. if !(errors.Is(err, catalog.ErrDescriptorNotFound) || pgerror.GetPGCode(err) == pgcode.UndefinedObject) { @@ -3224,12 +3191,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().ByIDWithLeased(p.txn).WithoutNonPublic().Get().Schema(ctx, typDesc.GetParentSchemaID()) if err != nil { return nil, nil, err } @@ -3332,7 +3294,7 @@ https://www.postgresql.org/docs/9.5/catalog-pg-type.html`, // It's an entry for the implicit record type created on behalf of each // table. We have special logic for this case. if typDesc.GetKind() == descpb.TypeDescriptor_TABLE_IMPLICIT_RECORD_TYPE { - table, err := p.Descriptors().GetImmutableTableByID(ctx, p.txn, id, tree.ObjectLookupFlags{}) + table, err := p.Descriptors().ByIDWithLeased(p.txn).WithoutNonPublic().Get().Table(ctx, id) if err != nil { if errors.Is(err, catalog.ErrDescriptorNotFound) || pgerror.GetPGCode(err) == pgcode.UndefinedObject || @@ -3577,9 +3539,7 @@ https://www.postgresql.org/docs/13/catalog-pg-statistic-ext.html`, h.writeUInt64(uint64(statisticsID)) statisticsOID := h.getOid() - tbl, err := p.Descriptors().GetImmutableTableByID( - ctx, p.Txn(), descpb.ID(tableID), tree.ObjectLookupFlagsWithRequired(), - ) + tbl, err := p.Descriptors().ByIDWithLeased(p.Txn()).WithoutNonPublic().Get().Table(ctx, descpb.ID(tableID)) if err != nil { return err } diff --git a/pkg/sql/planner.go b/pkg/sql/planner.go index 604d087b33f9..c973ccf19cc9 100644 --- a/pkg/sql/planner.go +++ b/pkg/sql/planner.go @@ -591,7 +591,10 @@ func (p *planner) GetTypeFromValidSQLSyntax(sql string) (*types.T, error) { // ResolveTableName implements the eval.DatabaseCatalog interface. func (p *planner) ResolveTableName(ctx context.Context, tn *tree.TableName) (tree.ID, error) { - flags := tree.ObjectLookupFlagsWithRequiredTableKind(tree.ResolveAnyTableKind) + flags := tree.ObjectLookupFlags{ + Required: true, + DesiredObjectKind: tree.TableObject, + } _, desc, err := resolver.ResolveExistingTableObject(ctx, p, tn, flags) if err != nil { return 0, err @@ -604,9 +607,7 @@ func (p *planner) ResolveTableName(ctx context.Context, tn *tree.TableName) (tre func (p *planner) LookupTableByID( ctx context.Context, tableID descpb.ID, ) (catalog.TableDescriptor, error) { - const required = true // lookups by ID are always "required" - table, err := p.Descriptors().GetImmutableTableByID(ctx, p.txn, tableID, p.ObjectLookupFlags( - required, false /* requireMutable */)) + table, err := p.byIDGetterBuilder().WithoutNonPublic().Get().Table(ctx, tableID) if err != nil { return nil, err } diff --git a/pkg/sql/privileged_accessor.go b/pkg/sql/privileged_accessor.go index b0d308146fcd..c93a5d4e16a6 100644 --- a/pkg/sql/privileged_accessor.go +++ b/pkg/sql/privileged_accessor.go @@ -80,16 +80,7 @@ func (p *planner) LookupZoneConfigByNamespaceID( // to check the permissions of a descriptor given its ID, or the id given // is not a descriptor of a table or database. func (p *planner) checkDescriptorPermissions(ctx context.Context, id descpb.ID) error { - desc, err := p.Descriptors().GetImmutableDescriptorByID( - ctx, p.txn, id, - tree.CommonLookupFlags{ - IncludeDropped: true, - IncludeOffline: true, - // Note that currently the ByID API implies required regardless of whether it - // is set. Set it just to be explicit. - Required: true, - }, - ) + desc, err := p.Descriptors().ByIDWithLeased(p.txn).Get().Desc(ctx, id) if err != nil { // Filter the error due to the descriptor not existing. if errors.Is(err, catalog.ErrDescriptorNotFound) { diff --git a/pkg/sql/reassign_owned_by.go b/pkg/sql/reassign_owned_by.go index 395525b2b397..4f818515b9c2 100644 --- a/pkg/sql/reassign_owned_by.go +++ b/pkg/sql/reassign_owned_by.go @@ -122,8 +122,7 @@ func (n *reassignOwnedByNode) startExec(params runParams) error { // Filter for all objects in current database. currentDatabase := params.p.CurrentDatabase() - currentDbDesc, err := params.p.Descriptors().GetMutableDatabaseByName( - params.ctx, params.p.txn, currentDatabase, tree.DatabaseLookupFlags{Required: true}) + currentDbDesc, err := params.p.Descriptors().MutableByName(params.p.txn).Database(params.ctx, currentDatabase) if err != nil { return err } @@ -202,7 +201,7 @@ func (n *reassignOwnedByNode) startExec(params runParams) error { func (n *reassignOwnedByNode) reassignDatabaseOwner( dbDesc catalog.DatabaseDescriptor, params runParams, ) error { - mutableDbDesc, err := params.p.Descriptors().GetMutableDescriptorByID(params.ctx, params.p.txn, dbDesc.GetID()) + mutableDbDesc, err := params.p.Descriptors().MutableByID(params.p.txn).Desc(params.ctx, dbDesc.GetID()) if err != nil { return err } @@ -231,7 +230,7 @@ func (n *reassignOwnedByNode) reassignDatabaseOwner( func (n *reassignOwnedByNode) reassignSchemaOwner( schemaDesc catalog.SchemaDescriptor, dbDesc *dbdesc.Mutable, params runParams, ) error { - mutableSchemaDesc, err := params.p.Descriptors().GetMutableDescriptorByID(params.ctx, params.p.txn, schemaDesc.GetID()) + mutableSchemaDesc, err := params.p.Descriptors().MutableByID(params.p.txn).Desc(params.ctx, schemaDesc.GetID()) if err != nil { return err } @@ -260,7 +259,7 @@ func (n *reassignOwnedByNode) reassignSchemaOwner( func (n *reassignOwnedByNode) reassignTableOwner( tbDesc catalog.TableDescriptor, params runParams, ) error { - mutableTbDesc, err := params.p.Descriptors().GetMutableDescriptorByID(params.ctx, params.p.txn, tbDesc.GetID()) + mutableTbDesc, err := params.p.Descriptors().MutableByID(params.p.txn).Desc(params.ctx, tbDesc.GetID()) if err != nil { return err } @@ -293,15 +292,14 @@ func (n *reassignOwnedByNode) reassignTableOwner( func (n *reassignOwnedByNode) reassignTypeOwner( typDesc catalog.TypeDescriptor, params runParams, ) error { - mutableTypDesc, err := params.p.Descriptors().GetMutableDescriptorByID(params.ctx, params.p.txn, typDesc.GetID()) + mutableTypDesc, err := params.p.Descriptors().MutableByID(params.p.txn).Desc(params.ctx, typDesc.GetID()) if err != nil { return err } if mutableTypDesc.Dropped() { return nil } - arrayDesc, err := params.p.Descriptors().GetMutableTypeVersionByID( - params.ctx, params.p.txn, typDesc.GetArrayTypeID()) + arrayDesc, err := params.p.Descriptors().MutableByID(params.p.txn).Type(params.ctx, typDesc.GetArrayTypeID()) if err != nil { return err } @@ -342,9 +340,7 @@ func (n *reassignOwnedByNode) reassignTypeOwner( func (n *reassignOwnedByNode) reassignFunctionOwner( fnDesc catalog.FunctionDescriptor, params runParams, ) error { - mutableDesc, err := params.p.Descriptors().GetMutableFunctionByID( - params.ctx, params.p.txn, fnDesc.GetID(), tree.ObjectLookupFlagsWithRequired(), - ) + mutableDesc, err := params.p.Descriptors().MutableByID(params.p.txn).Function(params.ctx, fnDesc.GetID()) if err != nil { return err } diff --git a/pkg/sql/region_util.go b/pkg/sql/region_util.go index 88a420c9da3b..97008fd1bfb8 100644 --- a/pkg/sql/region_util.go +++ b/pkg/sql/region_util.go @@ -1199,14 +1199,7 @@ func partitionByForRegionalByRow( // ValidateAllMultiRegionZoneConfigsInCurrentDatabase is part of the eval.DatabaseCatalog interface. func (p *planner) ValidateAllMultiRegionZoneConfigsInCurrentDatabase(ctx context.Context) error { - dbDesc, err := p.Descriptors().GetImmutableDatabaseByName( - ctx, - p.txn, - p.CurrentDatabase(), - tree.DatabaseLookupFlags{ - Required: true, - }, - ) + dbDesc, err := p.Descriptors().ByNameWithLeased(p.txn).Get().Database(ctx, p.CurrentDatabase()) if err != nil { return err } @@ -1243,7 +1236,7 @@ func (p *planner) ValidateAllMultiRegionZoneConfigsInCurrentDatabase(ctx context // zone configurations to match what would have originally been set by the // multi-region syntax. func (p *planner) ResetMultiRegionZoneConfigsForTable(ctx context.Context, id int64) error { - desc, err := p.Descriptors().GetMutableTableVersionByID(ctx, descpb.ID(id), p.txn) + desc, err := p.Descriptors().MutableByID(p.txn).Table(ctx, descpb.ID(id)) if err != nil { return errors.Wrapf(err, "error resolving referenced table ID %d", id) } @@ -1278,15 +1271,7 @@ func (p *planner) ResetMultiRegionZoneConfigsForTable(ctx context.Context, id in // database's zone configuration to match what would have originally been set by // the multi-region syntax. func (p *planner) ResetMultiRegionZoneConfigsForDatabase(ctx context.Context, id int64) error { - _, dbDesc, err := p.Descriptors().GetImmutableDatabaseByID( - ctx, - p.txn, - descpb.ID(id), - tree.DatabaseLookupFlags{ - Required: true, - AvoidLeased: true, - }, - ) + dbDesc, err := p.Descriptors().ByID(p.txn).WithoutNonPublic().Get().Database(ctx, descpb.ID(id)) if err != nil { return err } @@ -1380,14 +1365,7 @@ func (p *planner) validateAllMultiRegionZoneConfigsInDatabase( func (p *planner) CurrentDatabaseRegionConfig( ctx context.Context, ) (eval.DatabaseRegionConfig, error) { - dbDesc, err := p.Descriptors().GetImmutableDatabaseByName( - ctx, - p.txn, - p.CurrentDatabase(), - tree.DatabaseLookupFlags{ - Required: true, - }, - ) + dbDesc, err := p.Descriptors().ByNameWithLeased(p.txn).Get().Database(ctx, p.CurrentDatabase()) if err != nil { return nil, err } @@ -1592,11 +1570,17 @@ func getDBAndRegionEnumDescs( useCache bool, includeOffline bool, ) (dbDesc catalog.DatabaseDescriptor, regionEnumDesc catalog.TypeDescriptor, _ error) { - _, dbDesc, err := descsCol.GetImmutableDatabaseByID(ctx, txn, dbID, tree.DatabaseLookupFlags{ - AvoidLeased: !useCache, - Required: true, - IncludeOffline: includeOffline, - }) + var b descs.ByIDGetterBuilder + if useCache { + b = descsCol.ByIDWithLeased(txn) + } else { + b = descsCol.ByID(txn) + } + if !includeOffline { + b = b.WithoutOffline() + } + g := b.Get() + dbDesc, err := g.Database(ctx, dbID) if err != nil { return nil, nil, err } @@ -1607,18 +1591,7 @@ func getDBAndRegionEnumDescs( if err != nil { return nil, nil, err } - regionEnumDesc, err = descsCol.GetImmutableTypeByID( - ctx, - txn, - regionEnumID, - tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - AvoidLeased: !useCache, - Required: true, - IncludeOffline: includeOffline, - }, - }, - ) + regionEnumDesc, err = g.Type(ctx, regionEnumID) if err != nil { return nil, nil, err } @@ -1703,12 +1676,7 @@ func (p *planner) CheckZoneConfigChangePermittedForMultiRegion( // Check if what we're altering is a multi-region entity. if zs.Database != "" { isDB = true - dbDesc, err := p.Descriptors().GetImmutableDatabaseByName( - ctx, - p.txn, - string(zs.Database), - tree.DatabaseLookupFlags{Required: true}, - ) + dbDesc, err := p.Descriptors().ByNameWithLeased(p.txn).Get().Database(ctx, string(zs.Database)) if err != nil { return err } @@ -2478,11 +2446,7 @@ func (p *planner) OptimizeSystemDatabase(ctx context.Context) error { // Retrieve the system database descriptor and ensure it supports // multi-region - options := tree.CommonLookupFlags{ - AvoidLeased: true, - Required: true, - } - _, systemDB, err := p.Descriptors().GetImmutableDatabaseByID(ctx, p.txn, keys.SystemDatabaseID, options) + systemDB, err := p.Descriptors().ByID(p.txn).WithoutNonPublic().Get().Database(ctx, keys.SystemDatabaseID) if err != nil { return err } @@ -2490,9 +2454,7 @@ func (p *planner) OptimizeSystemDatabase(ctx context.Context) error { if err != nil { return errors.Wrap(err, "system database is not multi-region") } - enumTypeDesc, err := p.Descriptors().GetMutableTypeByID(ctx, p.txn, regionEnumID, tree.ObjectLookupFlags{ - CommonLookupFlags: options, - }) + enumTypeDesc, err := p.Descriptors().MutableByID(p.txn).Type(ctx, regionEnumID) if err != nil { return err } diff --git a/pkg/sql/rename_database.go b/pkg/sql/rename_database.go index 2cca0d1f98b8..6fb7198da94a 100644 --- a/pkg/sql/rename_database.go +++ b/pkg/sql/rename_database.go @@ -16,6 +16,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/dbdesc" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/descs" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" "github.com/cockroachdb/cockroach/pkg/sql/privilege" @@ -54,8 +55,7 @@ func (p *planner) RenameDatabase(ctx context.Context, n *tree.RenameDatabase) (p return nil, pgerror.DangerousStatementf("RENAME DATABASE on current database") } - dbDesc, err := p.Descriptors().GetMutableDatabaseByName(ctx, p.txn, string(n.Name), - tree.DatabaseLookupFlags{Required: true}) + dbDesc, err := p.Descriptors().MutableByName(p.txn).Database(ctx, string(n.Name)) if err != nil { return nil, err } @@ -119,19 +119,16 @@ func (n *renameDatabaseNode) startExec(params runParams) error { // Rather than trying to rewrite them with the changed DB name, we // simply disallow such renames for now. // See #34416. - lookupFlags := p.CommonLookupFlagsRequired() - // DDL statements bypass the cache. - lookupFlags.AvoidLeased = true schemas, err := p.Descriptors().GetSchemasForDatabase(ctx, p.txn, dbDesc) if err != nil { return err } for _, schema := range schemas { - sc, err := p.Descriptors().GetImmutableSchemaByName(ctx, p.txn, dbDesc, schema, lookupFlags) + sc, err := p.Descriptors().ByName(p.txn).Get().Schema(ctx, dbDesc, schema) if err != nil { return err } - if err := maybeFailOnDependentDescInRename(ctx, p, dbDesc, sc, lookupFlags, catalog.Database); err != nil { + if err := maybeFailOnDependentDescInRename(ctx, p, dbDesc, sc, !p.skipDescriptorCache, catalog.Database); err != nil { return err } } @@ -203,14 +200,20 @@ func maybeFailOnDependentDescInRename( p *planner, db catalog.DatabaseDescriptor, sc catalog.SchemaDescriptor, - lookupFlags tree.CommonLookupFlags, + withLeased bool, renameDescType catalog.DescriptorType, ) error { _, ids, err := p.GetObjectNamesAndIDs(ctx, db, sc) if err != nil { return err } - descs, err := p.Descriptors().GetImmutableDescriptorsByID(ctx, p.txn, lookupFlags, ids...) + var b descs.ByIDGetterBuilder + if withLeased { + b = p.Descriptors().ByIDWithLeased(p.txn) + } else { + b = p.Descriptors().ByID(p.txn) + } + descs, err := b.WithoutNonPublic().Get().Descs(ctx, ids) if err != nil { return err } @@ -228,7 +231,7 @@ func maybeFailOnDependentDescInRename( } if err := tbDesc.ForeachDependedOnBy(func(dependedOn *descpb.TableDescriptor_Reference) error { - dependentDesc, err := p.Descriptors().GetMutableDescriptorByID(ctx, p.txn, dependedOn.ID) + dependentDesc, err := p.Descriptors().MutableByID(p.txn).Desc(ctx, dependedOn.ID) if err != nil { return err } diff --git a/pkg/sql/rename_table.go b/pkg/sql/rename_table.go index 0ec5de6ccfa8..04b19422f0db 100644 --- a/pkg/sql/rename_table.go +++ b/pkg/sql/rename_table.go @@ -117,17 +117,11 @@ func (n *renameTableNode) startExec(params runParams) error { if !newTn.ExplicitSchema && !newTn.ExplicitCatalog { newTn.ObjectNamePrefix = oldTn.ObjectNamePrefix var err error - targetDbDesc, err = p.Descriptors().GetImmutableDatabaseByName(ctx, p.txn, - string(oldTn.CatalogName), tree.DatabaseLookupFlags{Required: true}) + targetDbDesc, err = p.Descriptors().ByNameWithLeased(p.txn).Get().Database(ctx, string(oldTn.CatalogName)) if err != nil { return err } - - targetSchemaDesc, err = p.Descriptors().GetImmutableSchemaByName( - ctx, p.txn, targetDbDesc, oldTn.Schema(), tree.SchemaLookupFlags{ - Required: true, - AvoidLeased: true, - }) + targetSchemaDesc, err = p.Descriptors().ByName(p.txn).Get().Schema(ctx, targetDbDesc, oldTn.Schema()) if err != nil { return err } @@ -252,7 +246,7 @@ func (n *renameTableNode) Close(context.Context) {} func (p *planner) dependentError( ctx context.Context, typeName string, objName string, parentID descpb.ID, id descpb.ID, op string, ) error { - desc, err := p.Descriptors().GetImmutableDescriptorByID(ctx, p.txn, id, tree.CommonLookupFlags{Required: true}) + desc, err := p.Descriptors().ByIDWithLeased(p.txn).WithoutNonPublic().Get().Desc(ctx, id) if err != nil { return err } @@ -324,13 +318,7 @@ func (n *renameTableNode) checkForCrossDbReferences( tableID = fk.GetOriginTableID() } - referencedTable, err := p.Descriptors().GetImmutableTableByID(ctx, p.txn, tableID, - tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - Required: true, - AvoidLeased: true, - }, - }) + referencedTable, err := p.Descriptors().ByID(p.txn).WithoutNonPublic().Get().Table(ctx, tableID) if err != nil { return err } @@ -354,12 +342,7 @@ func (n *renameTableNode) checkForCrossDbReferences( type crossDBDepType int const owner, reference crossDBDepType = 0, 1 checkDepForCrossDbRef := func(depID descpb.ID, depType crossDBDepType) error { - dependentObject, err := p.Descriptors().GetImmutableTableByID(ctx, p.txn, depID, - tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - Required: true, - AvoidLeased: true, - }}) + dependentObject, err := p.Descriptors().ByID(p.txn).WithoutNonPublic().Get().Table(ctx, depID) if err != nil { return err } @@ -455,12 +438,7 @@ func (n *renameTableNode) checkForCrossDbReferences( if allowCrossDatabaseViews.Get(&p.execCfg.Settings.SV) { return nil } - dependentObject, err := p.Descriptors().GetImmutableTypeByID(ctx, p.txn, depID, - tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - Required: true, - AvoidLeased: true, - }}) + dependentObject, err := p.Descriptors().ByID(p.txn).WithoutNonPublic().Get().Type(ctx, depID) if err != nil { return err } diff --git a/pkg/sql/repair.go b/pkg/sql/repair.go index 8e1fc61dc00b..7c0e64e90f28 100644 --- a/pkg/sql/repair.go +++ b/pkg/sql/repair.go @@ -35,7 +35,6 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgnotice" "github.com/cockroachdb/cockroach/pkg/sql/privilege" - "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/storage" "github.com/cockroachdb/cockroach/pkg/util/hlc" "github.com/cockroachdb/cockroach/pkg/util/ioctx" @@ -418,11 +417,8 @@ func (p *planner) UnsafeUpsertNamespaceEntry( if val.Value != nil { existingID = descpb.ID(val.ValueInt()) } - flags := p.CommonLookupFlagsRequired() - flags.IncludeDropped = true - flags.IncludeOffline = true validateDescriptor := func() error { - desc, err := p.Descriptors().GetImmutableDescriptorByID(ctx, p.Txn(), descID, flags) + desc, err := p.byIDGetterBuilder().Get().Desc(ctx, descID) if err != nil && descID != keys.PublicSchemaID { return errors.Wrapf(err, "failed to retrieve descriptor %d", descID) } @@ -456,7 +452,7 @@ func (p *planner) UnsafeUpsertNamespaceEntry( if parentID == descpb.InvalidID { return nil } - parent, err := p.Descriptors().GetImmutableDescriptorByID(ctx, p.Txn(), parentID, flags) + parent, err := p.byIDGetterBuilder().Get().Desc(ctx, parentID) if err != nil { return errors.Wrapf(err, "failed to look up parent %d", parentID) } @@ -470,7 +466,7 @@ func (p *planner) UnsafeUpsertNamespaceEntry( if parentSchemaID == descpb.InvalidID || parentSchemaID == keys.PublicSchemaID { return nil } - schema, err := p.Descriptors().GetImmutableDescriptorByID(ctx, p.Txn(), parentSchemaID, flags) + schema, err := p.byIDGetterBuilder().Get().Desc(ctx, parentSchemaID) if err != nil { return err } @@ -666,7 +662,7 @@ func (p *planner) UnsafeDeleteDescriptor(ctx context.Context, descID int64, forc func unsafeReadDescriptor( ctx context.Context, p *planner, id descpb.ID, force bool, ) (mut catalog.MutableDescriptor, notice error, err error) { - mut, err = p.Descriptors().GetMutableDescriptorByID(ctx, p.txn, id) + mut, err = p.Descriptors().MutableByID(p.txn).Desc(ctx, id) if mut != nil { return mut, nil, nil } @@ -718,14 +714,7 @@ func (p *planner) ForceDeleteTableData(ctx context.Context, descID int64) error // Validate no descriptor exists for this table id := descpb.ID(descID) - desc, err := p.Descriptors().GetImmutableTableByID(ctx, p.txn, id, - tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - Required: true, - AvoidLeased: true, - }, - DesiredTableDescKind: tree.ResolveRequireTableDesc, - }) + desc, err := p.Descriptors().ByID(p.txn).WithoutNonPublic().Get().Table(ctx, id) if err != nil && pgerror.GetPGCode(err) != pgcode.UndefinedTable { return err } diff --git a/pkg/sql/resolver.go b/pkg/sql/resolver.go index f75f0a9211fb..d05d8490302d 100644 --- a/pkg/sql/resolver.go +++ b/pkg/sql/resolver.go @@ -21,6 +21,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/dbdesc" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/descs" "github.com/cockroachdb/cockroach/pkg/sql/catalog/funcdesc" "github.com/cockroachdb/cockroach/pkg/sql/catalog/resolver" "github.com/cockroachdb/cockroach/pkg/sql/catalog/schemadesc" @@ -72,7 +73,7 @@ func (p *planner) resolveUncachedTableDescriptor( var desc catalog.Descriptor p.runWithOptions(resolveFlags{skipCache: true}, func() { lookupFlags := tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{Required: required}, + Required: required, DesiredObjectKind: tree.TableObject, DesiredTableDescKind: requiredType, } @@ -114,8 +115,7 @@ func (p *planner) ResolveTargetObject( func (p *planner) GetSchemasForDB( ctx context.Context, dbName string, ) (map[descpb.ID]string, error) { - dbDesc, err := p.Descriptors().GetImmutableDatabaseByName(ctx, p.txn, dbName, - tree.DatabaseLookupFlags{AvoidLeased: true}) + dbDesc, err := p.Descriptors().ByName(p.txn).Get().Database(ctx, dbName) if err != nil { return nil, err } @@ -191,24 +191,26 @@ func (p *planner) ResolveDescriptorForPrivilegeSpecifier( ctx context.Context, specifier eval.HasPrivilegeSpecifier, ) (catalog.Descriptor, error) { if specifier.DatabaseName != nil { - return p.Descriptors().GetImmutableDatabaseByName( - ctx, p.txn, *specifier.DatabaseName, tree.DatabaseLookupFlags{Required: true}, - ) + return p.Descriptors().ByNameWithLeased(p.txn).Get().Database(ctx, *specifier.DatabaseName) } else if specifier.DatabaseOID != nil { - _, database, err := p.Descriptors().GetImmutableDatabaseByID( - ctx, p.txn, descpb.ID(*specifier.DatabaseOID), tree.DatabaseLookupFlags{}, - ) + database, err := p.Descriptors().ByIDWithLeased(p.txn).WithoutNonPublic().Get().Database(ctx, descpb.ID(*specifier.DatabaseOID)) + // When a DatabaseOID is specified and the database is not found, + // we return NULL. + if err != nil && sqlerrors.IsUndefinedDatabaseError(err) { + // nolint:returnerrcheck + return nil, nil + } return database, err } else if specifier.SchemaName != nil { - database, err := p.Descriptors().GetImmutableDatabaseByName( - ctx, p.txn, *specifier.SchemaDatabaseName, tree.DatabaseLookupFlags{Required: true}, - ) + database, err := p.Descriptors().ByNameWithLeased(p.txn).Get().Database(ctx, *specifier.SchemaDatabaseName) if err != nil { return nil, err } - return p.Descriptors().GetImmutableSchemaByName( - ctx, p.txn, database, *specifier.SchemaName, tree.SchemaLookupFlags{Required: *specifier.SchemaIsRequired}, - ) + b := p.Descriptors().ByNameWithLeased(p.txn) + if *specifier.SchemaIsRequired { + return b.Get().Schema(ctx, database, *specifier.SchemaName) + } + return b.MaybeGet().Schema(ctx, database, *specifier.SchemaName) } else if specifier.TableName != nil || specifier.TableOID != nil { var table catalog.TableDescriptor var err error @@ -228,13 +230,9 @@ func (p *planner) ResolveDescriptorForPrivilegeSpecifier( return nil, pgerror.Newf(pgcode.FeatureNotSupported, "cross-database references are not implemented: %s", tn) } - _, table, err = p.Descriptors().GetImmutableTableByName( - ctx, p.txn, tn, tree.ObjectLookupFlags{}, - ) + _, table, err = descs.PrefixAndTable(ctx, p.Descriptors().ByNameWithLeased(p.txn).MaybeGet(), tn) } else { - table, err = p.Descriptors().GetImmutableTableByID( - ctx, p.txn, descpb.ID(*specifier.TableOID), tree.ObjectLookupFlags{}, - ) + table, err = p.Descriptors().ByIDWithLeased(p.txn).WithoutNonPublic().Get().Table(ctx, descpb.ID(*specifier.TableOID)) // When a TableOID is specified and the relation is not found, we return NULL. if err != nil && sqlerrors.IsUndefinedRelationError(err) { // nolint:returnerrcheck @@ -261,7 +259,7 @@ func (p *planner) ResolveDescriptorForPrivilegeSpecifier( return table, nil } else if specifier.FunctionOID != nil { fnID := funcdesc.UserDefinedFunctionOIDToID(*specifier.FunctionOID) - return p.Descriptors().GetImmutableFunctionByID(ctx, p.txn, fnID, tree.ObjectLookupFlagsWithRequired()) + return p.Descriptors().ByIDWithLeased(p.txn).WithoutNonPublic().Get().Function(ctx, fnID) } return nil, errors.AssertionFailedf("invalid HasPrivilegeSpecifier") } @@ -290,14 +288,6 @@ func validateColumnForHasPrivilegeSpecifier( return nil } -// ObjectLookupFlags is part of the resolver.SchemaResolver interface. -func (p *planner) ObjectLookupFlags(required, requireMutable bool) tree.ObjectLookupFlags { - flags := p.CommonLookupFlagsRequired() - flags.Required = required - flags.RequireMutable = requireMutable - return tree.ObjectLookupFlags{CommonLookupFlags: flags} -} - // DescriptorWithObjectType wraps a descriptor with the corresponding // privilege object type. type DescriptorWithObjectType struct { @@ -312,18 +302,13 @@ func (p *planner) getDescriptorsFromTargetListForPrivilegeChange( ctx context.Context, targets tree.GrantTargetList, ) ([]DescriptorWithObjectType, error) { const required = true - flags := tree.CommonLookupFlags{ - Required: required, - AvoidLeased: p.skipDescriptorCache, - } if targets.Databases != nil { if len(targets.Databases) == 0 { return nil, errNoDatabase } descs := make([]DescriptorWithObjectType, 0, len(targets.Databases)) for _, database := range targets.Databases { - descriptor, err := p.Descriptors(). - GetMutableDatabaseByName(ctx, p.txn, string(database), flags) + descriptor, err := p.Descriptors().MutableByName(p.txn).Database(ctx, string(database)) if err != nil { return nil, err } @@ -377,7 +362,7 @@ func (p *planner) getDescriptorsFromTargetListForPrivilegeChange( continue } fnResolved.Add(fnID) - fnDesc, err := p.Descriptors().GetMutableFunctionByID(ctx, p.txn, fnID, tree.ObjectLookupFlagsWithRequired()) + fnDesc, err := p.Descriptors().MutableByID(p.txn).Function(ctx, fnID) if err != nil { return nil, err } @@ -401,13 +386,11 @@ func (p *planner) getDescriptorsFromTargetListForPrivilegeChange( if scName.ExplicitCatalog { dbName = scName.Catalog() } - db, err := p.Descriptors().GetMutableDatabaseByName(ctx, p.txn, dbName, flags) + db, err := p.Descriptors().MutableByName(p.txn).Database(ctx, dbName) if err != nil { return nil, err } - sc, err := p.Descriptors().GetImmutableSchemaByName( - ctx, p.txn, db, scName.Schema(), p.CommonLookupFlagsRequired(), - ) + sc, err := p.byNameGetterBuilder().Get().Schema(ctx, db, scName.Schema()) if err != nil { return nil, err } @@ -415,7 +398,7 @@ func (p *planner) getDescriptorsFromTargetListForPrivilegeChange( if err != nil { return nil, err } - muts, err := p.Descriptors().GetMutableDescriptorsByID(ctx, p.txn, objectIDs...) + muts, err := p.Descriptors().MutableByID(p.txn).Descs(ctx, objectIDs) if err != nil { return nil, err } @@ -459,18 +442,16 @@ func (p *planner) getDescriptorsFromTargetListForPrivilegeChange( if scName.ExplicitCatalog { dbName = scName.Catalog() } - db, err := p.Descriptors().GetMutableDatabaseByName(ctx, p.txn, dbName, flags) + db, err := p.Descriptors().MutableByName(p.txn).Database(ctx, dbName) if err != nil { return nil, err } - sc, err := p.Descriptors().GetImmutableSchemaByName( - ctx, p.txn, db, scName.Schema(), tree.SchemaLookupFlags{Required: true, AvoidLeased: true}, - ) + sc, err := p.Descriptors().ByName(p.txn).Get().Schema(ctx, db, scName.Schema()) if err != nil { return nil, err } err = sc.ForEachFunctionOverload(func(overload descpb.SchemaDescriptor_FunctionOverload) error { - fn, err := p.Descriptors().GetMutableFunctionByID(ctx, p.txn, overload.ID, tree.ObjectLookupFlagsWithRequired()) + fn, err := p.Descriptors().MutableByID(p.txn).Function(ctx, overload.ID) if err != nil { return err } @@ -500,7 +481,7 @@ func (p *planner) getDescriptorsFromTargetListForPrivilegeChange( if sc.ExplicitCatalog { dbName = sc.Catalog() } - db, err := p.Descriptors().GetMutableDatabaseByName(ctx, p.txn, dbName, flags) + db, err := p.Descriptors().MutableByName(p.txn).Database(ctx, dbName) if err != nil { return nil, err } @@ -511,17 +492,13 @@ func (p *planner) getDescriptorsFromTargetListForPrivilegeChange( } for _, sc := range targetSchemas { - resSchema, err := p.Descriptors().GetImmutableSchemaByName( - ctx, p.txn, sc.dbDesc, sc.schema, flags, - ) + resSchema, err := p.byNameGetterBuilder().Get().Schema(ctx, sc.dbDesc, sc.schema) if err != nil { return nil, err } switch resSchema.SchemaKind() { case catalog.SchemaUserDefined: - mutSchema, err := p.Descriptors().GetMutableSchemaByID( - ctx, p.txn, resSchema.GetID(), flags, - ) + mutSchema, err := p.Descriptors().MutableByID(p.txn).Schema(ctx, resSchema.GetID()) if err != nil { return nil, err } @@ -550,7 +527,7 @@ func (p *planner) getDescriptorsFromTargetListForPrivilegeChange( if err != nil { return nil, err } - muts, err := p.Descriptors().GetMutableDescriptorsByID(ctx, p.txn, objectIDs...) + muts, err := p.Descriptors().MutableByID(p.txn).Descs(ctx, objectIDs) if err != nil { return nil, err } @@ -592,14 +569,7 @@ func (p *planner) getFullyQualifiedTableNamesFromIDs( ctx context.Context, ids []descpb.ID, ) (fullyQualifiedNames []string, _ error) { for _, id := range ids { - desc, err := p.Descriptors().GetImmutableDescriptorByID( - ctx, p.txn, id, - tree.CommonLookupFlags{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - }, - ) + desc, err := p.Descriptors().ByID(p.txn).Get().Desc(ctx, id) if err != nil { return nil, err } @@ -626,11 +596,7 @@ func (p *planner) getFullyQualifiedTableNamesFromIDs( func (p *planner) getQualifiedSchemaName( ctx context.Context, desc catalog.SchemaDescriptor, ) (*tree.ObjectNamePrefix, error) { - _, dbDesc, err := p.Descriptors().GetImmutableDatabaseByID(ctx, p.txn, desc.GetParentID(), - tree.DatabaseLookupFlags{ - Required: true, - AvoidLeased: true, - }) + dbDesc, err := p.Descriptors().ByID(p.txn).WithoutNonPublic().Get().Database(ctx, desc.GetParentID()) if err != nil { return nil, err } @@ -647,19 +613,13 @@ func (p *planner) getQualifiedSchemaName( func (p *planner) getQualifiedTypeName( ctx context.Context, desc catalog.TypeDescriptor, ) (*tree.TypeName, error) { - _, dbDesc, err := p.Descriptors().GetImmutableDatabaseByID(ctx, p.txn, desc.GetParentID(), - tree.DatabaseLookupFlags{ - Required: true, - AvoidLeased: true, - }) + dbDesc, err := p.Descriptors().ByID(p.txn).WithoutNonPublic().Get().Database(ctx, desc.GetParentID()) if err != nil { return nil, err } schemaID := desc.GetParentSchemaID() - scDesc, err := p.Descriptors().GetImmutableSchemaByID( - ctx, p.txn, schemaID, tree.SchemaLookupFlags{Required: true}, - ) + scDesc, err := p.Descriptors().ByIDWithLeased(p.txn).WithoutNonPublic().Get().Schema(ctx, schemaID) if err != nil { return nil, err } @@ -734,7 +694,7 @@ func expandIndexName( // Memoize the table name that was found. tn is a reference to the table name // stored in index.Table. *tn = tableName - tblMutable, err := p.Descriptors().GetMutableTableVersionByID(ctx, tbl.GetID(), p.Txn()) + tblMutable, err := p.Descriptors().MutableByID(p.Txn()).Table(ctx, tbl.GetID()) if err != nil { return nil, nil, err } @@ -787,7 +747,7 @@ func (p *planner) getTableAndIndexImpl( // Use the descriptor collection to get a proper handle to the mutable // descriptor for the relevant table and use that mutable object to // get a handle to the corresponding index. - mut, err := p.Descriptors().GetMutableTableVersionByID(ctx, tbl.GetID(), p.Txn()) + mut, err := p.Descriptors().MutableByID(p.Txn()).Table(ctx, tbl.GetID()) if err != nil { return catalog.ResolvedObjectPrefix{}, nil, nil, errors.NewAssertionErrorWithWrappedErrf(err, "failed to re-resolve table %d for index %s", tbl.GetID(), tableWithIndex) @@ -1210,7 +1170,8 @@ func (p *planner) ResolveMutableTableDescriptorExAllowNoPrimaryKey( requiredType tree.RequiredTableKind, ) (catalog.ResolvedObjectPrefix, *tabledesc.Mutable, error) { lookupFlags := tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{Required: required, RequireMutable: true}, + Required: required, + RequireMutable: true, AllowWithoutPrimaryKey: true, DesiredObjectKind: tree.TableObject, DesiredTableDescKind: requiredType, @@ -1252,11 +1213,11 @@ func (p *planner) ResolveExistingObjectEx( requiredType tree.RequiredTableKind, ) (res catalog.TableDescriptor, err error) { lookupFlags := tree.ObjectLookupFlags{ - CommonLookupFlags: p.CommonLookupFlagsRequired(), + Required: required, + AvoidLeased: p.skipDescriptorCache, DesiredObjectKind: tree.TableObject, DesiredTableDescKind: requiredType, } - lookupFlags.Required = required desc, prefix, err := resolver.ResolveExistingObject(ctx, p, name, lookupFlags) if err != nil || desc == nil { return nil, err diff --git a/pkg/sql/row/row_converter.go b/pkg/sql/row/row_converter.go index f8bc70d33b6b..43a8752e3ca1 100644 --- a/pkg/sql/row/row_converter.go +++ b/pkg/sql/row/row_converter.go @@ -277,12 +277,7 @@ func (c *DatumRowConverter) getSequenceAnnotation( if err := txn.SetFixedTimestamp(ctx, hlc.Timestamp{WallTime: evalCtx.TxnTimestamp.UnixNano()}); err != nil { return err } - flags := tree.CommonLookupFlags{ - AvoidLeased: true, - IncludeOffline: true, - IncludeDropped: true, - } - seqs, err := descsCol.GetImmutableDescriptorsByID(ctx, txn, flags, sequenceIDs.Ordered()...) + seqs, err := descsCol.ByID(txn).Get().Descs(ctx, sequenceIDs.Ordered()) if err != nil { return err } diff --git a/pkg/sql/rowexec/backfiller.go b/pkg/sql/rowexec/backfiller.go index 5da2a95eff80..ec0ec23eebbc 100644 --- a/pkg/sql/rowexec/backfiller.go +++ b/pkg/sql/rowexec/backfiller.go @@ -201,13 +201,7 @@ func GetResumeSpans( mutationID descpb.MutationID, filter backfill.MutationFilter, ) ([]roachpb.Span, *jobs.Job, int, error) { - tableDesc, err := col.GetImmutableTableByID(ctx, txn, tableID, tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - }, - }) + tableDesc, err := col.ByID(txn).Get().Table(ctx, tableID) if err != nil { return nil, nil, 0, err } diff --git a/pkg/sql/schema_change_plan_node.go b/pkg/sql/schema_change_plan_node.go index 6065b3d7b704..05b8ddc2930c 100644 --- a/pkg/sql/schema_change_plan_node.go +++ b/pkg/sql/schema_change_plan_node.go @@ -197,11 +197,7 @@ func (p *planner) waitForDescriptorSchemaChanges( if err := txn.SetFixedTimestamp(ctx, now); err != nil { return err } - desc, err := descriptors.GetImmutableDescriptorByID(ctx, txn, descID, - tree.CommonLookupFlags{ - Required: true, - AvoidLeased: true, - }) + desc, err := descriptors.ByID(txn).WithoutNonPublic().Get().Desc(ctx, descID) if err != nil { return err } diff --git a/pkg/sql/schema_changer.go b/pkg/sql/schema_changer.go index 4ebda18e3de4..73edd81e4e68 100644 --- a/pkg/sql/schema_changer.go +++ b/pkg/sql/schema_changer.go @@ -448,7 +448,7 @@ func (sc *SchemaChanger) maybeMakeAddTablePublic( log.Info(ctx, "making table public") return sc.txn(ctx, func(ctx context.Context, txn *kv.Txn, descsCol *descs.Collection) error { - mut, err := descsCol.GetMutableTableVersionByID(ctx, table.GetID(), txn) + mut, err := descsCol.MutableByID(txn).Table(ctx, table.GetID()) if err != nil { return err } @@ -489,7 +489,7 @@ func (sc *SchemaChanger) ignoreRevertedDropIndex( return nil } return sc.txn(ctx, func(ctx context.Context, txn *kv.Txn, descsCol *descs.Collection) error { - mut, err := descsCol.GetMutableTableVersionByID(ctx, table.GetID(), txn) + mut, err := descsCol.MutableByID(txn).Table(ctx, table.GetID()) if err != nil { return err } @@ -583,13 +583,7 @@ func (sc *SchemaChanger) getTargetDescriptor(ctx context.Context) (catalog.Descr if err := sc.txn(ctx, func( ctx context.Context, txn *kv.Txn, descriptors *descs.Collection, ) (err error) { - flags := tree.CommonLookupFlags{ - AvoidLeased: true, - Required: true, - IncludeOffline: true, - IncludeDropped: true, - } - desc, err = descriptors.GetImmutableDescriptorByID(ctx, txn, sc.descID, flags) + desc, err = descriptors.ByID(txn).Get().Desc(ctx, sc.descID) return err }); err != nil { return nil, err @@ -893,9 +887,7 @@ func (sc *SchemaChanger) handlePermanentSchemaChangeError( // initialize the job running status. func (sc *SchemaChanger) initJobRunningStatus(ctx context.Context) error { return sc.txn(ctx, func(ctx context.Context, txn *kv.Txn, descriptors *descs.Collection) error { - flags := tree.ObjectLookupFlagsWithRequired() - flags.AvoidLeased = true - desc, err := descriptors.GetImmutableTableByID(ctx, txn, sc.descID, flags) + desc, err := descriptors.ByID(txn).WithoutNonPublic().Get().Table(ctx, sc.descID) if err != nil { return err } @@ -938,7 +930,7 @@ func (sc *SchemaChanger) dropViewDeps( // Remove back-references from the tables/views this view depends on. dependedOn := append([]descpb.ID(nil), viewDesc.DependsOn...) for _, depID := range dependedOn { - dependencyDesc, err := descsCol.GetMutableTableVersionByID(ctx, depID, txn) + dependencyDesc, err := descsCol.MutableByID(txn).Table(ctx, depID) if err != nil { log.Warningf(ctx, "error resolving dependency relation ID %d", depID) continue @@ -958,7 +950,7 @@ func (sc *SchemaChanger) dropViewDeps( // If anything depends on this table clean up references from that object as well. DependedOnBy := append([]descpb.TableDescriptor_Reference(nil), viewDesc.DependedOnBy...) for _, depRef := range DependedOnBy { - dependencyDesc, err := descsCol.GetMutableTableVersionByID(ctx, depRef.ID, txn) + dependencyDesc, err := descsCol.MutableByID(txn).Table(ctx, depRef.ID) if err != nil { log.Warningf(ctx, "error resolving dependency relation ID %d", depRef.ID) continue @@ -978,14 +970,7 @@ func (sc *SchemaChanger) dropViewDeps( return err } for id := range typeClosure { - typeDesc, err := descsCol.GetMutableTypeByID(ctx, - txn, - id, - tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - AvoidLeased: true, - }, - }) + typeDesc, err := descsCol.MutableByID(txn).Type(ctx, id) if err != nil { log.Warningf(ctx, "error resolving type dependency %d", id) continue @@ -998,7 +983,7 @@ func (sc *SchemaChanger) dropViewDeps( } for i := 0; i < col.NumUsesSequences(); i++ { id := col.GetUsesSequenceID(i) - seqDesc, err := descsCol.GetMutableTableVersionByID(ctx, id, txn) + seqDesc, err := descsCol.MutableByID(txn).Table(ctx, id) if err != nil { log.Warningf(ctx, "error resolving sequence dependency %d", id) continue @@ -1045,7 +1030,7 @@ func (sc *SchemaChanger) rollbackSchemaChange(ctx context.Context, err error) er // clean up the descriptor. gcJobID := sc.jobRegistry.MakeJobID() if err := sc.txn(ctx, func(ctx context.Context, txn *kv.Txn, descsCol *descs.Collection) error { - scTable, err := descsCol.GetMutableTableVersionByID(ctx, sc.descID, txn) + scTable, err := descsCol.MutableByID(txn).Table(ctx, sc.descID) if err != nil { return err } @@ -1106,19 +1091,11 @@ func (sc *SchemaChanger) RunStateMachineBeforeBackfill(ctx context.Context) erro if err := sc.txn(ctx, func( ctx context.Context, txn *kv.Txn, descsCol *descs.Collection, ) error { - tbl, err := descsCol.GetMutableTableVersionByID(ctx, sc.descID, txn) + tbl, err := descsCol.MutableByID(txn).Table(ctx, sc.descID) if err != nil { return err } - _, dbDesc, err := descsCol.GetImmutableDatabaseByID( - ctx, - txn, - tbl.GetParentID(), - tree.DatabaseLookupFlags{ - Required: true, - AvoidLeased: true, - }, - ) + dbDesc, err := descsCol.ByID(txn).WithoutNonPublic().Get().Database(ctx, tbl.GetParentID()) if err != nil { return err } @@ -1220,7 +1197,7 @@ func (sc *SchemaChanger) stepStateMachineAfterIndexBackfill(ctx context.Context) if err := sc.txn(ctx, func( ctx context.Context, txn *kv.Txn, descsCol *descs.Collection, ) error { - tbl, err := descsCol.GetMutableTableVersionByID(ctx, sc.descID, txn) + tbl, err := descsCol.MutableByID(txn).Table(ctx, sc.descID) if err != nil { return err } @@ -1365,27 +1342,19 @@ func (sc *SchemaChanger) done(ctx context.Context) error { depMutationJobs = depMutationJobs[:0] otherJobIDs = otherJobIDs[:0] var err error - scTable, err := descsCol.GetMutableTableVersionByID(ctx, sc.descID, txn) + scTable, err := descsCol.MutableByID(txn).Table(ctx, sc.descID) if err != nil { return err } - _, dbDesc, err := descsCol.GetImmutableDatabaseByID( - ctx, - txn, - scTable.GetParentID(), - tree.DatabaseLookupFlags{ - Required: true, - AvoidLeased: true, - }, - ) + dbDesc, err := descsCol.ByID(txn).WithoutNonPublic().Get().Database(ctx, scTable.GetParentID()) if err != nil { return err } collectReferencedTypeIDs := func() (catalog.DescriptorIDSet, error) { typeLookupFn := func(id descpb.ID) (catalog.TypeDescriptor, error) { - desc, err := descsCol.GetImmutableTypeByID(ctx, txn, id, tree.ObjectLookupFlags{}) + desc, err := descsCol.ByIDWithLeased(txn).WithoutNonPublic().Get().Type(ctx, id) if err != nil { return nil, err } @@ -1441,7 +1410,7 @@ func (sc *SchemaChanger) done(ctx context.Context) error { if fk := m.AsForeignKey(); fk != nil && fk.Adding() && fk.GetConstraintValidity() == descpb.ConstraintValidity_Unvalidated { // Add backreference on the referenced table (which could be the same table) - backrefTable, err := descsCol.GetMutableTableVersionByID(ctx, fk.GetReferencedTableID(), txn) + backrefTable, err := descsCol.MutableByID(txn).Table(ctx, fk.GetReferencedTableID()) if err != nil { return err } @@ -1713,7 +1682,7 @@ func (sc *SchemaChanger) done(ctx context.Context) error { // Update the set of back references. for id, isAddition := range update { - typ, err := descsCol.GetMutableTypeVersionByID(ctx, txn, id) + typ, err := descsCol.MutableByID(txn).Type(ctx, id) if err != nil { return err } @@ -1754,7 +1723,7 @@ func (sc *SchemaChanger) done(ctx context.Context) error { // Update the set of back references. for id, colIDSet := range update { - tbl, err := descsCol.GetMutableTableVersionByID(ctx, id, txn) + tbl, err := descsCol.MutableByID(txn).Table(ctx, id) if err != nil { return err } @@ -1946,7 +1915,7 @@ func (sc *SchemaChanger) maybeReverseMutations(ctx context.Context, causingError alreadyReversed := false const kvTrace = true // TODO(ajwerner): figure this out err := sc.txn(ctx, func(ctx context.Context, txn *kv.Txn, descsCol *descs.Collection) error { - scTable, err := descsCol.GetMutableTableVersionByID(ctx, sc.descID, txn) + scTable, err := descsCol.MutableByID(txn).Table(ctx, sc.descID) if err != nil { return err } @@ -2019,7 +1988,7 @@ func (sc *SchemaChanger) maybeReverseMutations(ctx context.Context, causingError } // Get the foreign key backreferences to remove. if fk := constraint.AsForeignKey(); fk != nil { - backrefTable, err := descsCol.GetMutableTableVersionByID(ctx, fk.GetReferencedTableID(), txn) + backrefTable, err := descsCol.MutableByID(txn).Table(ctx, fk.GetReferencedTableID()) if err != nil { return err } @@ -3093,15 +3062,7 @@ func (sc *SchemaChanger) preSplitHashShardedIndexRanges(ctx context.Context) err ctx context.Context, txn *kv.Txn, descsCol *descs.Collection, ) error { hour := hlc.Timestamp{WallTime: timeutil.Now().Add(time.Hour).UnixNano()} - tableDesc, err := descsCol.GetMutableTableByID( - ctx, txn, sc.descID, - tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - IncludeOffline: true, - IncludeDropped: true, - }, - }, - ) + tableDesc, err := descsCol.MutableByID(txn).Table(ctx, sc.descID) if err != nil { return err } diff --git a/pkg/sql/schema_changer_test.go b/pkg/sql/schema_changer_test.go index 711ed7590e26..393c043a9d24 100644 --- a/pkg/sql/schema_changer_test.go +++ b/pkg/sql/schema_changer_test.go @@ -45,7 +45,6 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/execinfra" "github.com/cockroachdb/cockroach/pkg/sql/gcjob" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" - "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sqltestutils" "github.com/cockroachdb/cockroach/pkg/sql/stats" "github.com/cockroachdb/cockroach/pkg/sql/tests" @@ -1504,12 +1503,7 @@ CREATE TABLE t.test (k INT PRIMARY KEY, v INT); timeoutCtx, cancel := context.WithTimeout(ctx, base.DefaultDescriptorLeaseDuration/2) defer cancel() if err := sql.TestingDescsTxn(timeoutCtx, s, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) error { - flags := tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - Required: true, AvoidLeased: true, - }, - } - tbl, err := col.GetMutableTableByID(ctx, txn, tableDesc.GetID(), flags) + tbl, err := col.MutableByID(txn).Table(ctx, tableDesc.GetID()) if err != nil { return err } diff --git a/pkg/sql/schema_resolver.go b/pkg/sql/schema_resolver.go index cbbb02739768..75c72c074967 100644 --- a/pkg/sql/schema_resolver.go +++ b/pkg/sql/schema_resolver.go @@ -97,11 +97,7 @@ func (sr *schemaResolver) GetObjectNamesAndIDs( func (sr *schemaResolver) MustGetCurrentSessionDatabase( ctx context.Context, ) (catalog.DatabaseDescriptor, error) { - flags := tree.DatabaseLookupFlags{ - AvoidLeased: true, - Required: true, - } - return sr.descCollection.GetImmutableDatabaseByName(ctx, sr.txn, sr.CurrentDatabase(), flags) + return sr.descCollection.ByName(sr.txn).Get().Database(ctx, sr.CurrentDatabase()) } // CurrentSearchPath implements the resolver.SchemaResolver interface. @@ -109,21 +105,24 @@ func (sr *schemaResolver) CurrentSearchPath() sessiondata.SearchPath { return sr.sessionDataStack.Top().SearchPath } -// CommonLookupFlagsRequired is a convenience method for returning a -// default set of tree.CommonLookupFlags. -func (sr *schemaResolver) CommonLookupFlagsRequired() tree.CommonLookupFlags { - return tree.CommonLookupFlags{ - Required: true, - AvoidLeased: sr.skipDescriptorCache, +func (sr *schemaResolver) byIDGetterBuilder() descs.ByIDGetterBuilder { + if sr.skipDescriptorCache { + return sr.descCollection.ByID(sr.txn) + } + return sr.descCollection.ByIDWithLeased(sr.txn) +} + +func (sr *schemaResolver) byNameGetterBuilder() descs.ByNameGetterBuilder { + if sr.skipDescriptorCache { + return sr.descCollection.ByName(sr.txn) } + return sr.descCollection.ByNameWithLeased(sr.txn) } // LookupObject implements the tree.ObjectNameExistingResolver interface. func (sr *schemaResolver) LookupObject( ctx context.Context, flags tree.ObjectLookupFlags, dbName, scName, obName string, -) (found bool, prefix catalog.ResolvedObjectPrefix, objMeta catalog.Descriptor, err error) { - flags.CommonLookupFlags.Required = false - flags.CommonLookupFlags.AvoidLeased = sr.skipDescriptorCache +) (found bool, prefix catalog.ResolvedObjectPrefix, desc catalog.Descriptor, err error) { // Check if we are looking up a type which matches a built-in type in // CockroachDB but is an extension type on the public schema in PostgreSQL. @@ -137,8 +136,7 @@ func (sr *schemaResolver) LookupObject( if err != nil || !found { return found, prefix, nil, err } - dbDesc, err := sr.descCollection.GetImmutableDatabaseByName(ctx, sr.txn, dbName, - tree.DatabaseLookupFlags{AvoidLeased: sr.skipDescriptorCache}) + dbDesc, err := sr.byNameGetterBuilder().MaybeGet().Database(ctx, dbName) if err != nil { return found, prefix, nil, err } @@ -150,58 +148,49 @@ func (sr *schemaResolver) LookupObject( } } - prefix, objMeta, err = sr.GetObjectByName( - ctx, dbName, scName, obName, flags, - ) - return objMeta != nil, prefix, objMeta, err -} - -// GetObjectByName returns an object descriptor by name. -func (sr *schemaResolver) GetObjectByName( - ctx context.Context, catalogName, schemaName, objectName string, flags tree.ObjectLookupFlags, -) (prefix catalog.ResolvedObjectPrefix, desc catalog.Descriptor, err error) { - b := sr.descCollection.ByName(sr.txn).WithObjFlags(flags) - g := b.Immutable() - if catalogName != "" { - prefix.Database, err = g.Database(ctx, catalogName) - if err != nil || prefix.Database == nil { - return prefix, nil, err - } + b := sr.descCollection.ByName(sr.txn) + if !sr.skipDescriptorCache && !flags.RequireMutable { + b = sr.descCollection.ByNameWithLeased(sr.txn) } - prefix.Schema, err = g.Schema(ctx, prefix.Database, schemaName) - if err != nil || prefix.Schema == nil { - return prefix, nil, err - } - if flags.RequireMutable { - g = b.Mutable().AsByNameGetter() + if flags.IncludeOffline { + b = b.WithOffline() } + g := b.MaybeGet() + tn := tree.MakeQualifiedTypeName(dbName, scName, obName) switch flags.DesiredObjectKind { case tree.TableObject: - desc, err = g.Table(ctx, prefix.Database, prefix.Schema, objectName) + prefix, desc, err = descs.PrefixAndTable(ctx, g, &tn) case tree.TypeObject: - desc, err = g.Type(ctx, prefix.Database, prefix.Schema, objectName) + prefix, desc, err = descs.PrefixAndType(ctx, g, &tn) default: - return prefix, nil, errors.AssertionFailedf( + return false, prefix, nil, errors.AssertionFailedf( "unknown desired object kind %v", flags.DesiredObjectKind, ) } - if errors.Is(err, catalog.ErrDescriptorWrongType) && !flags.Required { - return prefix, nil, nil + if errors.Is(err, catalog.ErrDescriptorWrongType) { + return false, prefix, nil, nil + } + if flags.RequireMutable && desc != nil { + switch flags.DesiredObjectKind { + case tree.TableObject: + desc, err = sr.descCollection.MutableByID(sr.txn).Table(ctx, desc.GetID()) + case tree.TypeObject: + desc, err = sr.descCollection.MutableByID(sr.txn).Type(ctx, desc.GetID()) + } } - return prefix, desc, err + return desc != nil, prefix, desc, err } // LookupSchema implements the resolver.ObjectNameTargetResolver interface. func (sr *schemaResolver) LookupSchema( ctx context.Context, dbName, scName string, ) (found bool, scMeta catalog.ResolvedObjectPrefix, err error) { - flags := sr.CommonLookupFlagsRequired() - flags.Required = false - db, err := sr.descCollection.GetImmutableDatabaseByName(ctx, sr.txn, dbName, flags) + g := sr.byNameGetterBuilder().MaybeGet() + db, err := g.Database(ctx, dbName) if err != nil || db == nil { return false, catalog.ResolvedObjectPrefix{}, err } - sc, err := sr.descCollection.GetImmutableSchemaByName(ctx, sr.txn, db, scName, flags) + sc, err := g.Schema(ctx, db, scName) if err != nil || sc == nil { return false, catalog.ResolvedObjectPrefix{}, err } @@ -218,14 +207,7 @@ func (sr *schemaResolver) CurrentDatabase() string { func (sr *schemaResolver) GetQualifiedTableNameByID( ctx context.Context, id int64, requiredType tree.RequiredTableKind, ) (*tree.TableName, error) { - lookupFlags := tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{Required: true}, - DesiredObjectKind: tree.TableObject, - DesiredTableDescKind: requiredType, - } - - table, err := sr.descCollection.GetImmutableTableByID( - ctx, sr.txn, descpb.ID(id), lookupFlags) + table, err := sr.descCollection.ByIDWithLeased(sr.txn).WithoutNonPublic().Get().Table(ctx, descpb.ID(id)) if err != nil { return nil, err } @@ -238,13 +220,7 @@ func (sr *schemaResolver) GetQualifiedTableNameByID( func (sr *schemaResolver) getQualifiedTableName( ctx context.Context, desc catalog.TableDescriptor, ) (*tree.TableName, error) { - _, dbDesc, err := sr.descCollection.GetImmutableDatabaseByID(ctx, sr.txn, desc.GetParentID(), - tree.DatabaseLookupFlags{ - Required: true, - IncludeOffline: true, - IncludeDropped: true, - AvoidLeased: true, - }) + dbDesc, err := sr.descCollection.ByID(sr.txn).Get().Database(ctx, desc.GetParentID()) if err != nil { return nil, err } @@ -258,13 +234,7 @@ 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).Get().Schema(ctx, schemaID) switch { case scDesc != nil: schemaName = tree.Name(scDesc.GetName()) @@ -293,17 +263,11 @@ func (sr *schemaResolver) getQualifiedTableName( func (sr *schemaResolver) getQualifiedFunctionName( ctx context.Context, fnDesc catalog.FunctionDescriptor, ) (*tree.FunctionName, error) { - lookupFlags := tree.CommonLookupFlags{ - Required: true, - IncludeOffline: true, - IncludeDropped: true, - AvoidLeased: true, - } - _, dbDesc, err := sr.descCollection.GetImmutableDatabaseByID(ctx, sr.txn, fnDesc.GetParentID(), lookupFlags) + dbDesc, err := sr.descCollection.ByID(sr.txn).Get().Database(ctx, fnDesc.GetParentID()) if err != nil { return nil, err } - scDesc, err := sr.descCollection.GetImmutableSchemaByID(ctx, sr.txn, fnDesc.GetParentSchemaID(), lookupFlags) + scDesc, err := sr.descCollection.ByID(sr.txn).Get().Schema(ctx, fnDesc.GetParentSchemaID()) if err != nil { return nil, err } @@ -317,7 +281,8 @@ func (sr *schemaResolver) ResolveType( ctx context.Context, name *tree.UnresolvedObjectName, ) (*types.T, error) { lookupFlags := tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{Required: true, RequireMutable: false}, + Required: true, + RequireMutable: false, DesiredObjectKind: tree.TypeObject, } desc, prefix, err := resolver.ResolveExistingObject(ctx, sr, name, lookupFlags) @@ -363,25 +328,20 @@ func (sr *schemaResolver) ResolveTypeByOID(ctx context.Context, oid oid.Oid) (*t func (sr *schemaResolver) GetTypeDescriptor( ctx context.Context, id descpb.ID, ) (tree.TypeName, catalog.TypeDescriptor, error) { - tc := sr.descCollection - // Note that the value of required doesn't matter for lookups by ID. - flags := sr.CommonLookupFlagsRequired() - flags.ParentID = sr.typeResolutionDbID - desc, err := tc.GetImmutableTypeByID(ctx, sr.txn, id, tree.ObjectLookupFlags{ - CommonLookupFlags: flags, - }) + g := sr.byIDGetterBuilder().WithoutNonPublic().WithoutOtherParent(sr.typeResolutionDbID).Get() + desc, err := g.Type(ctx, id) if err != nil { return tree.TypeName{}, nil, err } dbName := sr.CurrentDatabase() if !descpb.IsVirtualTable(desc.GetID()) { - _, db, err := tc.GetImmutableDatabaseByID(ctx, sr.txn, desc.GetParentID(), flags) + db, err := g.Database(ctx, desc.GetParentID()) if err != nil { return tree.TypeName{}, nil, err } dbName = db.GetName() } - sc, err := tc.GetImmutableSchemaByID(ctx, sr.txn, desc.GetParentSchemaID(), flags) + sc, err := g.Schema(ctx, desc.GetParentSchemaID()) if err != nil { return tree.TypeName{}, nil, err } @@ -588,11 +548,9 @@ func (sr *schemaResolver) ResolveFunctionByOID( } } - flags := sr.CommonLookupFlagsRequired() - flags.ParentID = sr.typeResolutionDbID + g := sr.byIDGetterBuilder().WithoutNonPublic().WithoutOtherParent(sr.typeResolutionDbID).Get() descID := funcdesc.UserDefinedFunctionOIDToID(oid) - funcDesc, err := sr.descCollection.GetImmutableFunctionByID(ctx, sr.txn, descID, - tree.ObjectLookupFlags{CommonLookupFlags: flags}) + funcDesc, err := g.Function(ctx, descID) if err != nil { return "", nil, err } diff --git a/pkg/sql/schemachanger/scdeps/build_deps.go b/pkg/sql/schemachanger/scdeps/build_deps.go index e989c05d6071..54ceb8a2d0af 100644 --- a/pkg/sql/schemachanger/scdeps/build_deps.go +++ b/pkg/sql/schemachanger/scdeps/build_deps.go @@ -96,9 +96,7 @@ var _ scbuild.CatalogReader = (*buildDeps)(nil) func (d *buildDeps) MayResolveDatabase( ctx context.Context, name tree.Name, ) catalog.DatabaseDescriptor { - db, err := d.descsCollection.GetImmutableDatabaseByName(ctx, d.txn, string(name), tree.DatabaseLookupFlags{ - AvoidLeased: true, - }) + db, err := d.descsCollection.ByName(d.txn).MaybeGet().Database(ctx, string(name)) if err != nil { panic(err) } @@ -113,9 +111,7 @@ func (d *buildDeps) MayResolveSchema( name.CatalogName = tree.Name(d.schemaResolver.CurrentDatabase()) } db := d.MayResolveDatabase(ctx, name.CatalogName) - schema, err := d.descsCollection.GetImmutableSchemaByName(ctx, d.txn, db, name.Schema(), tree.SchemaLookupFlags{ - AvoidLeased: true, - }) + schema, err := d.descsCollection.ByName(d.txn).MaybeGet().Schema(ctx, db, name.Schema()) if err != nil { panic(err) } @@ -127,9 +123,7 @@ func (d *buildDeps) MayResolveTable( ctx context.Context, name tree.UnresolvedObjectName, ) (catalog.ResolvedObjectPrefix, catalog.TableDescriptor) { desc, prefix, err := resolver.ResolveExistingObject(ctx, d.schemaResolver, &name, tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - AvoidLeased: true, - }, + AvoidLeased: true, DesiredObjectKind: tree.TableObject, }) if err != nil { @@ -146,9 +140,7 @@ func (d *buildDeps) MayResolveType( ctx context.Context, name tree.UnresolvedObjectName, ) (catalog.ResolvedObjectPrefix, catalog.TypeDescriptor) { desc, prefix, err := resolver.ResolveExistingObject(ctx, d.schemaResolver, &name, tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - AvoidLeased: true, - }, + AvoidLeased: true, DesiredObjectKind: tree.TypeObject, }) if err != nil { @@ -230,14 +222,7 @@ func (d *buildDeps) CurrentDatabase() string { // MustReadDescriptor implements the scbuild.CatalogReader interface. func (d *buildDeps) MustReadDescriptor(ctx context.Context, id descpb.ID) catalog.Descriptor { - flags := tree.CommonLookupFlags{ - Required: true, - RequireMutable: false, - AvoidLeased: true, - IncludeOffline: true, - IncludeDropped: true, - } - desc, err := d.descsCollection.GetImmutableDescriptorByID(ctx, d.txn, id, flags) + desc, err := d.descsCollection.ByID(d.txn).Get().Desc(ctx, id) if err != nil { panic(err) } diff --git a/pkg/sql/schemachanger/scdeps/exec_deps.go b/pkg/sql/schemachanger/scdeps/exec_deps.go index 2420e2d9f380..398190a81a14 100644 --- a/pkg/sql/schemachanger/scdeps/exec_deps.go +++ b/pkg/sql/schemachanger/scdeps/exec_deps.go @@ -132,29 +132,13 @@ var _ scexec.Catalog = (*txnDeps)(nil) func (d *txnDeps) MustReadImmutableDescriptors( ctx context.Context, ids ...descpb.ID, ) ([]catalog.Descriptor, error) { - flags := tree.CommonLookupFlags{ - Required: true, - RequireMutable: false, - AvoidLeased: true, - IncludeOffline: true, - IncludeDropped: true, - AvoidSynthetic: true, - } - return d.descsCollection.GetImmutableDescriptorsByID(ctx, d.txn, flags, ids...) + return d.descsCollection.ByID(d.txn).WithoutSynthetic().Get().Descs(ctx, ids) } // GetFullyQualifiedName implements the scmutationexec.CatalogReader interface func (d *txnDeps) GetFullyQualifiedName(ctx context.Context, id descpb.ID) (string, error) { - flags := tree.CommonLookupFlags{ - Required: true, - IncludeOffline: true, - IncludeDropped: true, - AvoidLeased: true, - AvoidSynthetic: true, - } - objectDesc, err := d.descsCollection.GetImmutableDescriptorByID( - ctx, d.txn, id, flags, - ) + g := d.descsCollection.ByID(d.txn).WithoutSynthetic().Get() + objectDesc, err := g.Desc(ctx, id) if err != nil { return "", err } @@ -162,15 +146,11 @@ func (d *txnDeps) GetFullyQualifiedName(ctx context.Context, id descpb.ID) (stri // we can fetch the fully qualified names. if objectDesc.DescriptorType() != catalog.Database && objectDesc.DescriptorType() != catalog.Schema { - _, databaseDesc, err := d.descsCollection.GetImmutableDatabaseByID( - ctx, d.txn, objectDesc.GetParentID(), flags, - ) + databaseDesc, err := g.Database(ctx, objectDesc.GetParentID()) if err != nil { return "", err } - schemaDesc, err := d.descsCollection.GetImmutableSchemaByID( - ctx, d.txn, objectDesc.GetParentSchemaID(), flags, - ) + schemaDesc, err := g.Schema(ctx, objectDesc.GetParentSchemaID()) if err != nil { return "", err } @@ -183,9 +163,7 @@ func (d *txnDeps) GetFullyQualifiedName(ctx context.Context, id descpb.ID) (stri } else if objectDesc.DescriptorType() == catalog.Database { return objectDesc.GetName(), nil } else if objectDesc.DescriptorType() == catalog.Schema { - _, databaseDesc, err := d.descsCollection.GetImmutableDatabaseByID(ctx, - d.txn, objectDesc.GetParentID(), flags, - ) + databaseDesc, err := g.Database(ctx, objectDesc.GetParentID()) if err != nil { return "", err } @@ -198,7 +176,7 @@ func (d *txnDeps) GetFullyQualifiedName(ctx context.Context, id descpb.ID) (stri func (d *txnDeps) MustReadMutableDescriptor( ctx context.Context, id descpb.ID, ) (catalog.MutableDescriptor, error) { - return d.descsCollection.GetMutableDescriptorByID(ctx, d.txn, id) + return d.descsCollection.MutableByID(d.txn).Desc(ctx, id) } // AddSyntheticDescriptor is part of the @@ -336,13 +314,7 @@ func (d *txnDeps) MaybeSplitIndexSpans( func (d *txnDeps) GetResumeSpans( ctx context.Context, tableID descpb.ID, indexID descpb.IndexID, ) ([]roachpb.Span, error) { - table, err := d.descsCollection.GetImmutableTableByID(ctx, d.txn, tableID, tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - Required: true, - AvoidLeased: true, - AvoidSynthetic: true, - }, - }) + table, err := d.descsCollection.ByID(d.txn).WithoutNonPublic().WithoutSynthetic().Get().Table(ctx, tableID) if err != nil { return nil, err } diff --git a/pkg/sql/schemachanger/scdeps/run_deps.go b/pkg/sql/schemachanger/scdeps/run_deps.go index 91a9b9b5cac9..a9f8dd9de176 100644 --- a/pkg/sql/schemachanger/scdeps/run_deps.go +++ b/pkg/sql/schemachanger/scdeps/run_deps.go @@ -24,7 +24,6 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scexec" "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scexec/backfiller" "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scrun" - "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sessiondata" "github.com/cockroachdb/cockroach/pkg/util/timeutil" ) @@ -160,7 +159,7 @@ func (d *jobExecutionDeps) WithTxnInJob(ctx context.Context, fn scrun.JobTxnFunc ctx context.Context, txn *kv.Txn, descriptors *descs.Collection, ) error { for _, id := range tableStatsToRefresh { - tbl, err := descriptors.GetImmutableTableByID(ctx, txn, id, tree.ObjectLookupFlagsWithRequired()) + tbl, err := descriptors.ByIDWithLeased(txn).WithoutNonPublic().Get().Table(ctx, id) if err != nil { return err } diff --git a/pkg/sql/schemachanger/scexec/executor_external_test.go b/pkg/sql/schemachanger/scexec/executor_external_test.go index b7e9fef24de9..39b0f0398178 100644 --- a/pkg/sql/schemachanger/scexec/executor_external_test.go +++ b/pkg/sql/schemachanger/scexec/executor_external_test.go @@ -123,19 +123,7 @@ func TestExecutorDescriptorMutationOps(t *testing.T) { return cpy.ImmutableCopy().(catalog.TableDescriptor) } } - mutFlags := tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - Required: true, - RequireMutable: true, - AvoidLeased: true, - }, - } - immFlags := tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - Required: true, - AvoidLeased: true, - }, - } + run := func(t *testing.T, c testCase) { ctx := context.Background() ti := setupTestInfra(t) @@ -152,9 +140,7 @@ CREATE TABLE db.t ( require.NoError(t, ti.txn(ctx, func( ctx context.Context, txn *kv.Txn, descriptors *descs.Collection, ) (err error) { - if _, table, err = descriptors.GetMutableTableByName( - ctx, txn, &tn, mutFlags, - ); err != nil { + if _, table, err = descs.PrefixAndMutableTable(ctx, descriptors.MutableByName(txn), &tn); err != nil { return err } return nil @@ -164,11 +150,11 @@ CREATE TABLE db.t ( ctx context.Context, txn *kv.Txn, descriptors *descs.Collection, ) error { exDeps := ti.newExecDeps(txn, descriptors) - _, orig, err := descriptors.GetImmutableTableByName(ctx, txn, &tn, immFlags) + _, orig, err := descs.PrefixAndTable(ctx, descriptors.ByName(txn).Get(), &tn) require.NoError(t, err) require.Equal(t, c.orig().TableDesc(), orig.TableDesc()) require.NoError(t, scexec.ExecuteStage(ctx, exDeps, c.ops())) - _, after, err := descriptors.GetImmutableTableByName(ctx, txn, &tn, immFlags) + _, after, err := descs.PrefixAndTable(ctx, descriptors.ByName(txn).Get(), &tn) require.NoError(t, err) require.Equal(t, c.exp().TableDesc(), after.TableDesc()) return nil @@ -267,7 +253,7 @@ func TestSchemaChanger(t *testing.T) { ctx context.Context, txn *kv.Txn, descriptors *descs.Collection, ) (err error) { tn := tree.MakeTableNameWithSchema("db", tree.PublicSchemaName, "foo") - _, fooTable, err := descriptors.GetImmutableTableByName(ctx, txn, &tn, tree.ObjectLookupFlagsWithRequired()) + _, fooTable, err := descs.PrefixAndTable(ctx, descriptors.ByNameWithLeased(txn).Get(), &tn) require.NoError(t, err) stmts := []scpb.Statement{ diff --git a/pkg/sql/scrub.go b/pkg/sql/scrub.go index 1ec87ff0ce3d..de2dfc798f1d 100644 --- a/pkg/sql/scrub.go +++ b/pkg/sql/scrub.go @@ -152,8 +152,7 @@ func (n *scrubNode) Close(ctx context.Context) { func (n *scrubNode) startScrubDatabase(ctx context.Context, p *planner, name *tree.Name) error { // Check that the database exists. database := string(*name) - db, err := p.Descriptors().GetImmutableDatabaseByName(ctx, p.txn, - database, tree.DatabaseLookupFlags{Required: true}) + db, err := p.Descriptors().ByNameWithLeased(p.txn).Get().Database(ctx, database) if err != nil { return err } @@ -165,9 +164,7 @@ func (n *scrubNode) startScrubDatabase(ctx context.Context, p *planner, name *tr var tbNames tree.TableNames for _, schema := range schemas { - sc, err := p.Descriptors().GetImmutableSchemaByName( - ctx, p.txn, db, schema, p.CommonLookupFlagsRequired(), - ) + sc, err := p.byNameGetterBuilder().Get().Schema(ctx, db, schema) if err != nil { return err } @@ -180,9 +177,9 @@ func (n *scrubNode) startScrubDatabase(ctx context.Context, p *planner, name *tr for i := range tbNames { tableName := &tbNames[i] - _, objDesc, err := p.GetObjectByName( - ctx, tableName.Catalog(), tableName.Schema(), tableName.Table(), - p.ObjectLookupFlags(true /*required*/, false /*requireMutable*/), + flags := tree.ObjectLookupFlags{DesiredObjectKind: tree.TableObject} + _, _, objDesc, err := p.LookupObject( + ctx, flags, tableName.Catalog(), tableName.Schema(), tableName.Table(), ) // Skip over descriptors that are not tables (like types). // Note: We are asking for table objects above, so It's valid to only @@ -439,9 +436,7 @@ func createConstraintCheckOperations( if ck := constraint.AsCheck(); ck != nil { op = newSQLCheckConstraintCheckOperation(tableName, tableDesc, ck, asOf) } else if fk := constraint.AsForeignKey(); fk != nil { - referencedTable, err := p.Descriptors().GetImmutableTableByID( - ctx, p.Txn(), fk.GetReferencedTableID(), tree.ObjectLookupFlagsWithRequired(), - ) + referencedTable, err := p.Descriptors().ByIDWithLeased(p.Txn()).WithoutNonPublic().Get().Table(ctx, fk.GetReferencedTableID()) if err != nil { return nil, err } diff --git a/pkg/sql/sem/tree/name_resolution.go b/pkg/sql/sem/tree/name_resolution.go index 2420de8bba37..de619351feb7 100644 --- a/pkg/sql/sem/tree/name_resolution.go +++ b/pkg/sql/sem/tree/name_resolution.go @@ -17,7 +17,6 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" "github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants" - "github.com/cockroachdb/cockroach/pkg/sql/sem/catid" ) // This file contains the two major components to name resolution: @@ -156,42 +155,6 @@ func newInvTableNameError(n fmt.Stringer) error { "invalid table name: %s", n) } -// CommonLookupFlags is the common set of flags for the various accessor interfaces. -type CommonLookupFlags struct { - // Required specifies that the lookup will return an error if the item is - // not found. - Required bool - // RequireMutable specifies whether to return a mutable descriptor. - RequireMutable bool - // AvoidLeased, if set, avoid the leased (possibly stale) version of the - // descriptor. It must be set when callers want consistent reads. - AvoidLeased bool - // IncludeOffline specifies if offline descriptors should be visible. - IncludeOffline bool - // IncludeOffline specifies if dropped descriptors should be visible. - IncludeDropped bool - // AvoidSynthetic specifies if the synthetic descriptors will be ignored. - AvoidSynthetic bool - // ParentID enforces that the resolved descriptor exist with this parent - // ID if non-zero. - ParentID catid.DescID - // SkipHydration enforce descriptor lookups to skip hydration. This can be set - // to true only when looking up descriptors when hydrating another group of - // descriptors. The purpose is to avoid potential infinite recursion loop when - // trying to hydrate a descriptor which would lead to hydration of another - // descriptor depends on it. - SkipHydration bool -} - -// SchemaLookupFlags is the flag struct suitable for GetSchemaByName(). -type SchemaLookupFlags = CommonLookupFlags - -// DatabaseLookupFlags is the flag struct suitable for GetImmutableDatabaseByName(). -type DatabaseLookupFlags = CommonLookupFlags - -// DatabaseListFlags is the flag struct suitable for GetObjectNamesAndIDs(). -type DatabaseListFlags = CommonLookupFlags - // DesiredObjectKind represents what kind of object is desired in a name // resolution attempt. type DesiredObjectKind byte @@ -203,21 +166,6 @@ const ( TypeObject ) -// NewQualifiedObjectName returns an ObjectName of the corresponding kind. -// It is used mainly for constructing appropriate error messages depending -// on what kind of object was requested. -func NewQualifiedObjectName(catalog, schema, object string, kind DesiredObjectKind) ObjectName { - switch kind { - case TableObject: - name := MakeTableNameWithSchema(Name(catalog), Name(schema), Name(object)) - return &name - case TypeObject: - name := MakeQualifiedTypeName(catalog, schema, object) - return &name - } - return nil -} - // RequiredTableKind controls what kind of TableDescriptor backed object is // requested to be resolved. type RequiredTableKind byte @@ -245,7 +193,17 @@ func (r RequiredTableKind) String() string { // ObjectLookupFlags is the flag struct suitable for GetObjectByName(). type ObjectLookupFlags struct { - CommonLookupFlags + // Required specifies that the lookup will return an error if the item is + // not found. + Required bool + // RequireMutable specifies whether to return a mutable descriptor. + RequireMutable bool + // AvoidLeased, if set, avoid the leased (possibly stale) version of the + // descriptor. It must be set when callers want consistent reads. + AvoidLeased bool + // IncludeOffline specifies if offline descriptors should be visible. + IncludeOffline bool + // AllowWithoutPrimaryKey specifies if tables without PKs can be resolved. AllowWithoutPrimaryKey bool // Control what type of object is being requested. DesiredObjectKind DesiredObjectKind @@ -254,25 +212,6 @@ type ObjectLookupFlags struct { DesiredTableDescKind RequiredTableKind } -// ObjectLookupFlagsWithRequired returns a default ObjectLookupFlags object -// with just the Required flag true. This is a common configuration of the -// flags. -func ObjectLookupFlagsWithRequired() ObjectLookupFlags { - return ObjectLookupFlags{ - CommonLookupFlags: CommonLookupFlags{Required: true}, - } -} - -// ObjectLookupFlagsWithRequiredTableKind returns an ObjectLookupFlags with -// Required set to true, and the DesiredTableDescKind set to the input kind. -func ObjectLookupFlagsWithRequiredTableKind(kind RequiredTableKind) ObjectLookupFlags { - return ObjectLookupFlags{ - CommonLookupFlags: CommonLookupFlags{Required: true}, - DesiredObjectKind: TableObject, - DesiredTableDescKind: kind, - } -} - // IndexLookupFlags is the flag struct used for resolver.ResolveIndex() only. type IndexLookupFlags struct { // Required, if true, indicates lookup can return nil index without diff --git a/pkg/sql/sequence.go b/pkg/sql/sequence.go index f46608d023ee..b7589d8517d9 100644 --- a/pkg/sql/sequence.go +++ b/pkg/sql/sequence.go @@ -41,7 +41,11 @@ import ( func (p *planner) GetSerialSequenceNameFromColumn( ctx context.Context, tn *tree.TableName, columnName tree.Name, ) (*tree.TableName, error) { - flags := tree.ObjectLookupFlagsWithRequiredTableKind(tree.ResolveRequireTableDesc) + flags := tree.ObjectLookupFlags{ + Required: true, + DesiredObjectKind: tree.TableObject, + DesiredTableDescKind: tree.ResolveRequireTableDesc, + } _, tableDesc, err := resolver.ResolveExistingTableObject(ctx, p, tn, flags) if err != nil { return nil, err @@ -56,12 +60,7 @@ func (p *planner) GetSerialSequenceNameFromColumn( // as well as backward compatibility) so we're using this heuristic for now. // TODO(#52487): fix this up. if col.NumUsesSequences() == 1 { - seq, err := p.Descriptors().GetImmutableTableByID( - ctx, - p.txn, - col.GetUsesSequenceID(0), - tree.ObjectLookupFlagsWithRequiredTableKind(tree.ResolveRequireSequenceDesc), - ) + seq, err := p.Descriptors().ByIDWithLeased(p.txn).WithoutNonPublic().Get().Table(ctx, col.GetUsesSequenceID(0)) if err != nil { return nil, err } @@ -78,8 +77,7 @@ func (p *planner) IncrementSequenceByID(ctx context.Context, seqID int64) (int64 if p.EvalContext().TxnReadOnly { return 0, readOnlyError("nextval()") } - flags := tree.ObjectLookupFlagsWithRequiredTableKind(tree.ResolveRequireSequenceDesc) - descriptor, err := p.Descriptors().GetImmutableTableByID(ctx, p.txn, descpb.ID(seqID), flags) + descriptor, err := p.Descriptors().ByIDWithLeased(p.txn).WithoutNonPublic().Get().Table(ctx, descpb.ID(seqID)) if err != nil { return 0, err } @@ -251,8 +249,7 @@ func boundsExceededError(descriptor catalog.TableDescriptor) error { func (p *planner) GetLatestValueInSessionForSequenceByID( ctx context.Context, seqID int64, ) (int64, error) { - flags := tree.ObjectLookupFlagsWithRequiredTableKind(tree.ResolveRequireSequenceDesc) - descriptor, err := p.Descriptors().GetImmutableTableByID(ctx, p.txn, descpb.ID(seqID), flags) + descriptor, err := p.Descriptors().ByIDWithLeased(p.txn).WithoutNonPublic().Get().Table(ctx, descpb.ID(seqID)) if err != nil { return 0, err } @@ -290,8 +287,7 @@ func (p *planner) SetSequenceValueByID( return readOnlyError("setval()") } - flags := tree.ObjectLookupFlagsWithRequiredTableKind(tree.ResolveRequireSequenceDesc) - descriptor, err := p.Descriptors().GetImmutableTableByID(ctx, p.txn, descpb.ID(seqID), flags) + descriptor, err := p.Descriptors().ByIDWithLeased(p.txn).WithoutNonPublic().Get().Table(ctx, descpb.ID(seqID)) if err != nil { return err } @@ -515,7 +511,7 @@ func removeSequenceOwnerIfExists( if !opts.HasOwner() { return nil } - tableDesc, err := p.Descriptors().GetMutableTableVersionByID(ctx, opts.SequenceOwner.OwnerTableID, p.txn) + tableDesc, err := p.Descriptors().MutableByID(p.txn).Table(ctx, opts.SequenceOwner.OwnerTableID) if err != nil { // Special case error swallowing for #50711 and #50781, which can cause a // column to own sequences that have been dropped/do not exist. @@ -762,7 +758,7 @@ func (p *planner) dropSequencesOwnedByCol( } for _, sequenceID := range colOwnsSequenceIDs { - seqDesc, err := p.Descriptors().GetMutableTableVersionByID(ctx, sequenceID, p.txn) + seqDesc, err := p.Descriptors().MutableByID(p.txn).Table(ctx, sequenceID) // Special case error swallowing for #50781, which can cause a // column to own sequences that do not exist. if err != nil { @@ -802,7 +798,7 @@ func (p *planner) removeSequenceDependencies( for i := 0; i < col.NumUsesSequences(); i++ { sequenceID := col.GetUsesSequenceID(i) // Get the sequence descriptor so we can remove the reference from it. - seqDesc, err := p.Descriptors().GetMutableTableVersionByID(ctx, sequenceID, p.txn) + seqDesc, err := p.Descriptors().MutableByID(p.txn).Table(ctx, sequenceID) if err != nil { return err } diff --git a/pkg/sql/sessioninit/cache.go b/pkg/sql/sessioninit/cache.go index 9a7f2107393c..62211d26fdc2 100644 --- a/pkg/sql/sessioninit/cache.go +++ b/pkg/sql/sessioninit/cache.go @@ -126,21 +126,11 @@ func (a *Cache) GetAuthInfo( err = f.DescsTxn(ctx, db, func( ctx context.Context, txn *kv.Txn, descriptors *descs.Collection, ) error { - _, usersTableDesc, err = descriptors.GetImmutableTableByName( - ctx, - txn, - UsersTableName, - tree.ObjectLookupFlagsWithRequired(), - ) + _, usersTableDesc, err = descs.PrefixAndTable(ctx, descriptors.ByNameWithLeased(txn).Get(), UsersTableName) if err != nil { return err } - _, roleOptionsTableDesc, err = descriptors.GetImmutableTableByName( - ctx, - txn, - RoleOptionsTableName, - tree.ObjectLookupFlagsWithRequired(), - ) + _, roleOptionsTableDesc, err = descs.PrefixAndTable(ctx, descriptors.ByNameWithLeased(txn).Get(), RoleOptionsTableName) return err }) if err != nil { @@ -291,18 +281,13 @@ func (a *Cache) GetDefaultSettings( err = f.DescsTxn(ctx, db, func( ctx context.Context, txn *kv.Txn, descriptors *descs.Collection, ) error { - _, dbRoleSettingsTableDesc, err = descriptors.GetImmutableTableByName( - ctx, - txn, - DatabaseRoleSettingsTableName, - tree.ObjectLookupFlagsWithRequired(), - ) + _, dbRoleSettingsTableDesc, err = descs.PrefixAndTable(ctx, descriptors.ByNameWithLeased(txn).Get(), DatabaseRoleSettingsTableName) if err != nil { return err } databaseID = descpb.ID(0) if databaseName != "" { - dbDesc, err := descriptors.GetImmutableDatabaseByName(ctx, txn, databaseName, tree.DatabaseLookupFlags{}) + dbDesc, err := descriptors.ByNameWithLeased(txn).MaybeGet().Database(ctx, databaseName) if err != nil { return err } diff --git a/pkg/sql/set_schema.go b/pkg/sql/set_schema.go index c02d34a5ae7a..7e4a64d74cbe 100644 --- a/pkg/sql/set_schema.go +++ b/pkg/sql/set_schema.go @@ -43,12 +43,7 @@ func (p *planner) prepareSetSchema( } // Lookup the schema we want to set to. - res, err := p.Descriptors().GetImmutableSchemaByName( - ctx, p.txn, db, schema, tree.SchemaLookupFlags{ - Required: true, - RequireMutable: true, - AvoidLeased: true, - }) + res, err := p.Descriptors().ByName(p.txn).Get().Schema(ctx, db, schema) if err != nil { return 0, err } diff --git a/pkg/sql/set_zone_config.go b/pkg/sql/set_zone_config.go index 33162dc3ee3d..ca700778550d 100644 --- a/pkg/sql/set_zone_config.go +++ b/pkg/sql/set_zone_config.go @@ -286,8 +286,7 @@ func checkPrivilegeForSetZoneConfig(ctx context.Context, p *planner, zs tree.Zon if zs.Database == "system" { return p.RequireAdminRole(ctx, "alter the system database") } - dbDesc, err := p.Descriptors().GetImmutableDatabaseByName(ctx, p.txn, - string(zs.Database), tree.DatabaseLookupFlags{Required: true}) + dbDesc, err := p.Descriptors().ByNameWithLeased(p.txn).Get().Database(ctx, string(zs.Database)) if err != nil { return err } diff --git a/pkg/sql/show_create_table_test.go b/pkg/sql/show_create_table_test.go index 6beff87c3cc9..999be78899d1 100644 --- a/pkg/sql/show_create_table_test.go +++ b/pkg/sql/show_create_table_test.go @@ -52,9 +52,7 @@ func TestShowCreateTableWithConstraintInvalidated(t *testing.T) { ctx context.Context, txn *kv.Txn, descriptors *descs.Collection, ie sqlutil.InternalExecutor, ) error { tn := tree.MakeTableNameWithSchema("db", "schema", "table") - flags := tree.ObjectLookupFlagsWithRequired() - - _, mut, err := descriptors.GetMutableTableByName(ctx, txn, &tn, flags) + _, mut, err := descs.PrefixAndMutableTable(ctx, descriptors.MutableByName(txn), &tn) require.NoError(t, err) require.NotNil(t, mut) diff --git a/pkg/sql/show_zone_config.go b/pkg/sql/show_zone_config.go index c0895bae24c2..330602dcfc95 100644 --- a/pkg/sql/show_zone_config.go +++ b/pkg/sql/show_zone_config.go @@ -103,12 +103,7 @@ func getShowZoneConfigRow( return nil, err } } else if zoneSpecifier.Database != "" { - database, err := p.Descriptors().GetImmutableDatabaseByName( - ctx, - p.txn, - string(zoneSpecifier.Database), - tree.DatabaseLookupFlags{Required: true}, - ) + database, err := p.Descriptors().ByNameWithLeased(p.txn).Get().Database(ctx, string(zoneSpecifier.Database)) if err != nil { return nil, err } diff --git a/pkg/sql/sqlerrors/errors.go b/pkg/sql/sqlerrors/errors.go index 5cf3ef35f37e..7ae6412b7ebd 100644 --- a/pkg/sql/sqlerrors/errors.go +++ b/pkg/sql/sqlerrors/errors.go @@ -139,19 +139,6 @@ func NewInvalidWildcardError(name string) error { "%q does not match any valid database or schema", name) } -// NewUndefinedObjectError returns the correct undefined object error based on -// the kind of object that was requested. -func NewUndefinedObjectError(name tree.NodeFormatter, kind tree.DesiredObjectKind) error { - switch kind { - case tree.TableObject: - return NewUndefinedRelationError(name) - case tree.TypeObject: - return NewUndefinedTypeError(name) - default: - return errors.AssertionFailedf("unknown object kind %d", kind) - } -} - // NewUndefinedTypeError creates an error that represents a missing type. func NewUndefinedTypeError(name tree.NodeFormatter) error { return pgerror.Newf(pgcode.UndefinedObject, "type %q does not exist", tree.ErrString(name)) diff --git a/pkg/sql/stats/automatic_stats.go b/pkg/sql/stats/automatic_stats.go index fa8105453f5f..8766afb46a53 100644 --- a/pkg/sql/stats/automatic_stats.go +++ b/pkg/sql/stats/automatic_stats.go @@ -354,8 +354,7 @@ func (r *Refresher) getTableDescriptor( if err := r.cache.internalExecutorFactory.DescsTxn(ctx, r.cache.ClientDB, func( ctx context.Context, txn *kv.Txn, descriptors *descs.Collection, ) (err error) { - flags := tree.ObjectLookupFlagsWithRequired() - if desc, err = descriptors.GetImmutableTableByID(ctx, txn, tableID, flags); err != nil { + if desc, err = descriptors.ByIDWithLeased(txn).WithoutNonPublic().Get().Table(ctx, tableID); err != nil { err = errors.Wrapf(err, "failed to get table descriptor for automatic stats on table id: %d", tableID) } diff --git a/pkg/sql/syntheticprivilegecache/cache.go b/pkg/sql/syntheticprivilegecache/cache.go index 4fd75673033b..a7fac8e0adc9 100644 --- a/pkg/sql/syntheticprivilegecache/cache.go +++ b/pkg/sql/syntheticprivilegecache/cache.go @@ -71,12 +71,7 @@ func New( func (c *Cache) Get( ctx context.Context, txn *kv.Txn, col *descs.Collection, spo syntheticprivilege.Object, ) (*catpb.PrivilegeDescriptor, error) { - _, desc, err := col.GetImmutableTableByName( - ctx, - txn, - syntheticprivilege.SystemPrivilegesTableName, - tree.ObjectLookupFlagsWithRequired(), - ) + _, desc, err := descs.PrefixAndTable(ctx, col.ByNameWithLeased(txn).Get(), syntheticprivilege.SystemPrivilegesTableName) if err != nil { return nil, err } @@ -217,12 +212,7 @@ func (c *Cache) start(ctx context.Context) error { ) if err := c.ief.DescsTxnWithExecutor(ctx, c.db, nil /* sessionData */, func( ctx context.Context, txn *kv.Txn, descsCol *descs.Collection, ie sqlutil.InternalExecutor) (retErr error) { - _, systemPrivDesc, err := descsCol.GetImmutableTableByName( - ctx, - txn, - syntheticprivilege.SystemPrivilegesTableName, - tree.ObjectLookupFlagsWithRequired(), - ) + _, systemPrivDesc, err := descs.PrefixAndTable(ctx, descsCol.ByNameWithLeased(txn).Get(), syntheticprivilege.SystemPrivilegesTableName) if err != nil { return err } diff --git a/pkg/sql/table_test.go b/pkg/sql/table_test.go index 95a0cc1d19ba..33c24f725238 100644 --- a/pkg/sql/table_test.go +++ b/pkg/sql/table_test.go @@ -442,13 +442,7 @@ CREATE TABLE test.tt (x test.t); typLookup := func(ctx context.Context, id descpb.ID) (tree.TypeName, catalog.TypeDescriptor, error) { var typeDesc catalog.TypeDescriptor if err := TestingDescsTxn(ctx, s, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) (err error) { - typeDesc, err = col.GetImmutableTypeByID(ctx, txn, id, tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - }, - }) + typeDesc, err = col.ByID(txn).Get().Type(ctx, id) return err }); err != nil { return tree.TypeName{}, nil, err diff --git a/pkg/sql/temporary_schema.go b/pkg/sql/temporary_schema.go index 1e170f33e5e3..eae625eaa53e 100644 --- a/pkg/sql/temporary_schema.go +++ b/pkg/sql/temporary_schema.go @@ -98,8 +98,7 @@ func (p *planner) getOrCreateTemporarySchema( ctx context.Context, db catalog.DatabaseDescriptor, ) (catalog.SchemaDescriptor, error) { tempSchemaName := p.TemporarySchemaName() - flags := tree.CommonLookupFlags{AvoidLeased: true} - sc, err := p.Descriptors().GetImmutableSchemaByName(ctx, p.txn, db, tempSchemaName, flags) + sc, err := p.Descriptors().ByName(p.txn).MaybeGet().Schema(ctx, db, tempSchemaName) if sc != nil || err != nil { return sc, err } @@ -122,7 +121,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.byIDGetterBuilder().WithoutNonPublic().Get().Schema(ctx, id) } // temporarySchemaName returns the session specific temporary schema name given @@ -175,8 +174,7 @@ func cleanupSessionTempObjects( return err } for _, dbDesc := range allDbDescs { - flags := tree.CommonLookupFlags{AvoidLeased: true} - tempSchema, err := descsCol.GetImmutableSchemaByName(ctx, txn, dbDesc, tempSchemaName, flags) + tempSchema, err := descsCol.ByName(txn).MaybeGet().Schema(ctx, dbDesc, tempSchemaName) if err != nil { return err } @@ -307,17 +305,15 @@ func cleanupTempSchemaObjects( if _, ok := tblDescsByID[d.ID]; ok { return nil } - flags := tree.CommonLookupFlags{AvoidLeased: true} - dTableDesc, err := descsCol.GetImmutableTableByID( - ctx, txn, d.ID, tree.ObjectLookupFlags{CommonLookupFlags: flags}) + dTableDesc, err := descsCol.ByID(txn).WithoutNonPublic().Get().Table(ctx, d.ID) if err != nil { return err } - _, db, err := descsCol.GetImmutableDatabaseByID(ctx, txn, dTableDesc.GetParentID(), flags) + db, err := descsCol.ByID(txn).WithoutNonPublic().Get().Database(ctx, dTableDesc.GetParentID()) if err != nil { return err } - sc, err := descsCol.GetImmutableSchemaByID(ctx, txn, dTableDesc.GetParentSchemaID(), flags) + sc, err := descsCol.ByID(txn).WithoutNonPublic().Get().Schema(ctx, dTableDesc.GetParentSchemaID()) if err != nil { return err } diff --git a/pkg/sql/temporary_schema_test.go b/pkg/sql/temporary_schema_test.go index ffb9bd06ba39..94773b0045a0 100644 --- a/pkg/sql/temporary_schema_test.go +++ b/pkg/sql/temporary_schema_test.go @@ -25,7 +25,6 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog/descs" "github.com/cockroachdb/cockroach/pkg/sql/catalog/lease" "github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants" - "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sqlutil" "github.com/cockroachdb/cockroach/pkg/sql/tests" "github.com/cockroachdb/cockroach/pkg/testutils" @@ -104,14 +103,11 @@ INSERT INTO perm_table VALUES (DEFAULT, 1); ) error { // Add a hack to not wait for one version on the descriptors. defer descsCol.ReleaseAll(ctx) - _, defaultDB, err := descsCol.GetImmutableDatabaseByID( - ctx, txn, namesToID["defaultdb"], tree.CommonLookupFlags{AvoidLeased: true}, - ) + defaultDB, err := descsCol.ByID(txn).WithoutNonPublic().Get().Database(ctx, namesToID["defaultdb"]) if err != nil { return err } - flags := tree.CommonLookupFlags{Required: true, AvoidLeased: true} - tempSchema, err := descsCol.GetImmutableSchemaByName(ctx, txn, defaultDB, tempSchemaName, flags) + tempSchema, err := descsCol.ByName(txn).Get().Schema(ctx, defaultDB, tempSchemaName) if err != nil { return err } diff --git a/pkg/sql/truncate.go b/pkg/sql/truncate.go index c4de5784ca0b..36f56b301efb 100644 --- a/pkg/sql/truncate.go +++ b/pkg/sql/truncate.go @@ -89,7 +89,7 @@ func (t *truncateNode) startExec(params runParams) error { if _, ok := toTruncate[tableID]; ok { return nil } - other, err := p.Descriptors().GetMutableTableVersionByID(ctx, tableID, p.txn) + other, err := p.Descriptors().MutableByID(p.txn).Table(ctx, tableID) if err != nil { return err } @@ -163,7 +163,7 @@ var PreservedSplitCountMultiple = settings.RegisterIntSetting( func (p *planner) truncateTable(ctx context.Context, id descpb.ID, jobDesc string) error { // Read the table descriptor because it might have changed // while another table in the truncation list was truncated. - tableDesc, err := p.Descriptors().GetMutableTableVersionByID(ctx, id, p.txn) + tableDesc, err := p.Descriptors().MutableByID(p.txn).Table(ctx, id) if err != nil { return err } diff --git a/pkg/sql/ttl/ttljob/ttljob.go b/pkg/sql/ttl/ttljob/ttljob.go index 65685384011c..208ab4a425da 100644 --- a/pkg/sql/ttl/ttljob/ttljob.go +++ b/pkg/sql/ttl/ttljob/ttljob.go @@ -113,12 +113,7 @@ func (t rowLevelTTLResumer) Resume(ctx context.Context, execCtx interface{}) err var relationName string var entirePKSpan roachpb.Span if err := db.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error { - desc, err := descsCol.GetImmutableTableByID( - ctx, - txn, - details.TableID, - tree.ObjectLookupFlagsWithRequired(), - ) + desc, err := descsCol.ByIDWithLeased(txn).WithoutNonPublic().Get().Table(ctx, details.TableID) if err != nil { return err } diff --git a/pkg/sql/ttl/ttljob/ttljob_processor.go b/pkg/sql/ttl/ttljob/ttljob_processor.go index 4548bbbe0da1..a9739cfd0cbb 100644 --- a/pkg/sql/ttl/ttljob/ttljob_processor.go +++ b/pkg/sql/ttl/ttljob/ttljob_processor.go @@ -69,12 +69,7 @@ func (t *ttlProcessor) work(ctx context.Context) error { var pkTypes []*types.T var labelMetrics bool if err := serverCfg.DB.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error { - desc, err := descsCol.GetImmutableTableByID( - ctx, - txn, - details.TableID, - tree.ObjectLookupFlagsWithRequired(), - ) + desc, err := descsCol.ByIDWithLeased(txn).WithoutNonPublic().Get().Table(ctx, details.TableID) if err != nil { return err } @@ -302,12 +297,7 @@ func (t *ttlProcessor) runTTLOnSpan( if err := serverCfg.DB.TxnWithSteppingEnabled(ctx, sessiondatapb.TTLLow, func(ctx context.Context, txn *kv.Txn) error { // If we detected a schema change here, the DELETE will not succeed // (the SELECT still will because of the AOST). Early exit here. - desc, err := flowCtx.Descriptors.GetImmutableTableByID( - ctx, - txn, - details.TableID, - tree.ObjectLookupFlagsWithRequired(), - ) + desc, err := flowCtx.Descriptors.ByIDWithLeased(txn).WithoutNonPublic().Get().Table(ctx, details.TableID) if err != nil { return err } diff --git a/pkg/sql/ttl/ttlschedule/ttlschedule.go b/pkg/sql/ttl/ttlschedule/ttlschedule.go index 2aa58d1f6567..6ef8e4853f3d 100644 --- a/pkg/sql/ttl/ttlschedule/ttlschedule.go +++ b/pkg/sql/ttl/ttlschedule/ttlschedule.go @@ -71,9 +71,7 @@ func (s rowLevelTTLExecutor) OnDrop( } if !canDrop { - tbl, err := descsCol.GetImmutableTableByID( - ctx, txn, args.TableID, tree.ObjectLookupFlagsWithRequired(), - ) + tbl, err := descsCol.ByIDWithLeased(txn).WithoutNonPublic().Get().Table(ctx, args.TableID) if err != nil { return 0, err } @@ -103,7 +101,7 @@ func canDropTTLSchedule( schedule *jobs.ScheduledJob, args catpb.ScheduledRowLevelTTLArgs, ) (bool, error) { - desc, err := descsCol.GetImmutableTableByID(ctx, txn, args.TableID, tree.ObjectLookupFlags{}) + desc, err := descsCol.ByIDWithLeased(txn).WithoutNonPublic().Get().Table(ctx, args.TableID) if err != nil { // If the descriptor does not exist we can drop this schedule. if sqlerrors.IsUndefinedRelationError(err) { @@ -210,9 +208,7 @@ func (s rowLevelTTLExecutor) GetCreateScheduleStatement( if err := pbtypes.UnmarshalAny(sj.ExecutionArgs().Args, args); err != nil { return "", err } - tbl, err := descsCol.GetImmutableTableByID( - ctx, txn, args.TableID, tree.ObjectLookupFlagsWithRequired(), - ) + tbl, err := descsCol.ByIDWithLeased(txn).WithoutNonPublic().Get().Table(ctx, args.TableID) if err != nil { return "", err } @@ -258,7 +254,7 @@ func createRowLevelTTLJob( jobRegistry *jobs.Registry, ttlArgs catpb.ScheduledRowLevelTTLArgs, ) (jobspb.JobID, error) { - tableDesc, err := descsCol.GetImmutableTableByID(ctx, txn, ttlArgs.TableID, tree.ObjectLookupFlagsWithRequired()) + tableDesc, err := descsCol.ByIDWithLeased(txn).WithoutNonPublic().Get().Table(ctx, ttlArgs.TableID) if err != nil { return 0, err } diff --git a/pkg/sql/type_change.go b/pkg/sql/type_change.go index 6a00669b2b98..584aeeeca495 100644 --- a/pkg/sql/type_change.go +++ b/pkg/sql/type_change.go @@ -197,14 +197,9 @@ func (t *typeSchemaChanger) getTypeDescFromStore( ) (catalog.TypeDescriptor, error) { var typeDesc catalog.TypeDescriptor if err := DescsTxn(ctx, t.execCfg, func(ctx context.Context, txn *kv.Txn, col *descs.Collection) error { - flags := tree.ObjectLookupFlags{CommonLookupFlags: tree.CommonLookupFlags{ - AvoidLeased: true, - IncludeDropped: true, - IncludeOffline: true, - }} // Avoid GetImmutableTypeByID, downstream logic relies on // catalog.ErrDescriptorNotFound. - desc, err := col.GetImmutableDescriptorByID(ctx, txn, t.typeID, flags.CommonLookupFlags) + desc, err := col.ByID(txn).Get().Desc(ctx, t.typeID) if err != nil { return err } @@ -307,7 +302,7 @@ func (t *typeSchemaChanger) exec(ctx context.Context) error { ctx context.Context, txn *kv.Txn, descsCol *descs.Collection, f func(finalizer *databaseRegionChangeFinalizer) error, ) error { - typeDesc, err := descsCol.GetMutableTypeVersionByID(ctx, txn, t.typeID) + typeDesc, err := descsCol.MutableByID(txn).Type(ctx, t.typeID) if err != nil { return err } @@ -350,7 +345,7 @@ func (t *typeSchemaChanger) exec(ctx context.Context) error { // use and fail. This is done in a separate txn to the one that mutates the // descriptor, as this validation can take arbitrarily long. validateDrops := func(ctx context.Context, txn *kv.Txn, descsCol *descs.Collection) error { - typeDesc, err := descsCol.GetMutableTypeVersionByID(ctx, txn, t.typeID) + typeDesc, err := descsCol.MutableByID(txn).Type(ctx, t.typeID) if err != nil { return err } @@ -407,7 +402,7 @@ func (t *typeSchemaChanger) exec(ctx context.Context) error { // have performed any necessary pre-drop work, we can actually go about // modifying the type descriptor. run := func(ctx context.Context, txn *kv.Txn, descsCol *descs.Collection) error { - typeDesc, err := descsCol.GetMutableTypeVersionByID(ctx, txn, t.typeID) + typeDesc, err := descsCol.MutableByID(txn).Type(ctx, t.typeID) if err != nil { return err } @@ -453,7 +448,7 @@ func (t *typeSchemaChanger) exec(ctx context.Context) error { // The version of the array type needs to get bumped as well so that // changes to the underlying type are picked up. Simply reading the // mutable descriptor and writing it back should do the trick. - arrayTypeDesc, err := descsCol.GetMutableTypeVersionByID(ctx, txn, typeDesc.ArrayTypeID) + arrayTypeDesc, err := descsCol.MutableByID(txn).Type(ctx, typeDesc.ArrayTypeID) if err != nil { return err } @@ -551,7 +546,7 @@ func (t *typeSchemaChanger) cleanupEnumValues(ctx context.Context) error { var regionChangeFinalizer *databaseRegionChangeFinalizer // Cleanup: cleanup := func(ctx context.Context, txn *kv.Txn, descsCol *descs.Collection) error { - typeDesc, err := descsCol.GetMutableTypeVersionByID(ctx, txn, t.typeID) + typeDesc, err := descsCol.MutableByID(txn).Type(ctx, t.typeID) if err != nil { return err } @@ -762,12 +757,7 @@ func (t *typeSchemaChanger) canRemoveEnumValue( descsCol *descs.Collection, ) error { for _, ID := range typeDesc.ReferencingDescriptorIDs { - desc, err := descsCol.GetImmutableTableByID(ctx, txn, ID, tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - AvoidLeased: true, - Required: true, - }, - }) + desc, err := descsCol.ByID(txn).WithoutNonPublic().Get().Table(ctx, ID) if err != nil { return errors.Wrapf(err, "could not validate enum value removal for %q", member.LogicalRepresentation) @@ -922,11 +912,7 @@ func (t *typeSchemaChanger) canRemoveEnumValue( // be unset by default) when executing the query constructed above. This is // because the enum value may be used in a view expression, which is // name resolved in the context of the type's database. - _, dbDesc, err := descsCol.GetImmutableDatabaseByID( - ctx, txn, typeDesc.ParentID, tree.DatabaseLookupFlags{ - Required: true, - AvoidLeased: true, - }) + dbDesc, err := descsCol.ByID(txn).WithoutNonPublic().Get().Database(ctx, typeDesc.ParentID) const validationErr = "could not validate removal of enum value %q" if err != nil { return errors.Wrapf(err, validationErr, member.LogicalRepresentation) @@ -964,8 +950,7 @@ func (t *typeSchemaChanger) canRemoveEnumValue( } // Do validation for the array type now. - arrayTypeDesc, err := descsCol.GetImmutableTypeByID( - ctx, txn, typeDesc.ArrayTypeID, tree.ObjectLookupFlags{}) + arrayTypeDesc, err := descsCol.ByIDWithLeased(txn).WithoutNonPublic().Get().Type(ctx, typeDesc.ArrayTypeID) if err != nil { return err } @@ -1099,7 +1084,7 @@ func (t *typeSchemaChanger) canRemoveEnumValueFromArrayUsages( const validationErr = "could not validate removal of enum value %q" for i := 0; i < arrayTypeDesc.NumReferencingDescriptors(); i++ { id := arrayTypeDesc.GetReferencingDescriptorID(i) - desc, err := descsCol.GetImmutableTableByID(ctx, txn, id, tree.ObjectLookupFlags{}) + desc, err := descsCol.ByIDWithLeased(txn).WithoutNonPublic().Get().Table(ctx, id) if err != nil { return errors.Wrapf(err, validationErr, member.LogicalRepresentation) } @@ -1148,11 +1133,7 @@ func (t *typeSchemaChanger) canRemoveEnumValueFromArrayUsages( } query.WriteString(fmt.Sprintf(") WHERE unnest = %s", sqlPhysRep)) - _, dbDesc, err := descsCol.GetImmutableDatabaseByID( - ctx, txn, arrayTypeDesc.GetParentID(), tree.DatabaseLookupFlags{ - Required: true, - AvoidLeased: true, - }) + dbDesc, err := descsCol.ByID(txn).WithoutNonPublic().Get().Database(ctx, arrayTypeDesc.GetParentID()) if err != nil { return errors.Wrapf(err, validationErr, member.LogicalRepresentation) } @@ -1172,8 +1153,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.ByIDWithLeased(txn).WithoutNonPublic().Get().Schema(ctx, desc.GetParentSchemaID()) if err != nil { return err } diff --git a/pkg/sql/user.go b/pkg/sql/user.go index a5158e010ae6..79082b846eea 100644 --- a/pkg/sql/user.go +++ b/pkg/sql/user.go @@ -769,9 +769,7 @@ func updateUserPasswordHash( // Error, or no update took place. return err } - usersTable, err := d.GetMutableTableByID( - ctx, txn, keys.UsersTableID, tree.ObjectLookupFlagsWithRequired(), - ) + usersTable, err := d.MutableByID(txn).Table(ctx, keys.UsersTableID) if err != nil { return err } diff --git a/pkg/sql/vars.go b/pkg/sql/vars.go index 8d2578aaabfd..bd118662efd0 100644 --- a/pkg/sql/vars.go +++ b/pkg/sql/vars.go @@ -291,9 +291,7 @@ var varGen = map[string]sessionVar{ if len(dbName) != 0 { // Verify database descriptor exists. - if _, err := evalCtx.Descs.GetImmutableDatabaseByName( - ctx, txn, dbName, tree.DatabaseLookupFlags{Required: true}, - ); err != nil { + if _, err := evalCtx.Descs.ByNameWithLeased(txn).Get().Database(ctx, dbName); err != nil { return "", err } } diff --git a/pkg/sql/virtual_schema.go b/pkg/sql/virtual_schema.go index dc5331eaca0a..eb21497b4425 100644 --- a/pkg/sql/virtual_schema.go +++ b/pkg/sql/virtual_schema.go @@ -596,10 +596,7 @@ func (e *virtualDefEntry) getPlanInfo( var dbDesc catalog.DatabaseDescriptor var err error if dbName != "" { - dbDesc, err = p.Descriptors().GetImmutableDatabaseByName(ctx, p.txn, - dbName, tree.DatabaseLookupFlags{ - Required: true, AvoidLeased: p.skipDescriptorCache, - }) + dbDesc, err = p.byNameGetterBuilder().Get().Database(ctx, dbName) if err != nil { return nil, err } diff --git a/pkg/sql/virtual_table.go b/pkg/sql/virtual_table.go index 3c7fa3790aa2..0e7afec4463e 100644 --- a/pkg/sql/virtual_table.go +++ b/pkg/sql/virtual_table.go @@ -271,14 +271,7 @@ func (v *vTableLookupJoinNode) startExec(params runParams) error { } v.run.indexKeyDatums = make(tree.Datums, len(v.columns)) var err error - db, err := params.p.Descriptors().GetImmutableDatabaseByName( - params.ctx, - params.p.txn, - v.dbName, - tree.DatabaseLookupFlags{ - Required: true, AvoidLeased: params.p.skipDescriptorCache, - }, - ) + db, err := params.p.byNameGetterBuilder().Get().Database(params.ctx, v.dbName) if err != nil { return err } diff --git a/pkg/sql/zone_config.go b/pkg/sql/zone_config.go index 6a05d331eca2..dfdb2c649b4c 100644 --- a/pkg/sql/zone_config.go +++ b/pkg/sql/zone_config.go @@ -384,8 +384,11 @@ func (p *planner) resolveTableForZone( } else if zs.TargetsTable() { var immutRes catalog.TableDescriptor p.runWithOptions(resolveFlags{skipCache: true}, func() { - flags := tree.ObjectLookupFlagsWithRequiredTableKind(tree.ResolveAnyTableKind) - flags.IncludeOffline = true + flags := tree.ObjectLookupFlags{ + Required: true, + IncludeOffline: true, + DesiredObjectKind: tree.TableObject, + } _, immutRes, err = resolver.ResolveExistingTableObject(ctx, p, &zs.TableOrIndex.Table, flags) }) if err != nil { diff --git a/pkg/upgrade/upgrades/fix_userfile_descriptor_corruption.go b/pkg/upgrade/upgrades/fix_userfile_descriptor_corruption.go index f83fc5dafee0..827bda23f3c5 100644 --- a/pkg/upgrade/upgrades/fix_userfile_descriptor_corruption.go +++ b/pkg/upgrade/upgrades/fix_userfile_descriptor_corruption.go @@ -58,12 +58,7 @@ func fixInvalidObjectsThatLookLikeBadUserfileConstraint( errString := string(tree.MustBeDString(row[4])) if veryLikelyKnownUserfileBreakage(ctx, txn, descriptors, tableID, errString) { log.Infof(ctx, "attempting to fix invalid table descriptor %d assuming it is a userfile-related table", tableID) - mutTableDesc, err := descriptors.GetMutableTableByID(ctx, txn, tableID, tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - IncludeOffline: true, - IncludeDropped: true, - }, - }) + mutTableDesc, err := descriptors.MutableByID(txn).Table(ctx, tableID) if err != nil { return err } @@ -93,12 +88,7 @@ func veryLikelyKnownUserfileBreakage( return false } - tableDesc, err := descriptors.GetImmutableTableByID(ctx, txn, id, tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - IncludeOffline: true, - IncludeDropped: true, - }, - }) + tableDesc, err := descriptors.ByIDWithLeased(txn).Get().Table(ctx, id) if err != nil { return false } @@ -140,17 +130,7 @@ func mutationsLookLikeuserfilePayloadCorruption( mutation := tableDesc.AllMutations()[0] if mutation.Adding() && mutation.DeleteOnly() { if fkConstraint := mutation.AsForeignKey(); fkConstraint != nil { - targetTableDesc, err := descriptors.GetImmutableTableByID( - ctx, - txn, - fkConstraint.GetReferencedTableID(), - tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - IncludeOffline: true, - IncludeDropped: true, - }, - }, - ) + targetTableDesc, err := descriptors.ByIDWithLeased(txn).Get().Table(ctx, fkConstraint.GetReferencedTableID()) if err != nil { return false } diff --git a/pkg/upgrade/upgrades/helpers_test.go b/pkg/upgrade/upgrades/helpers_test.go index e3cc524f8562..4e9aecee3164 100644 --- a/pkg/upgrade/upgrades/helpers_test.go +++ b/pkg/upgrade/upgrades/helpers_test.go @@ -22,7 +22,6 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descs" "github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc" - "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/testutils/serverutils" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -79,7 +78,7 @@ func InjectLegacyTable( ctx context.Context, txn *kv.Txn, descriptors *descs.Collection, ) error { id := table.GetID() - tab, err := descriptors.GetMutableTableByID(ctx, txn, id, tree.ObjectLookupFlagsWithRequired()) + tab, err := descriptors.MutableByID(txn).Table(ctx, id) if err != nil { return err } @@ -143,12 +142,7 @@ func GetTable( err := s.InternalExecutorFactory().(descs.TxnManager).DescsTxn(ctx, s.DB(), func( ctx context.Context, txn *kv.Txn, descriptors *descs.Collection, ) (err error) { - table, err = descriptors.GetImmutableTableByID(ctx, txn, tableID, tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - AvoidLeased: true, - Required: true, - }, - }) + table, err = descriptors.ByID(txn).WithoutNonPublic().Get().Table(ctx, tableID) return err }) require.NoError(t, err) diff --git a/pkg/upgrade/upgrades/schema_changes.go b/pkg/upgrade/upgrades/schema_changes.go index 91bda5d9d480..7d5dcd70913f 100644 --- a/pkg/upgrade/upgrades/schema_changes.go +++ b/pkg/upgrade/upgrades/schema_changes.go @@ -144,12 +144,7 @@ func readTableDescriptor( if err := d.InternalExecutorFactory.DescsTxn(ctx, d.DB, func( ctx context.Context, txn *kv.Txn, descriptors *descs.Collection, ) (err error) { - t, err = descriptors.GetImmutableTableByID(ctx, txn, tableID, tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - AvoidLeased: true, - Required: true, - }, - }) + t, err = descriptors.ByID(txn).WithoutNonPublic().Get().Table(ctx, tableID) return err }); err != nil { return nil, err diff --git a/pkg/upgrade/upgrades/schema_changes_external_test.go b/pkg/upgrade/upgrades/schema_changes_external_test.go index c530b75e5299..8fe0e5db9634 100644 --- a/pkg/upgrade/upgrades/schema_changes_external_test.go +++ b/pkg/upgrade/upgrades/schema_changes_external_test.go @@ -385,12 +385,7 @@ func testMigrationWithFailures( ctx context.Context, txn *kv.Txn, descriptors *descs.Collection, ) (err error) { tn := tree.MakeTableNameWithSchema("test", "public", "test_table") - _, desc, err = descriptors.GetImmutableTableByName(ctx, txn, &tn, tree.ObjectLookupFlags{ - CommonLookupFlags: tree.CommonLookupFlags{ - Required: true, - AvoidLeased: true, - }, - }) + _, desc, err = descs.PrefixAndTable(ctx, descriptors.ByName(txn).Get(), &tn) return err })) tdb.Exec(t, "DROP TABLE test.test_table") diff --git a/pkg/upgrade/upgrades/update_invalid_column_ids_in_sequence_back_references.go b/pkg/upgrade/upgrades/update_invalid_column_ids_in_sequence_back_references.go index e38b8033f405..dcbdb8098988 100644 --- a/pkg/upgrade/upgrades/update_invalid_column_ids_in_sequence_back_references.go +++ b/pkg/upgrade/upgrades/update_invalid_column_ids_in_sequence_back_references.go @@ -19,7 +19,6 @@ import ( "github.com/cockroachdb/cockroach/pkg/kv" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descs" - "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sqlutil" "github.com/cockroachdb/cockroach/pkg/upgrade" "github.com/cockroachdb/cockroach/pkg/util/log" @@ -95,7 +94,7 @@ func maybeUpdateInvalidColumnIdsInSequenceBackReferences( ctx context.Context, txn *kv.Txn, idToUpgrade descpb.ID, descriptors *descs.Collection, ) (hasUpgraded bool, err error) { // Get the sequence descriptor that we are going to upgrade. - seqDesc, err := descriptors.GetMutableTableByID(ctx, txn, idToUpgrade, tree.ObjectLookupFlagsWithRequired()) + seqDesc, err := descriptors.MutableByID(txn).Table(ctx, idToUpgrade) if err != nil { return false, err } @@ -108,7 +107,7 @@ func maybeUpdateInvalidColumnIdsInSequenceBackReferences( // `ref.ColumnIDs` if the actual value is not equal to the // expected value. expectedColumnIDsInRef := make([]descpb.ColumnID, 0) - tableDesc, err := descriptors.GetMutableTableByID(ctx, txn, ref.ID, tree.ObjectLookupFlagsWithRequired()) + tableDesc, err := descriptors.MutableByID(txn).Table(ctx, ref.ID) if err != nil { return false, err } diff --git a/pkg/upgrade/upgrades/upgrade_sequence_to_be_referenced_by_ID.go b/pkg/upgrade/upgrades/upgrade_sequence_to_be_referenced_by_ID.go index 1c9810fef176..0e42c5a79295 100644 --- a/pkg/upgrade/upgrades/upgrade_sequence_to_be_referenced_by_ID.go +++ b/pkg/upgrade/upgrades/upgrade_sequence_to_be_referenced_by_ID.go @@ -148,14 +148,13 @@ func upgradeSetUpForTableOrView( idToUpgrade descpb.ID, ) (*tabledesc.Mutable, resolver.SchemaResolver, func(), error) { // Get the table descriptor that we are going to upgrade. - tableDesc, err := descriptors.GetMutableTableByID(ctx, txn, idToUpgrade, tree.ObjectLookupFlagsWithRequired()) + tableDesc, err := descriptors.MutableByID(txn).Table(ctx, idToUpgrade) if err != nil { return nil, nil, nil, err } // Get the database of the table to pass to the planner constructor. - _, dbDesc, err := descriptors.GetImmutableDatabaseByID( - ctx, txn, tableDesc.GetParentID(), tree.DatabaseLookupFlags{Required: true}) + dbDesc, err := descriptors.ByIDWithLeased(txn).WithoutNonPublic().Get().Database(ctx, tableDesc.GetParentID()) if err != nil { return nil, nil, nil, err }