Skip to content

Commit

Permalink
sql: add index on pg_namespace(oid)
Browse files Browse the repository at this point in the history
This will allow us to efficiently join to this table.

Release note: None
  • Loading branch information
rafiss committed Dec 9, 2022
1 parent 14748fe commit aec217f
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 22 deletions.
4 changes: 2 additions & 2 deletions pkg/bench/rttanalysis/testdata/benchmark_expectations
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ exp,benchmark
4,ORMQueries/information_schema._pg_index_position
3,ORMQueries/pg_attribute
3,ORMQueries/pg_class
9,ORMQueries/pg_is_other_temp_schema
17,ORMQueries/pg_is_other_temp_schema_multiple_times
7,ORMQueries/pg_is_other_temp_schema
7,ORMQueries/pg_is_other_temp_schema_multiple_times
4,ORMQueries/pg_my_temp_schema
4,ORMQueries/pg_my_temp_schema_multiple_times
4,ORMQueries/pg_namespace
Expand Down
5 changes: 3 additions & 2 deletions pkg/sql/logictest/testdata/logic_test/pg_catalog
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,8 @@ pg_catalog.pg_namespace CREATE TABLE pg_catalog.pg_namespace (
oid OID NULL,
nspname NAME NOT NULL,
nspowner OID NULL,
nspacl STRING[] NULL
nspacl STRING[] NULL,
INDEX pg_namespace_oid_idx (oid ASC) STORING (nspname, nspowner, nspacl)
)

query TTBTTTB colnames
Expand Down Expand Up @@ -2593,7 +2594,7 @@ objoid classoid objsubid description
4294967098 4294967123 0 pg_largeobject_metadata was created for compatibility and is currently unimplemented
4294967096 4294967123 0 locks held by active processes (empty - feature does not exist)
4294967095 4294967123 0 available materialized views (empty - feature does not exist)
4294967094 4294967123 0 available namespaces (incomplete; namespaces and databases are congruent in CockroachDB)
4294967094 4294967123 0 available namespaces
4294967093 4294967123 0 opclass (empty - Operator classes not supported yet)
4294967092 4294967123 0 operators (incomplete)
4294967091 4294967123 0 pg_opfamily was created for compatibility and is currently unimplemented
Expand Down
9 changes: 9 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/temp_table
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ SELECT table_name, type FROM [SHOW TABLES]
----
tbl table

# Verify that temp schema is visible in pg_namespace index.
let $tempSchemaOID
SELECT oid FROM pg_namespace where nspname LIKE 'pg_temp%'

query B
SELECT nspname LIKE 'pg_temp%' FROM pg_namespace where oid = $tempSchemaOID
----
true

statement ok
DROP TABLE tbl

Expand Down
18 changes: 7 additions & 11 deletions pkg/sql/opt/exec/execbuilder/testdata/virtual
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,15 @@ ORDER BY
distribution: local
vectorized: true
·
• sort
│ order: +conname
• render
└── • render
└── • virtual table lookup join
│ table: pg_namespace@pg_namespace_oid_idx
│ equality: (connamespace) = (oid)
│ pred: nspname = 'public'
└── • hash join
│ equality: (oid) = (connamespace)
├── • filter
│ │ filter: nspname = 'public'
│ │
│ └── • virtual table
│ table: pg_namespace@primary
└── • sort
│ order: +conname
└── • virtual table lookup join
│ table: pg_attribute@pg_attribute_attrelid_idx
Expand Down
70 changes: 64 additions & 6 deletions pkg/sql/pg_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ import (
"time"
"unicode"

"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/security/username"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catalogkeys"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catformat"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catprivilege"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/schemadesc"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/schemaexpr"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/typedesc"
Expand Down Expand Up @@ -2047,12 +2049,13 @@ https://www.postgresql.org/docs/9.6/view-pg-matviews.html`,
},
}

var adminOID = makeOidHasher().UserOid(username.MakeSQLUsernameFromPreNormalizedString("admin"))

