diff --git a/pkg/sql/information_schema.go b/pkg/sql/information_schema.go index 9b989e1d211b..891d1d74d0c7 100644 --- a/pkg/sql/information_schema.go +++ b/pkg/sql/information_schema.go @@ -2162,15 +2162,20 @@ func forEachDatabaseDesc( dbDescs = append(dbDescs, dbContext) } - // Ignore databases that the user cannot see. + // Ignore databases that the user cannot see. We add a special case for the + // current database. This is because we currently allow a user to connect + // to a database even without the CONNECT privilege, but it would be poor + // UX to not show the current database in pg_catalog/information_schema + // tables. + // See https://github.com/cockroachdb/cockroach/issues/59875. for _, dbDesc := range dbDescs { canSeeDescriptor := !requiresPrivileges if requiresPrivileges { - var err error - canSeeDescriptor, err = userCanSeeDescriptor(ctx, p, dbDesc, nil /* parentDBDesc */, false /* allowAdding */) + hasPriv, err := userCanSeeDescriptor(ctx, p, dbDesc, nil /* parentDBDesc */, false /* allowAdding */) if err != nil { return err } + canSeeDescriptor = hasPriv || p.CurrentDatabase() == dbDesc.GetName() } if canSeeDescriptor { if err := fn(dbDesc); err != nil { diff --git a/pkg/sql/logictest/testdata/logic_test/drop_view b/pkg/sql/logictest/testdata/logic_test/drop_view index 65371d67d88e..df4cce86caf3 100644 --- a/pkg/sql/logictest/testdata/logic_test/drop_view +++ b/pkg/sql/logictest/testdata/logic_test/drop_view @@ -99,6 +99,9 @@ DROP VIEW testuser3 query TTTTIT SHOW TABLES FROM test ---- +public d view root 0 NULL +public testuser1 view root 0 NULL +public testuser2 view root 0 NULL statement error cannot drop relation "testuser1" because view "testuser2" depends on it DROP VIEW testuser1 @@ -112,6 +115,7 @@ DROP VIEW testuser1 CASCADE query TTTTIT SHOW TABLES FROM test ---- +public d view root 0 NULL statement error pgcode 42P01 relation "testuser2" does not exist DROP VIEW testuser2 diff --git a/pkg/sql/logictest/testdata/logic_test/pg_catalog b/pkg/sql/logictest/testdata/logic_test/pg_catalog index 928dcb192849..2812c34d5d2c 100644 --- a/pkg/sql/logictest/testdata/logic_test/pg_catalog +++ b/pkg/sql/logictest/testdata/logic_test/pg_catalog @@ -432,6 +432,30 @@ oid nspname nspowner nspacl 1098122499 pg_extension NULL NULL 4101115737 public 2310524507 NULL +# Verify that we can still see the schemas even if we don't have any privilege +# on the current database. + +statement ok +REVOKE ALL ON DATABASE test FROM public; +REVOKE ALL ON DATABASE test FROM testuser + +user testuser + +query OTOT colnames +SELECT * FROM pg_catalog.pg_namespace +---- +oid nspname nspowner nspacl +1445254017 crdb_internal NULL NULL +155990598 information_schema NULL NULL +2154378761 pg_catalog NULL NULL +1098122499 pg_extension NULL NULL +4101115737 public 2310524507 NULL + +user root + +statement ok +GRANT CONNECT ON DATABASE test TO public + ## pg_catalog.pg_database query OTOITTBB colnames