-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sql: add a default AOST option to CREATE STATISTICS #124488
sql: add a default AOST option to CREATE STATISTICS #124488
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice PR! It looks great!
This is a straightforward change, and the motivation makes sense to me. But I wonder if anyone has written a script that is calling CREATE STATISTICS
manually which will be broken by this new requirement (as our tests were)? Maybe it would be better to add a default AOST if one isn't provided?
Reviewable status: complete! 0 of 0 LGTMs obtained (waiting on @DrewKimball and @Uzair5162)
pkg/sql/create_stats.go
line 234 at r1 (raw file):
if n.Options.AsOf.Expr == nil { return nil, pgerror.New(pgcode.Syntax,
Instead of returning an error, what would you think of adding a default AOST of -1μs, like we do for ANALYZE
?
Here's where that happens:
cockroach/pkg/sql/opt/optbuilder/builder.go
Lines 429 to 444 in d146ecf
case *tree.CreateStats: | |
return b.buildCreateStatistics(stmt, inScope) | |
case *tree.Analyze: | |
// ANALYZE is syntactic sugar for CREATE STATISTICS. We add AS OF SYSTEM | |
// TIME '-0.001ms' to trigger use of inconsistent scans. This prevents | |
// GC TTL errors during ANALYZE. See the sql.stats.max_timestamp_age | |
// setting. | |
return b.buildCreateStatistics(&tree.CreateStats{ | |
Table: stmt.Table, | |
Options: tree.CreateStatsOptions{ | |
AsOf: tree.AsOfClause{ | |
Expr: tree.NewStrVal("-0.001ms"), | |
}, | |
}, | |
}, inScope) |
I like that idea. Is there any reasonable use case for running CREATE STATISTICS without AOST? If not, I think we should do this. If there is some need, maybe we need some setting to allow it for certain cases. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any reasonable use case for running CREATE STATISTICS without AOST?
Not that I can think of that wouldn't also work for -1μs.
Reviewable status: complete! 0 of 0 LGTMs obtained (waiting on @DrewKimball and @Uzair5162)
dd0366b
to
4bd76b6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion! I've changed it to apply a default AOST instead.
Reviewable status: complete! 0 of 0 LGTMs obtained (waiting on @DrewKimball)
4bd76b6
to
696d48d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great work!
Reviewed 4 of 4 files at r2, 2 of 2 files at r3, all commit messages.
Reviewable status: complete! 0 of 0 LGTMs obtained (waiting on @Uzair5162)
-- commits
line 5 at r2:
[nit] Implements a default AOST of
-> This commit adds a default AOST option of
pkg/sql/opt/optbuilder/testdata/misc_statements
line 186 at r3 (raw file):
---- create-statistics └── CREATE STATISTICS foo FROM ab WITH OPTIONS AS OF SYSTEM TIME '-1us'
A few other tests might be a good idea here:
- Make sure that a user-specified AOST doesn't get over-ruled.
- Make sure this works even if a non-AOST option is specified (like
THROTTLING 0.3
)
696d48d
to
937da4c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: complete! 0 of 0 LGTMs obtained (waiting on @DrewKimball)
Previously, DrewKimball (Drew Kimball) wrote…
[nit]
Implements a default AOST of
->This commit adds a default AOST option of
Done.
pkg/sql/opt/optbuilder/testdata/misc_statements
line 186 at r3 (raw file):
Previously, DrewKimball (Drew Kimball) wrote…
A few other tests might be a good idea here:
- Make sure that a user-specified AOST doesn't get over-ruled.
- Make sure this works even if a non-AOST option is specified (like
THROTTLING 0.3
)
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 1 of 1 files at r4, all commit messages.
Reviewable status: complete! 1 of 0 LGTMs obtained (waiting on @Uzair5162)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 1 of 4 files at r2, 2 of 2 files at r3, 1 of 1 files at r4, all commit messages.
Reviewable status: complete! 2 of 0 LGTMs obtained (waiting on @DrewKimball and @Uzair5162)
pkg/sql/opt/optbuilder/misc_statements.go
line 152 at r4 (raw file):
outScope = inScope.push() if n.Options.AsOf.Expr == nil {
nit: it might be nice to add a short comment here explaining why the AOST is helpful. (Totally ok to copy the comment from ANALYZE.)
pkg/sql/opt/optbuilder/testdata/misc_statements
line 186 at r3 (raw file):
Previously, Uzair5162 (Uzair Ahmad) wrote…
Done.
I'm curious, does AS OF SYSTEM TIME '0s'
work? That might be a good testcase as well.
Using CREATE STATISTICS without an AOST option results in a regular scan which could contend with concurrent transactions. This commit adds a default AOST option 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.
937da4c
to
805b682
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: complete! 0 of 0 LGTMs obtained (and 2 stale) (waiting on @DrewKimball and @michae2)
pkg/sql/opt/optbuilder/testdata/misc_statements
line 186 at r3 (raw file):
Previously, michae2 (Michael Erickson) wrote…
I'm curious, does
AS OF SYSTEM TIME '0s'
work? That might be a good testcase as well.
We require the AOST interval to be >= 1µs:
cockroach/pkg/sql/sem/asof/as_of.go
Line 266 in d146ecf
convErr = errors.Errorf("interval value %v too small, absolute value must be >= %v", d, time.Microsecond) |
bors r+ |
Great work @Uzair5162! |
Using CREATE STATISTICS without an AOST option results in a regular scan which could contend with concurrent transactions. This commit adds a default AOST option 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.