Skip to content

Commit

Permalink
opt: avoid looking up MultiregionConfig for non-multiregion tables
Browse files Browse the repository at this point in the history
This avoids expensive lookups of the regions in a table's parent
database for non-multiregion tables.

Release note: None
  • Loading branch information
Mark Sirek committed Nov 8, 2022
1 parent 1b4aa43 commit 030204c
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 5 deletions.
4 changes: 4 additions & 0 deletions pkg/sql/opt/cat/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ type Table interface {
// BY ROW.
IsRegionalByRow() bool

// IsMultiregion returns true if the table is a Multiregion table, defined
// with one of the LOCALITY clauses.
IsMultiregion() bool

// HomeRegionColName returns the name of the crdb_internal_region column name
// specifying the home region of each row in the table, if this table is a
// REGIONAL BY ROW TABLE, otherwise "", false is returned.
Expand Down
5 changes: 5 additions & 0 deletions pkg/sql/opt/exec/explain/plan_gist_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,11 @@ func (u *unknownTable) IsRegionalByRow() bool {
return false
}

// IsMultiregion is part of the cat.Table interface.
func (u *unknownTable) IsMultiregion() bool {
return false
}

// HomeRegionColName is part of the cat.Table interface.
func (u *unknownTable) HomeRegionColName() (colName string, ok bool) {
return "", false
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/opt/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ func TestTableMeta_GetRegionsInDatabase(t *testing.T) {
tab.DatabaseID = 1 // must be non-zero to trigger the region lookup
a := md.AddTable(tab, tn)
tabMeta := md.TableMeta(a)
tab.SetMultiRegion(true)

p := &fakeGetMultiregionConfigPlanner{}
// Call the function once, make sure our planner method gets invoked.
Expand Down
19 changes: 14 additions & 5 deletions pkg/sql/opt/table_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,7 @@ func (tm *TableMeta) GetRegionsInDatabase(
)
}
}()

if dbID == 0 {
if dbID == 0 || !tm.Table.IsMultiregion() {
return nil /* regionNames */, false
}
regionConfig, ok := planner.GetMultiregionConfig(ctx, dbID)
Expand Down Expand Up @@ -502,11 +501,21 @@ func (tm *TableMeta) GetDatabaseSurvivalGoal(
return multiregionConfig.SurvivalGoal(), true
}
dbID := tm.Table.GetDatabaseID()
defer func() {
if !ok {
tm.SetTableAnnotation(
regionConfigAnnID,
// Use a nil pointer to a RegionConfig, which is distinct from the
// untyped nil and will be detected in the type assertion above.
(*multiregion.RegionConfig)(nil),
)
}
}()
if dbID == 0 || !tm.Table.IsMultiregion() {
return descpb.SurvivalGoal_ZONE_FAILURE /* regionNames */, false
}
regionConfig, ok := planner.GetMultiregionConfig(ctx, dbID)
if !ok {
// Use a nil pointer to a RegionConfig, which is distinct from the
// untyped nil and will be detected in the type assertion above.
tm.SetTableAnnotation(regionConfigAnnID, (*multiregion.RegionConfig)(nil))
return descpb.SurvivalGoal_ZONE_FAILURE /* survivalGoal */, false
}
multiregionConfig, _ = regionConfig.(*multiregion.RegionConfig)
Expand Down
14 changes: 14 additions & 0 deletions pkg/sql/opt/testutils/testcat/test_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,8 @@ type Table struct {
// partitionBy is the partitioning clause that corresponds to the primary
// index. Used to initialize the partitioning for the primary index.
partitionBy *tree.PartitionBy

multiRegion bool
}

var _ cat.Table = &Table{}
Expand All @@ -703,6 +705,13 @@ func (tt *Table) String() string {
return tp.String()
}

// SetMultiRegion can make a table in the test catalog appear to be a
// multiregion table, in that it can cause cat.Table.IsMultiregion() to return
// true after SetMultiRegion(true) has been called.
func (tt *Table) SetMultiRegion(val bool) {
tt.multiRegion = val
}

// ID is part of the cat.DataSource interface.
func (tt *Table) ID() cat.StableID {
return tt.TabID
Expand Down Expand Up @@ -863,6 +872,11 @@ func (tt *Table) IsRegionalByRow() bool {
return false
}

// IsMultiregion is part of the cat.Table interface.
func (tt *Table) IsMultiregion() bool {
return tt.multiRegion
}

// HomeRegionColName is part of the cat.Table interface.
func (tt *Table) HomeRegionColName() (colName string, ok bool) {
return "", false
Expand Down
11 changes: 11 additions & 0 deletions pkg/sql/opt_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -1306,6 +1306,12 @@ func (ot *optTable) IsRegionalByRow() bool {
return localityConfig.GetRegionalByRow() != nil
}

// IsMultiregion is part of the cat.Table interface.
func (ot *optTable) IsMultiregion() bool {
localityConfig := ot.desc.GetLocalityConfig()
return localityConfig != nil
}

// HomeRegionColName is part of the cat.Table interface.
func (ot *optTable) HomeRegionColName() (colName string, ok bool) {
localityConfig := ot.desc.GetLocalityConfig()
Expand Down Expand Up @@ -2285,6 +2291,11 @@ func (ot *optVirtualTable) IsRegionalByRow() bool {
return false
}

// IsMultiregion is part of the cat.Table interface.
func (ot *optVirtualTable) IsMultiregion() bool {
return false
}

// HomeRegionColName is part of the cat.Table interface.
func (ot *optVirtualTable) HomeRegionColName() (colName string, ok bool) {
return "", false
Expand Down

0 comments on commit 030204c

Please sign in to comment.