forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stats: table-level setting to turn auto stats collection on/off
Fixes cockroachdb#40989 Previously, there was no way to enable or disable automatic statistics collection at the table level. It could only be turned on or off via the `sql.stats.automatic_collection.enabled` cluster setting. This was inadequate because statistics collection can be expensive for large tables, and it would be desirable to defer collection until after data is finished loading, or in off hours. Also, small tables which are frequently updated may trigger statistics collection leading to unnecessary overhead and/or unpredictable query plan changes. To address this, this patch adds support for setting of the following cluster settings at the table level: ``` sql.stats.automatic_collection.enabled sql.stats.automatic_collection.fraction_stale_rows sql.stats.automatic_collection.min_stale_rows ``` for example: ``` ALTER TABLE t1 SET ("sql.stats.automatic_collection.enabled" = true); ALTER TABLE t1 SET ("sql.stats.automatic_collection.fraction_stale_rows" = 0.1, "sql.stats.automatic_collection.min_stale_rows" = 2000); ``` The table-level setting takes precedence over the cluster setting. Release justification: Low risk fix for missing fine-grained control over automatic statistics collection. Release note (sql change): Automatic statistics collection can now be enabled or disabled for individual tables, taking precedence over the cluster setting, for example: ``` ALTER TABLE t1 SET ("sql.stats.automatic_collection.enabled" = true); ALTER TABLE t1 SET ("sql.stats.automatic_collection.enabled" = false); ALTER TABLE t1 RESET ("sql.stats.automatic_collection.enabled"); ``` RESET removes the setting value entirely, in which case the cluster setting of the same name is in effect for the table. Cluster settings `sql.stats.automatic_collection.fraction_stale_rows` and `sql.stats.automatic_collection.min_stale_rows` can now also be set at the table level, either at table creation time, or later, independent of whether auto stats is enabled: ``` ALTER TABLE t1 SET ("sql.stats.automatic_collection.fraction_stale_rows" = 0.1, "sql.stats.automatic_collection.min_stale_rows" = 2000); CREATE TABLE t1 (a INT, b INT) WITH ("sql.stats.automatic_collection.enabled" = true, "sql.stats.automatic_collection.min_stale_rows" = 1000000, "sql.stats.automatic_collection.fraction_stale_rows" = 0.05 ); ``` The current table-level cluster settings, along with storage parameters, is shown in the `WITH` clause output of `SHOW CREATE TABLE`. Note, any row mutations which have occurred within a couple minutes of disabling auto stats collection via `ALTER TABLE ... SET` may trigger stats collection, though DML submitted after the setting change will not.
- Loading branch information
Mark Sirek
committed
Mar 28, 2022
1 parent
cb10bfe
commit e6f508f
Showing
19 changed files
with
1,396 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// 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 catpb | ||
|
||
import "github.com/cockroachdb/cockroach/pkg/settings/cluster" | ||
|
||
// AutoStatsCollectionEnabled indicates if automatic statistics collection is | ||
// explicitly enabled or disabled. | ||
func (cs *ClusterSettingsForTable) AutoStatsCollectionEnabled() cluster.BoolSetting { | ||
if cs.SqlStatsAutomaticCollectionEnabled == nil { | ||
return cluster.NotSet | ||
} | ||
if *cs.SqlStatsAutomaticCollectionEnabled { | ||
return cluster.True | ||
} | ||
return cluster.False | ||
} | ||
|
||
// AutoStatsMinStaleRows indicates the setting of | ||
// sql.stats.automatic_collection.min_stale_rows in ClusterSettingsForTable. | ||
// If ok is true, then the minStaleRows value is valid, otherwise this has not | ||
// been set. | ||
func (cs *ClusterSettingsForTable) AutoStatsMinStaleRows() (minStaleRows int64, ok bool) { | ||
if cs.SqlStatsAutomaticCollectionMinStaleRows == nil { | ||
return 0, false | ||
} | ||
return *cs.SqlStatsAutomaticCollectionMinStaleRows, true | ||
} | ||
|
||
// AutoStatsFractionStaleRows indicates the setting of | ||
// sql.stats.automatic_collection.fraction_stale_rows in | ||
// ClusterSettingsForTable. If ok is true, then the fractionStaleRows value is | ||
// valid, otherwise this has not been set. | ||
func (cs *ClusterSettingsForTable) AutoStatsFractionStaleRows() ( | ||
fractionStaleRows float64, | ||
ok bool, | ||
) { | ||
if cs.SqlStatsAutomaticCollectionFractionStaleRows == nil { | ||
return 0, false | ||
} | ||
return *cs.SqlStatsAutomaticCollectionFractionStaleRows, true | ||
} | ||
|
||
// NoAutoStatsSettingsOverrides is true if no auto stats related cluster | ||
// settings are present in these ClusterSettingsForTable. | ||
func (cs *ClusterSettingsForTable) NoAutoStatsSettingsOverrides() bool { | ||
if cs == nil { | ||
return true | ||
} | ||
if cs.SqlStatsAutomaticCollectionEnabled != nil || | ||
cs.SqlStatsAutomaticCollectionMinStaleRows != nil || | ||
cs.SqlStatsAutomaticCollectionFractionStaleRows != nil { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
// AutoStatsSettingsEqual returns true if all auto stats related | ||
// ClusterSettingsForTable in this structure match those in `other`. An unset | ||
// value must also be unset in the other's settings to be equal. | ||
func (cs *ClusterSettingsForTable) AutoStatsSettingsEqual(other *ClusterSettingsForTable) bool { | ||
otherHasNoAutoStatsSettings := other.NoAutoStatsSettingsOverrides() | ||
if cs.NoAutoStatsSettingsOverrides() { | ||
return otherHasNoAutoStatsSettings | ||
} | ||
if otherHasNoAutoStatsSettings { | ||
return false | ||
} | ||
if cs.AutoStatsCollectionEnabled() != other.AutoStatsCollectionEnabled() { | ||
return false | ||
} | ||
minStaleRows := cs.SqlStatsAutomaticCollectionMinStaleRows | ||
otherMinStaleRows := other.SqlStatsAutomaticCollectionMinStaleRows | ||
if minStaleRows == nil { | ||
if otherMinStaleRows != nil { | ||
return false | ||
} | ||
} | ||
if otherMinStaleRows == nil { | ||
if minStaleRows != nil { | ||
return false | ||
} | ||
} | ||
if *minStaleRows != *otherMinStaleRows { | ||
return false | ||
} | ||
|
||
fractionStaleRows := cs.SqlStatsAutomaticCollectionFractionStaleRows | ||
otherFractionStaleRows := other.SqlStatsAutomaticCollectionFractionStaleRows | ||
if fractionStaleRows == nil { | ||
if otherFractionStaleRows != nil { | ||
return false | ||
} | ||
} | ||
if otherFractionStaleRows == nil { | ||
if fractionStaleRows != nil { | ||
return false | ||
} | ||
} | ||
if *fractionStaleRows != *otherFractionStaleRows { | ||
return false | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.