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

opt: don't apply huge cost for full scans of virtual tables #123780

Merged
merged 1 commit into from
May 9, 2024

Conversation

yuzefovich
Copy link
Member

@yuzefovich yuzefovich commented May 7, 2024

This commit fixes - what I believe is - an oversight where we applied the huge cost to the plans with full scans of virtual tables when disallow_full_table_scans variable is set. The intent behind this penalty is to make plans with full scans prohibitively expensive so that other plans (without full scans) would be preferred, and if there is no other plan, then the query would error out. Implementation of this approach was incomplete for virtual tables: namely, we'd apply the cost penalty, but then we would not actually error out the query. Additionally, given that we don't have stats on virtual tables, we would apply the cost penalty to all full scans of virtual tables.

As a result, we would either pick a plan without the full scan (that might actually be worse) or would allow the plan with the full scan to get executed still. We would also favor plans with fewer full scans of virtual tables (which might also be worse than had we not applied the cost penalty). This commit adjusts the code to not apply the cost penalty to full scans of virtual tables.

This change is gated based on the new session variable optimizer_apply_full_scan_penalty_to_virtual_tables which defaults to false on master. We can discuss on the backport PRs whether it should be default to true there to require users to opt in for this PR to have an effect.

Fixes: #122708.

Release note: None

Copy link

blathers-crl bot commented May 7, 2024

It looks like your PR touches production code but doesn't add or edit any test code. Did you consider adding tests to your PR?

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@yuzefovich yuzefovich added backport-23.1.x Flags PRs that need to be backported to 23.1 backport-23.2.x Flags PRs that need to be backported to 23.2. backport-24.1.x Flags PRs that need to be backported to 24.1. labels May 7, 2024
@yuzefovich yuzefovich force-pushed the full-scan-vt branch 3 times, most recently from b7cd87c to e44bc50 Compare May 7, 2024 23:25
This commit fixes - what I believe is - an oversight where we applied
the huge cost to the plans with full scans of virtual tables when
`disallow_full_table_scans` variable is set. The intent behind this
penalty is to make plans with full scans prohibitively expensive so
that other plans (without full scans) would be preferred, and if
there is no other plan, then the query would error out. Implementation
of this approach was incomplete for virtual tables: namely, we'd apply
the cost penalty, but then we would not actually error out the query.
Additionally, given that we don't have stats on virtual tables, we
would apply the cost penalty to all full scans of virtual tables.

As a result, we would either pick a plan without the full scan (that
might actually be worse) or would allow the plan with the full scan to
get executed still. We would also favor plans with fewer full scans of
virtual tables (which might also be worse than had we not applied the
cost penalty). This commit adjusts the code to not apply the cost
penalty to full scans of virtual tables.

This change is gated based on the new session variable
`optimizer_apply_full_scan_penalty_to_virtual_tables` which defaults to
`false` on master. We can discuss on the backport PRs whether it should
be default to `true` there to require users to opt in for this PR to
have an effect.

Release note: None
@yuzefovich yuzefovich marked this pull request as ready for review May 7, 2024 23:29
@yuzefovich yuzefovich requested a review from a team as a code owner May 7, 2024 23:29
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: Thanks for fixing this!

Reviewed 17 of 17 files at r1, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @michae2 and @yuzefovich)


-- commits line 28 at r1:
[nit] Should we add a release note since the issue this is fixing has an O-support label?

@michae2
Copy link
Collaborator

michae2 commented May 8, 2024

I will look today.

Copy link
Member Author

@yuzefovich yuzefovich 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! 1 of 0 LGTMs obtained (waiting on @DrewKimball and @michae2)


-- commits line 28 at r1:

Previously, DrewKimball (Drew Kimball) wrote…

[nit] Should we add a release note since the issue this is fixing has an O-support label?

My thinking was that the default value for disallow_full_table_scans is false, so this could be considered a bug fix to a non-default configuration, in which I thought we generally don't include the release notes. (I did see that we did include some release notes in the past for this feature though.) For the single customer that ran into this we'll be communicating directly, so I don't think they care whether we include it or not.

I can add a release note if we think it's worth it.

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.

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @michae2 and @yuzefovich)


-- commits line 28 at r1:

I don't think they care whether we include it or not.

If that's the case, I'm fine with just leaving it out.

@michae2 michae2 requested a review from rytaft May 8, 2024 16:57
Copy link
Collaborator

@rytaft rytaft left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewable status: :shipit: complete! 2 of 0 LGTMs obtained (waiting on @michae2 and @yuzefovich)

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.

foo

Reviewed 1 of 17 files at r1.
Reviewable status: :shipit: complete! 2 of 0 LGTMs obtained (waiting on @yuzefovich)


