Skip to content

Commit

Permalink
sql: exclude anonymous database from pg_catalog and pg_extension sche…
Browse files Browse the repository at this point in the history
…mas 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:

```
[email protected]:26257/movr> SELECT count(*) FROM pg_extension.spatial_ref_sys;
  count
---------
   6139
(1 row)

Time: 17ms total (execution 17ms / network 0ms)

[email protected]: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
  • Loading branch information
ecwall committed Jun 30, 2023
1 parent 55b7520 commit 6b4478c
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions pkg/sql/sem/tree/table_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 6b4478c

Please sign in to comment.