Skip to content

sql/catalog: resolve temp schemas from other sessions by ID#165395

Merged
trunk-io[bot] merged 1 commit intocockroachdb:masterfrom
rafiss:resolve-temp-schema
Mar 17, 2026
Merged

sql/catalog: resolve temp schemas from other sessions by ID#165395
trunk-io[bot] merged 1 commit intocockroachdb:masterfrom
rafiss:resolve-temp-schema

Conversation

@rafiss
Copy link
Collaborator

@rafiss rafiss commented Mar 11, 2026

Temporary schemas have no descriptor in system.descriptor — only namespace entries in system.namespace. The by-ID resolution path (lookupTemporary) only checked the current session's data via TemporarySchemaProvider, so temp schemas from other sessions couldn't be resolved by ID even though they could be found by name.

This caused failures when resolving a table from another session's temp schema, then trying to resolve its parent schema by ID. A workaround existed in pg_catalog.go that iterated all schemas to find the match.

Fix this by enhancing the by-ID resolution path to check the catalog reader's cached namespace entries as a fallback when the session-local check returns nil. Also fix IsDescIDKnownToNotExist to not incorrectly report temp schema IDs as non-existent after ScanAll (since they have namespace entries but no descriptors). With these fixes, the pg_catalog.go workaround is no longer needed and is removed.

Resolves: #97822

Release note (bug fix): Fixed a bug where temporary tables created in one session could fail to appear in pg_catalog queries from another session because the parent temporary schema could not be resolved by ID.

@trunk-io
Copy link
Contributor

trunk-io bot commented Mar 11, 2026

😎 Merged successfully - details.

@blathers-crl
Copy link

blathers-crl bot commented Mar 11, 2026

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.

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@rafiss rafiss marked this pull request as ready for review March 11, 2026 15:50
@rafiss rafiss requested a review from a team as a code owner March 11, 2026 15:50
@rafiss rafiss requested a review from fqazi March 11, 2026 15:51
Copy link
Collaborator

@fqazi fqazi left a comment

Choose a reason for hiding this comment

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

One concern with this change

@fqazi reviewed 4 files and all commit messages, and made 2 comments.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on rafiss).


pkg/sql/catalog/nstree/catalog.go line 189 at r1 (raw file):

// temporary schemas from other sessions, which have namespace entries but
// no descriptors.
func (c Catalog) LookupNamespaceEntryByID(id descpb.ID) NamespaceEntry {

This does a full search of the namespaces read by the transaction each getDescriptorsByID. Could we some how make this cheaper, if we need to enhance the cache / catalog read it may be worthwhile especially with a large number of descriptors.

Temporary schemas have no descriptor in system.descriptor — only
namespace entries in system.namespace. The by-ID resolution path
(lookupTemporary) only checked the current session's data via
TemporarySchemaProvider, so temp schemas from other sessions couldn't
be resolved by ID even though they could be found by name.

This caused failures when resolving a table from another session's
temp schema, then trying to resolve its parent schema by ID. A
workaround existed in pg_catalog.go that iterated all schemas to
find the match.

Fix this by enhancing the by-ID resolution path to check the catalog
reader's cached namespace entries as a fallback when the session-local
check returns nil. Also fix IsDescIDKnownToNotExist to not incorrectly
report temp schema IDs as non-existent after ScanAll (since they have
namespace entries but no descriptors). With these fixes, the
pg_catalog.go workaround is no longer needed and is removed.

Resolves: cockroachdb#97822

Release note (bug fix): Fixed a bug where temporary tables created
in one session could fail to appear in pg_catalog queries from
another session because the parent temporary schema could not be
resolved by ID.

Co-Authored-By: roachdev-claude <roachdev-claude-bot@cockroachlabs.com>
@rafiss rafiss force-pushed the resolve-temp-schema branch from 3b96f8b to 3fda94f Compare March 17, 2026 04:24
Copy link
Collaborator Author

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

@rafiss made 1 comment.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on fqazi).


pkg/sql/catalog/nstree/catalog.go line 189 at r1 (raw file):

Previously, fqazi (Faizan Qazi) wrote…

This does a full search of the namespaces read by the transaction each getDescriptorsByID. Could we some how make this cheaper, if we need to enhance the cache / catalog read it may be worthwhile especially with a large number of descriptors.

done

@rafiss rafiss requested a review from fqazi March 17, 2026 04:24
Copy link
Collaborator

@fqazi fqazi left a comment

Choose a reason for hiding this comment

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

@rafiss Nice work!

:lgtm_strong:

@fqazi reviewed 3 files and all commit messages, made 1 comment, and resolved 1 discussion.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on rafiss).

@rafiss
Copy link
Collaborator Author

rafiss commented Mar 17, 2026

/trunk merge

@trunk-io trunk-io bot merged commit 11464e8 into cockroachdb:master Mar 17, 2026
28 checks passed
@rafiss rafiss deleted the resolve-temp-schema branch March 18, 2026 01:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

catalog: fix limitation with resolving temp schemas from other sessions by ID

3 participants