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: remove round-trips from common DISCARD ALL #95876

Conversation

ajwerner
Copy link
Contributor

@ajwerner ajwerner commented Jan 25, 2023

sql: avoid checking if a role exists when setting to the current role

This is an uncached round-trip. It makes DISCARD expensive.
In #86485 we implemented
SET SESSION AUTHORIZATION DEFAULT, this added an extra sql query to
DISCARD ALL to check if the role we'd become exists. This is almost
certainly unintentional in the case where the role we'd become is the
current session role. I think this just happened because of code
consolidation.

We now no longer check if the current session role exists. This removes
the last round-trip from DISCARD ALL.

sql: use in-memory session data to decide what to do in DISCARD

In #86246 we introduced logic to discard schemas when running DISCARD ALL and DISCARD TEMP. This logic did expensive kv operations unconditionally; if the session knew it had never created a temporary schema, we'd still fetch all databases and proceed to search all databases for a temp schema. This is very expensive.

Fixes: #95864

Release note (performance improvement): In 22.2, logic was added to make
SET SESSION AUTHORIZATION DEFAULT not a no-op. This implementation used
more general code for setting the role for a session which made sure that
the role exists. The check for whether a role exists is currently uncached.
We don't need to check if the role we already are exists. This improves the
performance of DISCARD ALL in addition to SET SESSION AUTHORIZATION DEFAULT.

Release note (performance improvement): In 22.2, we introduced support for DISCARD TEMP and made DISCARD ALL actually discard temp tables. This implementation ran expensive logic to discover temp schemas rather than consulting in-memory data structures. As a result, DISCARD ALL, which is issued regularly by connection pools, became an expensive operation when it should be cheap. This problem is now resolved.

@blathers-crl
Copy link

blathers-crl bot commented Jan 25, 2023

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.

@ajwerner ajwerner requested a review from rafiss January 25, 2023 22:10
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Epic: none

Release note: None
In cockroachdb#86246 we introduced logic to discard schemas when running DISCARD ALL and
DISCARD TEMP. This logic did expensive kv operations unconditionally; if the
session knew it had never created a temporary schema, we'd still fetch all
databases and proceed to search all databases for a temp schema. This is very
expensive.

Fixes: cockroachdb#95864

Release note (performance improvement): In 22.2, we introduced support for
`DISCARD TEMP` and made `DISCARD ALL` actually discard temp tables. This
implementation ran expensive logic to discover temp schemas rather than
consulting in-memory data structures. As a result, `DISCARD ALL`, which
is issued regularly by connection pools, became an expensive operation
when it should be cheap. This problem is now resolved.
This is an uncached round-trip. It makes `DISCARD` expensive.
In cockroachdb#86485 we implemented
`SET SESSION AUTHORIZATION DEFAULT`, this added an extra sql query to
`DISCARD ALL` to check if the role we'd become exists. This is almost
certainly unintentional in the case where the role we'd become is the
current session role. I think this just happened because of code
consolidation.

We now no longer check if the current session role exists. This removes
the last round-trip from DISCARD ALL.

Release note (performance improvement): In 22.2, logic was added to make
`SET SESSION AUTHORIZATION DEFAULT` not a no-op. This implementation used
more general code for setting the role for a session which made sure that
the role exists. The check for whether a role exists is currently uncached.
We don't need to check if the role we already are exists. This improves the
performance of `DISCARD ALL` in addition to `SET SESSION AUTHORIZATION
DEFAULT`.
@ajwerner ajwerner force-pushed the ajwerner/avoid-doing-expensive-work-when-discarding-temp-tables-and-there-are-not-any branch from fcd41dd to 74487fc Compare January 25, 2023 23:16
@ajwerner ajwerner changed the title sql: use in-memory session data to decide what to do in DISCARD sql: remove round-trips from common DISCARD ALL Jan 25, 2023
@ajwerner ajwerner marked this pull request as ready for review January 25, 2023 23:18
@ajwerner ajwerner requested a review from a team January 25, 2023 23:18
Copy link
Collaborator

@ZhouXing19 ZhouXing19 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 fix!

@ajwerner
Copy link
Contributor Author

TFTR!

bors r+

@craig
Copy link
Contributor

craig bot commented Jan 26, 2023

Build failed (retrying...):

@craig craig bot merged commit ed9928f into cockroachdb:master Jan 26, 2023
@craig
Copy link
Contributor

craig bot commented Jan 26, 2023

Build succeeded:

@blathers-crl
Copy link

blathers-crl bot commented Jan 26, 2023

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 66e265e to blathers/backport-release-22.2-95876: 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 22.2.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
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

'DISCARD ALL' performance regression on v22.2
3 participants