var pgCatalogNamespaceTable = virtualSchemaTable{
comment: `available namespaces (incomplete; namespaces and databases are congruent in CockroachDB)
comment: `available namespaces
https://www.postgresql.org/docs/9.5/catalog-pg-namespace.html`,
schema: vtable.PGCatalogNamespace,
populate: func(ctx context.Context, p *planner, dbContext catalog.DatabaseDescriptor, addRow func(...tree.Datum) error) error {
h := makeOidHasher()
return forEachDatabaseDesc(ctx, p, dbContext, true, /* requiresPrivileges */
func(db catalog.DatabaseDescriptor) error {
return forEachSchema(ctx, p, db, func(sc catalog.SchemaDescriptor) error {
Expand All @@ -2065,10 +2068,7 @@ https://www.postgresql.org/docs/9.5/catalog-pg-namespace.html`,
}
} else if sc.SchemaKind() == catalog.SchemaPublic {
// admin is the owner of the public schema.
//
// TODO(ajwerner): The public schema effectively carries the privileges
// of the database so consider using the database's owner for public.
ownerOID = h.UserOid(username.MakeSQLUsernameFromPreNormalizedString("admin"))
ownerOID = adminOID
}
return addRow(
schemaOid(sc.GetID()), // oid
Expand All @@ -2079,6 +2079,64 @@ https://www.postgresql.org/docs/9.5/catalog-pg-namespace.html`,
})
})
},
indexes: []virtualIndex{
{
populate: func(ctx context.Context, unwrappedConstraint tree.Datum, p *planner, db catalog.DatabaseDescriptor,
addRow func(...tree.Datum) error,
) (bool, error) {
coid := tree.MustBeDOid(unwrappedConstraint)
ooid := coid.Oid
sc, ok, err := func() (_ catalog.SchemaDescriptor, found bool, _ error) {
// The system database still does not have a physical public schema.
if !db.HasPublicSchemaWithDescriptor() && ooid == keys.SystemPublicSchemaID {
return schemadesc.GetPublicSchema(), true, nil
}
if sc, ok := schemadesc.GetVirtualSchemaByID(descpb.ID(ooid)); ok {
return sc, true, nil
}
if sc, err := p.Descriptors().GetImmutableSchemaByID(
ctx, p.Txn(), descpb.ID(ooid), tree.SchemaLookupFlags{Required: true},
); err == nil {
return sc, true, nil
} else if !sqlerrors.IsUndefinedSchemaError(err) {
return nil, false, err
}
// Fallback to looking for temporary schemas.
schemaNames, err := getSchemaNames(ctx, p, db)
if err != nil {
return nil, false, err
}
if scName, ok := schemaNames[descpb.ID(ooid)]; ok {
return schemadesc.NewTemporarySchema(scName, descpb.ID(ooid), db.GetID()), true, nil
}
return nil, false, nil
}()
if !ok || err != nil {
return false, err
}
ownerOID := tree.DNull
if sc.SchemaKind() == catalog.SchemaUserDefined {
var err error
ownerOID, err = getOwnerOID(ctx, p, sc)
if err != nil {
return false, err
}
} else if sc.SchemaKind() == catalog.SchemaPublic {
// admin is the owner of the public schema.
ownerOID = adminOID
}
if err := addRow(
schemaOid(sc.GetID()), // oid
tree.NewDString(sc.GetName()), // nspname
ownerOID, // nspowner
tree.DNull, // nspacl
); err != nil {
return false, err
}
return true, nil
},
},
},
}

var (
Expand Down
3 changes: 2 additions & 1 deletion pkg/sql/vtable/pg_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,8 @@ CREATE TABLE pg_catalog.pg_namespace (
oid OID,
nspname NAME NOT NULL,
nspowner OID,
nspacl STRING[]
nspacl STRING[],
INDEX (oid)
)`

// PGCatalogOpclass describes the schema of the pg_catalog.pg_opclass table.
Expand Down

0 comments on commit aec217f

Please sign in to comment.