Skip to content

sql/opt: treat privilege errors as stale memos during dependency checks#169190

Merged
trunk-io[bot] merged 1 commit intocockroachdb:masterfrom
michae2:fix-cross-db-function-resolution
Apr 28, 2026
Merged

sql/opt: treat privilege errors as stale memos during dependency checks#169190
trunk-io[bot] merged 1 commit intocockroachdb:masterfrom
michae2:fix-cross-db-function-resolution

Conversation

@michae2
Copy link
Copy Markdown
Collaborator

@michae2 michae2 commented Apr 27, 2026

Previously, when the query cache's CheckDependencies re-resolved data sources from a cached memo and encountered a privilege error (e.g. because the memo referenced objects in a different database context), the error was propagated to the user. This caused unqualified function calls to fail with USAGE privilege errors referencing schemas from the wrong database when two databases had identically-named functions in custom schemas.

Now, maybeSwallowMetadataResolveErr also swallows pgcode.InsufficientPrivilege errors, treating them as indicators that the memo is stale. The memo is evicted and replanned in the correct user/database context, where genuine privilege errors will surface during planning.

Fixes: #168992

Release note (bug fix): Fixed a bug where unqualified function calls could fail with incorrect privilege errors when two databases on the same cluster had identically-named functions in custom schemas. The query cache could serve a memo from one database context to another, causing USAGE privilege errors referencing schemas from the wrong database.

Previously, when the query cache's `CheckDependencies` re-resolved data
sources from a cached memo and encountered a privilege error
(e.g. because the memo referenced objects in a different database
context), the error was propagated to the user. This caused unqualified
function calls to fail with USAGE privilege errors referencing schemas
from the wrong database when two databases had identically-named
functions in custom schemas.

Now, `maybeSwallowMetadataResolveErr` also swallows
`pgcode.InsufficientPrivilege` errors, treating them as indicators that
the memo is stale. The memo is evicted and replanned in the correct
user/database context, where genuine privilege errors will surface
during planning.

Fixes: cockroachdb#168992

Release note (bug fix): Fixed a bug where unqualified function calls
could fail with incorrect privilege errors when two databases on the
same cluster had identically-named functions in custom schemas. The
query cache could serve a memo from one database context to another,
causing USAGE privilege errors referencing schemas from the wrong
database.

Co-Authored-By: roachdev-claude <roachdev-claude-bot@cockroachlabs.com>
@trunk-io
Copy link
Copy Markdown
Contributor

trunk-io Bot commented Apr 27, 2026

😎 Merged successfully - details.

@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@michae2 michae2 requested review from a team, DrewKimball and rafiss April 27, 2026 22:31
@michae2 michae2 marked this pull request as ready for review April 27, 2026 22:31
@michae2 michae2 added backport-25.2.x Flags PRs that need to be backported to 25.2 backport-25.4.x Flags PRs that need to be backported to 25.4 backport-26.1.x Flags PRs that need to be backported to 26.1 backport-26.2.x Flags PRs that need to be backported to 26.2 labels Apr 27, 2026
@michae2
Copy link
Copy Markdown
Collaborator Author

michae2 commented Apr 27, 2026

How do we feel about backporting this without a session var or cluster setting?

@michae2 michae2 requested a review from mw5h April 27, 2026 22:35
@michae2 michae2 added the backport-25.2.18-rc FROZEN: requires ER request to thaw label Apr 27, 2026
Copy link
Copy Markdown
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 the fix!

@DrewKimball reviewed 2 files and all commit messages, and made 1 comment.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on mw5h and rafiss).

@michae2
Copy link
Copy Markdown
Collaborator Author

michae2 commented Apr 28, 2026

TFTR!

/trunk merge

@blathers-crl
Copy link
Copy Markdown

blathers-crl Bot commented Apr 28, 2026

Detected infrastructure failure on trunk-merge branch (matched: self-hosted runner lost communication with the server). Automatically resubmitting to merge queue (attempt 1 of 2). (run link)

@blathers-crl
Copy link
Copy Markdown

blathers-crl Bot commented Apr 28, 2026

/trunk merge

@trunk-io trunk-io Bot merged commit 9e5aaab into cockroachdb:master Apr 28, 2026
33 checks passed
@blathers-crl
Copy link
Copy Markdown

blathers-crl Bot commented Apr 28, 2026

Based on the specified backports for this PR, I applied new labels to the following linked issue(s). Please adjust the labels as needed to match the branches actually affected by the issue(s), including adding any known older branches.


Issue #168992: branch-release-25.2.18-rc, branch-release-25.4, branch-release-26.1, branch-release-26.2.


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

@blathers-crl
Copy link
Copy Markdown

blathers-crl Bot commented Apr 28, 2026

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 backport branch refs/heads/blathers/backport-release-25.2-169190: POST https://api.github.com/repos/michae2/cockroach/git/refs: 403 Resource not accessible by integration []

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


error creating backport branch refs/heads/blathers/backport-release-25.4-169190: POST https://api.github.com/repos/michae2/cockroach/git/refs: 403 Resource not accessible by integration []

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


error creating backport branch refs/heads/blathers/backport-release-26.1-169190: POST https://api.github.com/repos/michae2/cockroach/git/refs: 403 Resource not accessible by integration []

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


error creating backport branch refs/heads/blathers/backport-release-26.2-169190: POST https://api.github.com/repos/michae2/cockroach/git/refs: 403 Resource not accessible by integration []

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


error creating backport branch refs/heads/blathers/backport-release-25.2.18-rc-169190: POST https://api.github.com/repos/michae2/cockroach/git/refs: 403 Resource not accessible by integration []

Backport to branch 25.2.18-rc failed. See errors above.


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

@michae2 michae2 deleted the fix-cross-db-function-resolution branch April 28, 2026 22:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-25.2.x Flags PRs that need to be backported to 25.2 backport-25.2.18-rc FROZEN: requires ER request to thaw backport-25.4.x Flags PRs that need to be backported to 25.4 backport-26.1.x Flags PRs that need to be backported to 26.1 backport-26.2.x Flags PRs that need to be backported to 26.2 backport-failed target-release-26.3.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unqualified function resolution leaks descriptors across database boundaries

3 participants