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: cache role existence checks to fix perf regression #111934

Merged
merged 1 commit into from
Oct 10, 2023

Conversation

rafiss
Copy link
Collaborator

@rafiss rafiss commented Oct 6, 2023

In 12ec26f, we started to check for the existence of a role whenever a privilege for the public role was checked. This can hapen multiple times during some pg_catalog queries, so it introduced a performance regression.

Now, the role existence is cached so we avoid the penalty. The existence of a role is cached for the duration of the entire transaction, and also gets inherited by any internal executor used to implement a command run by the user's transaction.

No release note since this is new in 23.2.

informs #20718
fixes #112102
Release note: None

@rafiss rafiss requested a review from fqazi October 6, 2023 18:24
@rafiss rafiss requested review from a team as code owners October 6, 2023 18:24
@rafiss rafiss requested review from DrewKimball and removed request for a team October 6, 2023 18:24
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@rafiss rafiss changed the title sql: cache role existence checks sql: cache role existence checks to fix perf regression Oct 6, 2023
In 12ec26f, we started to check for the
existence of a role whenever a privilege for the public role was
checked. This can hapen multiple times during some pg_catalog queries,
so it introduced a performance regression.

Now, the role existence is cached so we avoid the penalty. The existence
of a role is cached for the duration of the entire transaction, and also
gets inherited by any internal executor used to implement a command run
by the user's transaction.

No release note since this is new in 23.2.

Release note: None
@rafiss rafiss force-pushed the cache-role-exists branch from 7178d4b to cf61b37 Compare October 6, 2023 20:42
@rafiss rafiss requested review from ecwall and removed request for fqazi October 9, 2023 20:00
func (p *planner) RoleExists(ctx context.Context, role username.SQLUsername) (bool, error) {
return RoleExists(ctx, p.InternalSQLTxn(), role)
cache := p.EvalContext().RoleExistsCache
if cache != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

What causes the cache to be nil here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

it shouldn't be nil, it was just a defensive check

Copy link
Contributor

Choose a reason for hiding this comment

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

IMO defensive checks like this make the code harder to maintain because it is hard to tell the difference between something that is expected to be nil and a bug, but I'll leave it up to you if you want to keep it.

@rafiss rafiss requested a review from ecwall October 10, 2023 15:18
@rafiss
Copy link
Collaborator Author

rafiss commented Oct 10, 2023

tftr

bors r+

@rafiss rafiss added the backport-23.2.x Flags PRs that need to be backported to 23.2. label Oct 10, 2023
@craig
Copy link
Contributor

craig bot commented Oct 10, 2023

Build succeeded:

@craig craig bot merged commit 4447a52 into cockroachdb:master Oct 10, 2023
3 checks passed
@rafiss rafiss deleted the cache-role-exists branch October 11, 2023 01:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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: 23.2 performance regression caused by role existence checks
3 participants