From 4bd76b671e1ddeb852141129838c1d780b32de5b Mon Sep 17 00:00:00 2001 From: Uzair Ahmad Date: Tue, 21 May 2024 09:47:08 -0400 Subject: [PATCH] sql: add a default AOST option to CREATE STATISTICS 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: #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. --- pkg/sql/opt/optbuilder/misc_statements.go | 5 +++++ pkg/sql/opt/optbuilder/testdata/misc_statements | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/pkg/sql/opt/optbuilder/misc_statements.go b/pkg/sql/opt/optbuilder/misc_statements.go index fb05ab36d218..12839528b0b9 100644 --- a/pkg/sql/opt/optbuilder/misc_statements.go +++ b/pkg/sql/opt/optbuilder/misc_statements.go @@ -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, }) diff --git a/pkg/sql/opt/optbuilder/testdata/misc_statements b/pkg/sql/opt/optbuilder/testdata/misc_statements index c704597932b2..7cb3d78ebb2b 100644 --- a/pkg/sql/opt/optbuilder/testdata/misc_statements +++ b/pkg/sql/opt/optbuilder/testdata/misc_statements @@ -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