Skip to content
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

Merged

Conversation

Uzair5162
Copy link
Contributor

@Uzair5162 Uzair5162 commented May 21, 2024

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.

@Uzair5162 Uzair5162 added the first-pr Use to mark the first PR sent by a contributor / team member. Reviewers should be mindful of this. label May 21, 2024
@Uzair5162 Uzair5162 requested review from DrewKimball and a team May 21, 2024 14:34
@Uzair5162 Uzair5162 requested a review from a team as a code owner May 21, 2024 14:34
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Collaborator

@michae2 michae2 left a 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: :shipit: 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:

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)

@mgartner
Copy link
Collaborator

Maybe it would be better to add a default AOST if one isn't provided?

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.

Copy link
Collaborator

@michae2 michae2 left a 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: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @DrewKimball and @Uzair5162)

@Uzair5162 Uzair5162 force-pushed the enforce-aost-in-create-stats-gh-72719 branch 3 times, most recently from dd0366b to 4bd76b6 Compare May 22, 2024 15:47
@Uzair5162 Uzair5162 changed the title sql: prohibit CREATE STATISTICS without specifying AOST option sql: add a default AOST option to CREATE STATISTICS May 22, 2024
Copy link
Contributor Author

@Uzair5162 Uzair5162 left a 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: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @DrewKimball)

@Uzair5162 Uzair5162 force-pushed the enforce-aost-in-create-stats-gh-72719 branch from 4bd76b6 to 696d48d Compare May 22, 2024 18:24
Copy link
Collaborator

@DrewKimball DrewKimball left a 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: :shipit: 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:

  1. Make sure that a user-specified AOST doesn't get over-ruled.
  2. Make sure this works even if a non-AOST option is specified (like THROTTLING 0.3)

@Uzair5162 Uzair5162 force-pushed the enforce-aost-in-create-stats-gh-72719 branch from 696d48d to 937da4c Compare May 22, 2024 18:59
Copy link
Contributor Author

@Uzair5162 Uzair5162 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @DrewKimball)


-- commits line 5 at r2:

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:

  1. Make sure that a user-specified AOST doesn't get over-ruled.
  2. Make sure this works even if a non-AOST option is specified (like THROTTLING 0.3)

Done.

Copy link
Collaborator

@DrewKimball DrewKimball left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm: Nice!

Reviewed 1 of 1 files at r4, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @Uzair5162)

Copy link
Collaborator

@michae2 michae2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm: Nice work!

Reviewed 1 of 4 files at r2, 2 of 2 files at r3, 1 of 1 files at r4, all commit messages.
Reviewable status: :shipit: 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.
@Uzair5162 Uzair5162 force-pushed the enforce-aost-in-create-stats-gh-72719 branch from 937da4c to 805b682 Compare May 22, 2024 20:27
Copy link
Contributor Author

@Uzair5162 Uzair5162 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: 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:

convErr = errors.Errorf("interval value %v too small, absolute value must be >= %v", d, time.Microsecond)

@Uzair5162
Copy link
Contributor Author

bors r+

@craig craig bot merged commit 565fa54 into cockroachdb:master May 23, 2024
22 checks passed
@mgartner
Copy link
Collaborator

mgartner commented Jun 4, 2024

Great work @Uzair5162!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
first-pr Use to mark the first PR sent by a contributor / team member. Reviewers should be mindful of this.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sql: consider prohibiting CREATE STATISTICS without specifying AOST option
5 participants