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 Oct 29, 2022
1 parent 70d9fed commit 0126e47
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 11 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
3,ORMQueries/information_schema._pg_index_position
2,ORMQueries/pg_attribute
2,ORMQueries/pg_class
10,ORMQueries/pg_is_other_temp_schema
18,ORMQueries/pg_is_other_temp_schema_multiple_times
8,ORMQueries/pg_is_other_temp_schema
8,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
6 changes: 4 additions & 2 deletions pkg/sql/logictest/testdata/logic_test/create_statements
Original file line number Diff line number Diff line change
Expand Up @@ -4772,12 +4772,14 @@ 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)
) 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)
) {} {}
CREATE TABLE pg_catalog.pg_opclass (
oid OID NULL,
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 @@ -3991,7 +3992,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
59 changes: 55 additions & 4 deletions pkg/sql/pg_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"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 @@ -2091,7 +2092,7 @@ https://www.postgresql.org/docs/9.6/view-pg-matviews.html`,
}

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 {
Expand All @@ -2108,9 +2109,6 @@ 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"))
}
return addRow(
Expand All @@ -2122,6 +2120,59 @@ https://www.postgresql.org/docs/9.5/catalog-pg-namespace.html`,
})
})
},
indexes: []virtualIndex{
{
// partial is true because this index does not contain temporary schemas.
partial: true,
populate: func(ctx context.Context, unwrappedConstraint tree.Datum, p *planner, db catalog.DatabaseDescriptor,
addRow func(...tree.Datum) error,
) (bool, error) {
h := makeOidHasher()
coid := tree.MustBeDOid(unwrappedConstraint)
ooid := coid.Oid
sc, err := func() (catalog.SchemaDescriptor, error) {
// The system database still does not have a physical public schema.
if !db.HasPublicSchemaWithDescriptor() && ooid == keys.SystemPublicSchemaID {
return schemadesc.GetPublicSchema(), nil
}
if sc, ok := schemadesc.GetVirtualSchemaByID(descpb.ID(ooid)); ok {
return sc, nil
}
return p.Descriptors().GetImmutableSchemaByID(
ctx, p.Txn(), descpb.ID(ooid), tree.SchemaLookupFlags{Required: true},
)
}()
if err != nil {
if sqlerrors.IsUndefinedSchemaError(err) {
// No schema found, so no rows.
//nolint:returnerrcheck
return false, 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 = h.UserOid(username.MakeSQLUsernameFromPreNormalizedString("admin"))
}
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 0126e47

Please sign in to comment.