Skip to content

sql/catalog: cache negative name lookups within a transaction#171979

Open
virajchogle wants to merge 1 commit into
cockroachdb:masterfrom
virajchogle:udt-negative-name-cache-171543
Open

sql/catalog: cache negative name lookups within a transaction#171979
virajchogle wants to merge 1 commit into
cockroachdb:masterfrom
virajchogle:udt-negative-name-cache-171543

Conversation

@virajchogle

Copy link
Copy Markdown
Contributor

Problem

By-name resolution of an unqualified object (notably a UDT cast like 'east'::region) probes each schema on the search_path in order. The not-found result was not cached: descriptor leases carry their own embedded name, so positive lookups are transitively cached via the leased descriptor, but a negative lookup has no leasable carrier. The miss bit lives in
cachedCatalogReader.byNameState but was never exposed to the layer that decides whether to call into the lease subsystem.

Reproducer (from #171543):

CREATE TYPE region AS ENUM ('east', 'west');
SELECT x::region FROM (VALUES ('east'), ('west'), ('east'), ('west')) AS v(x);

system.namespace reads scale linearly in rows × non-matching schemas.

Fix

Add CatalogReader.IsNameKnownToNotExist, consulted in the lookupStoreCacheID layer of Collection.getNonVirtualDescriptorID. Subsequent references to the same absent name within a txn halt before
lookupLeasedID, which would otherwise call resolveName and re-read system.namespace.

Per-txn only: the cached reader is reset at txn boundaries, and in-txn creates go through lookupUncommittedID (which runs first), so a name created mid-txn is never blocked. Cross-txn negative caching is left as a follow-up.

Tests

Extends TestDataDriven in pkg/sql/catalog/internal/catkv with an is_name_known_to_not_exist directive covering post-miss / unrelated absent / positive entry / post-reset cases, mirrored across both tenant testdata files.

Fixes #171543

Epic: none

Release note: none

By-name resolution of an absent object (e.g. 'east'::region when region is not on the search path) re-read system.namespace on every occurrence. Names have no leasable carrier for the negative case, and the cached reader's miss bit was never exposed to the resolver.

Add CatalogReader.IsNameKnownToNotExist and consult it in lookupStoreCacheID so subsequent references to an absent name halt before the leased lookup. The check is per-txn; in-txn creates run through the uncommitted layer first, so they are never blocked.

Fixes cockroachdb#171543
Epic: none
Release note: none
@virajchogle virajchogle requested a review from a team as a code owner June 25, 2026 01:09
@virajchogle virajchogle requested review from spilchen and removed request for a team June 25, 2026 01:09
@blathers-crl

blathers-crl Bot commented Jun 25, 2026

Copy link
Copy Markdown

Thank you for contributing to CockroachDB. Please ensure you have followed the guidelines for creating a PR.

My owl senses detect your PR is good for review. Please keep an eye out for any test failures in CI.

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

@blathers-crl blathers-crl Bot added the O-community Originated from the community label Jun 25, 2026
@aerfrei aerfrei added the T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) label Jun 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

O-community Originated from the community T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sql/catalog: unqualified UDT name resolution re-reads system.namespace on every occurrence

2 participants