pkg/sql/opt/xform/coster.go line 791 at r1 (raw file):

  	// TODO(#123783): once we start rejecting plans with full scans of>
  	// virtual tables, we should apply the cost penalty here.

Do we want to reject plans with full scans of virtual tables? I thought according to #122708 we decided to not apply disallow_full_table_scans to virtual tables? And it seems like this PR also fixes that issue by removing hugeCost?

I find the purpose of optimizer_apply_full_scan_penalty_to_virtual_tables a little muddy: it brings back the old behavior, but the old behavior was not correct as you point out in #123783. Would it make more sense to add a disallow_full_virtual_table_scans setting which, when enabled has both the hugeCost and the ContainsLargeFullTableScan flag, fixing 123783, and when disabled has neither, fixing 122708?

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.

sorry, ignore the foo 🤦

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

@yuzefovich yuzefovich requested a review from michae2 May 9, 2024 02:23
Copy link
Member Author

@yuzefovich yuzefovich 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! 2 of 0 LGTMs obtained (waiting on @michae2)


pkg/sql/opt/xform/coster.go line 791 at r1 (raw file):

Do we want to reject plans with full scans of virtual tables?

This is the question that I don't know the answer to. I find this problem quite confusing - I believe that originally "disallow full table scans" feature was designed with user tables in mind, so we probably didn't consider how it should behave for virtual tables. The goal is for DB infra team to force their app developers think better about schema / query design in order to not introduce expensive queries (to protect stability of the cluster), and we use "full scan" or "large scan" as a proxy for being expensive. However, for virtual tables our users have no control over the schema; true, in some cases they could rewrite the queries to avoid full scans of vtables, but I'd guess it's usually not possible - this is due to CRDB missing virtual indexes (in case it is possible to implement those). Do we want to error out such queries without having any workaround? I'm not sure.

This thinking is why I decided to file (rather than fix it right away) #123783 so that we could discuss in case we want to have a way to prohibit full scans of vtables. The current approach allows us to fix the issue for the customer with low risk and have a backportable solution. As a follow-up, I plan to remove this session variable altogether on master (so that this behavior is unconditional), and then it'll be up to us to fix #123783 (where a new session variable would be warranted) separately, and we'll have the session var available on older releases (where I think we should consider defaulting to enabling this patch, and the session var would provide us an escape hatch in case there is a regression).

WDYT?

Copy link
Member Author

@yuzefovich yuzefovich 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! 2 of 0 LGTMs obtained (waiting on @michae2)


pkg/sql/opt/xform/coster.go line 791 at r1 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

Do we want to reject plans with full scans of virtual tables?

This is the question that I don't know the answer to. I find this problem quite confusing - I believe that originally "disallow full table scans" feature was designed with user tables in mind, so we probably didn't consider how it should behave for virtual tables. The goal is for DB infra team to force their app developers think better about schema / query design in order to not introduce expensive queries (to protect stability of the cluster), and we use "full scan" or "large scan" as a proxy for being expensive. However, for virtual tables our users have no control over the schema; true, in some cases they could rewrite the queries to avoid full scans of vtables, but I'd guess it's usually not possible - this is due to CRDB missing virtual indexes (in case it is possible to implement those). Do we want to error out such queries without having any workaround? I'm not sure.

This thinking is why I decided to file (rather than fix it right away) #123783 so that we could discuss in case we want to have a way to prohibit full scans of vtables. The current approach allows us to fix the issue for the customer with low risk and have a backportable solution. As a follow-up, I plan to remove this session variable altogether on master (so that this behavior is unconditional), and then it'll be up to us to fix #123783 (where a new session variable would be warranted) separately, and we'll have the session var available on older releases (where I think we should consider defaulting to enabling this patch, and the session var would provide us an escape hatch in case there is a regression).

WDYT?

IIRC, the customer that ran into #122708 is the same that requested this feature originally. Perhaps we should reach out to them to see what they think with regards to prohibiting full scans of vtables?

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: We talked a bit, and I'm ok with this version, too.

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

@yuzefovich
Copy link
Member Author

TFTRs!

bors r+

@craig craig bot merged commit e0951b9 into cockroachdb:master May 9, 2024
21 of 22 checks passed
Copy link

blathers-crl bot commented May 9, 2024

Encountered an error creating backports. Some common things that can go wrong:

  1. The backport branch might have already existed.
  2. There was a merge conflict.
  3. The backport branch contained merge commits.

You might need to create your backport manually using the backport tool.


error creating merge commit from 1bb9662 to blathers/backport-release-23.1-123780: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 23.1.x failed. See errors above.


🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-23.1.x Flags PRs that need to be backported to 23.1 backport-23.2.x Flags PRs that need to be backported to 23.2. backport-24.1.x Flags PRs that need to be backported to 24.1.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sql: don't apply disallow_full_table_scans to virtual tables
5 participants