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: support index hints for insert and upsert #119104

Merged
merged 1 commit into from Feb 20, 2024

Conversation

rytaft
Copy link
Collaborator

@rytaft rytaft commented Feb 12, 2024

Fixes #98211

Release note (sql change): Added support for index hints with INSERT and UPSERT statements. This allows INSERT ... ON CONFLICT and UPSERT queries to use index hints in the same way they are already supported for UPDATE and DELETE statements.

@rytaft rytaft requested review from a team as code owners February 12, 2024 20:54
@rytaft rytaft requested review from mgartner and removed request for a team February 12, 2024 20:54
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@rytaft rytaft requested a review from michae2 February 12, 2024 20:56
@rytaft rytaft 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. labels Feb 12, 2024
@rytaft rytaft force-pushed the insert_upsert_index_hints branch 4 times, most recently from 5d827f4 to fe4d9c3 Compare February 14, 2024 20:52
Fixes cockroachdb#98211

Release note (sql change): Added support for index hints with INSERT
and UPSERT statements. This allows INSERT ... ON CONFLICT and UPSERT
queries to use index hints in the same way they are already supported
for UPDATE and DELETE statements.
@rytaft rytaft requested review from yuzefovich and DrewKimball and removed request for michae2 and yuzefovich February 20, 2024 15:15
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!

My only reservation is that it seems more difficult to determine whether a set of index hints will be valid for INSERT and UPSERT than for UPDATE and DELETE, since this involves more than just a straightforward scan with a WHERE clause. It's easy to determine whether a set of hints is compatible with the current schema by just running EXPLAIN for the query, but how does the user determine whether a schema change will invalidate existing hints without actually running the schema change?

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

Copy link
Collaborator Author

@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.

TFTR! Not sure I understand the concerns:

My only reservation is that it seems more difficult to determine whether a set of index hints will be valid for INSERT and UPSERT than for UPDATE and DELETE, since this involves more than just a straightforward scan with a WHERE clause

Difficult in the code? Or for the user? Not sure I understand.

how does the user determine whether a schema change will invalidate existing hints without actually running the schema change?

I don't think we have a good way to do this, but this has already been a problem for all types of hints (including for SELECT statements), right?

Maybe you can give me an example of a potential problem here?

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

@rytaft
Copy link
Collaborator Author

rytaft commented Feb 20, 2024

(Also note that the primary motivation for this change at the moment is to be able to use the NO_FULL_SCAN hint, which doesn't depend on any specific index)

@DrewKimball
Copy link
Collaborator

Difficult in the code? Or for the user? Not sure I understand.

I mean for the user - for UPDATE and DELETE, there's predictably a scan over the table with the filters specified in the WHERE clause. Whereas for INSERT and UPSERT, it's hard to tell what scans/joins will be planned without looking at the explain output.

I don't think we have a good way to do this, but this has already been a problem for all types of hints (including for SELECT statements), right?

You're right, I just thought it might be more of an issue here since it's harder to tell when a query is "sensitive" to a schema change.

I'm totally happy merging this as-is, just wanted to bring it up in case you had any ideas.

@rytaft
Copy link
Collaborator Author

rytaft commented Feb 20, 2024

I see, thanks. Yea, I don't have any great ideas. Maybe it would be useful to have a feature in the DBConsole to see all indexes that have been hinted in the last X hours? And as far as helping the user know which scan(s) will be affected by a hint, we might just want to suggest in the docs that they use EXPLAIN.

TFTR!

bors r+

@craig
Copy link
Contributor

craig bot commented Feb 20, 2024

Build succeeded:

@craig craig bot merged commit 4585b1d into cockroachdb:master Feb 20, 2024
13 of 17 checks passed
Copy link

blathers-crl bot commented Feb 20, 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 5a5739d to blathers/backport-release-23.1-119104: 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.

@mgartner
Copy link
Collaborator

Cool! I agree that it's a bit confusing what can happen when you specify a specific index for an insert or upsert. You may have 3 arbiter indexes selected, but because the hint propagates into the uniqueness checks (the inlined ones, not the post-query ones†) you won't actually use all 3 indexes. But I think this is ok because it's consistent with our hints in general—they specify a physical requirement (perform a scan index i) rather than a logical one (catch a conflict of the logical uniqueness property defined by arbiter i). This might be something we want to clarify in the docs.

† Does the hint also propagate to post-query uniqueness checks and FK checks? This might be something to add tests for.

@rytaft
Copy link
Collaborator Author

rytaft commented Feb 23, 2024

TFTR!

Does the hint also propagate to post-query uniqueness checks and FK checks?

For foreign key checks, we are only scanning the referenced or referencing table that is not updated / upserted / deleted, so there's no way the hint could propagate (but I also verified manually just to be sure). I opened #119597 with a few tests showing that the hints also do not propagate to uniqueness checks.

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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sql: support index hints for inserts and upserts
4 participants