Skip to content

Commit

Permalink
sql: add a default AOST option to CREATE STATISTICS
Browse files Browse the repository at this point in the history
Using CREATE STATISTICS without an AOST option results in a regular scan
which could contend with concurrent transactions. Implements a default
AOST of -1us to the CREATE STATISTICS command to avoid this.

Fixes: cockroachdb#72719

Release note (sql change): using the CREATE STATISTICS command without
the AS OF SYSTEM TIME option could contend with concurrent transactions
and cost performance. Running CREATE STATISTICS without specifying AS OF
SYSTEM TIME now uses a default of -1us.
  • Loading branch information
Uzair5162 committed May 22, 2024
1 parent 3a3c1d8 commit 4bd76b6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
5 changes: 5 additions & 0 deletions pkg/sql/opt/optbuilder/misc_statements.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ func (b *Builder) buildControlSchedules(

func (b *Builder) buildCreateStatistics(n *tree.CreateStats, inScope *scope) (outScope *scope) {
outScope = inScope.push()

if n.Options.AsOf.Expr == nil {
n.Options.AsOf.Expr = tree.NewStrVal("-1us")
}

outScope.expr = b.factory.ConstructCreateStatistics(&memo.CreateStatisticsPrivate{
Syntax: n,
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/opt/optbuilder/testdata/misc_statements
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ build
CREATE STATISTICS foo FROM ab
----
create-statistics
└── CREATE STATISTICS foo FROM ab
└── CREATE STATISTICS foo FROM ab WITH OPTIONS AS OF SYSTEM TIME '-1us'

build
ANALYZE ab
Expand Down

0 comments on commit 4bd76b6

Please sign in to comment.