Skip to content

Commit

Permalink
Merge #97727
Browse files Browse the repository at this point in the history
97727: pg_catalog: handle temp tables in lookup joins r=rafiss a=rafiss

The included test previously would have failed with an error like `unknown schema "[151]"`. In Postgres, temporary tables from other sessions are visible in pg_catalog, so we match that behavior.

Found in the npgsql nightly tests.
informs #97614

Release note (bug fix): Fixed an error that could occur when querying a pg_catalog table that included information about a temporary table created in another session.

Co-authored-by: Rafi Shamim <[email protected]>
  • Loading branch information
craig[bot] and rafiss committed Mar 1, 2023
2 parents a683fc7 + 6073b0e commit 8f3aeef
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
24 changes: 24 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/temp_table
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,27 @@ SELECT * FROM pg_temp.t
statement ok
USE defaultdb;
DROP DATABASE to_drop CASCADE;

subtest end

subtest temp_table_in_other_session

user testuser

statement ok
USE defaultdb;
CREATE TEMPORARY TABLE from_other_session(i INT PRIMARY KEY)

user root

statement ok
USE defaultdb

query TT
SELECT c.relname, a.attname FROM pg_attribute a
INNER LOOKUP JOIN pg_class c ON c.oid = a.attrelid
WHERE c.relname = 'from_other_session'
----
from_other_session i

subtest end
23 changes: 20 additions & 3 deletions pkg/sql/pg_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -1142,9 +1142,26 @@ func makeAllRelationsVirtualTableWithDescriptorIDIndex(
}
h := makeOidHasher()
scResolver := oneAtATimeSchemaResolver{p: p, ctx: ctx}
sc, err := p.Descriptors().ByIDWithLeased(p.txn).WithoutNonPublic().Get().Schema(ctx, table.GetParentSchemaID())
if err != nil {
return false, err
var sc catalog.SchemaDescriptor
if table.IsTemporary() {
// Temp tables from other sessions should still be visible here.
// Ideally, the catalog API would be able to return the temporary
// schemas from other sessions, but it cannot right now. See
// https://github.com/cockroachdb/cockroach/issues/97822.
if err := forEachSchema(ctx, p, db, false /* requiresPrivileges*/, func(schema catalog.SchemaDescriptor) error {
if schema.GetID() == table.GetParentSchemaID() {
sc = schema
}
return nil
}); err != nil {
return false, err
}
}
if sc == nil {
sc, err = p.Descriptors().ByIDWithLeased(p.txn).WithoutNonPublic().Get().Schema(ctx, table.GetParentSchemaID())
if err != nil {
return false, err
}
}
if err := populateFromTable(ctx, p, h, db, sc, table, scResolver,
addRow); err != nil {
Expand Down

0 comments on commit 8f3aeef

Please sign in to comment.