From 6b4478c682503c5e84059fe7cbc9018c94ce9223 Mon Sep 17 00:00:00 2001 From: Evan Wall Date: Fri, 30 Jun 2023 20:01:17 +0000 Subject: [PATCH] sql: exclude anonymous database from pg_catalog and pg_extension schemas in TableName.FQString() Fixes #105929 `"".pg_extension.spatial_ref_sys` is the output of `TableName.FQString()` for `spatial_ref_sys` which results in an error when trying to use the value: ``` demo@127.0.0.1:26257/movr> SELECT count(*) FROM pg_extension.spatial_ref_sys; count --------- 6139 (1 row) Time: 17ms total (execution 17ms / network 0ms) demo@127.0.0.1:26257/movr> SELECT count(*) FROM "".pg_extension.spatial_ref_sys; ERROR: cannot access virtual schema in anonymous database SQLSTATE: 42704 HINT: verify that the current database is set ``` This PR fixes this by removing the `"".` prefix. Release note: None --- pkg/sql/sem/tree/table_name.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/pkg/sql/sem/tree/table_name.go b/pkg/sql/sem/tree/table_name.go index bbcc40af19c7..8831542cf799 100644 --- a/pkg/sql/sem/tree/table_name.go +++ b/pkg/sql/sem/tree/table_name.go @@ -10,6 +10,8 @@ package tree +import "github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants" + // TableName corresponds to the name of a table in a FROM clause, // INSERT or UPDATE statement, etc. // @@ -46,8 +48,13 @@ func (t *TableName) objectName() {} // schema and catalog names. Suitable for logging, etc. func (t *TableName) FQString() string { ctx := NewFmtCtx(FmtSimple) - ctx.FormatNode(&t.CatalogName) - ctx.WriteByte('.') + schemaName := t.SchemaName.String() + // The pg_catalog and pg_extension schemas cannot be referenced from inside + // an anonymous ("") database. This makes their FQ string always relative. + if schemaName != catconstants.PgCatalogName && schemaName != catconstants.PgExtensionSchemaName { + ctx.FormatNode(&t.CatalogName) + ctx.WriteByte('.') + } ctx.FormatNode(&t.SchemaName) ctx.WriteByte('.') ctx.FormatNode(&t.ObjectName)