Skip to content

Commit

Permalink
pg_catalog: add virtual indexes and optimize existing ones
Browse files Browse the repository at this point in the history
Previously, the pg_description table was missing a virtual
index on objoid, with the addition of descriptions from all
builtins this table has much more data by default. As a result
any query joining with this table is slow. To address this,
this patch will first add a virtual index on the index
description table, and update existing incomplete indexes
on OID's to skip over builtins that refer to builtin functions.

Release note (performance improvement): improve performance
when joining with the pg_description table
  • Loading branch information
fqazi authored and rafiss committed May 16, 2023
1 parent a37130d commit dc93f55
Show file tree
Hide file tree
Showing 11 changed files with 183 additions and 130 deletions.
2 changes: 1 addition & 1 deletion pkg/bench/rttanalysis/testdata/benchmark_expectations
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ exp,benchmark
12,ORMQueries/hasura_column_descriptions_8_tables
5,ORMQueries/hasura_column_descriptions_modified
4,ORMQueries/information_schema._pg_index_position
134,ORMQueries/introspection_description_join
132,ORMQueries/introspection_description_join
4,ORMQueries/pg_attribute
4,ORMQueries/pg_class
6,ORMQueries/pg_is_other_temp_schema
Expand Down
2 changes: 2 additions & 0 deletions pkg/cli/clisqlshell/testdata/describe
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ pg_catalog,pg_db_role_setting,table,admin,NULL
pg_catalog,pg_default_acl,table,admin,NULL
pg_catalog,pg_depend,table,admin,NULL
pg_catalog,pg_description,table,admin,NULL
pg_catalog,pg_description_objoid_idx,index,admin,NULL
pg_catalog,pg_enum,table,admin,NULL
pg_catalog,pg_event_trigger,table,admin,NULL
pg_catalog,pg_extension,table,admin,NULL
Expand Down Expand Up @@ -563,6 +564,7 @@ pg_catalog,pg_depend,table,admin,NULL,permanent,prefix,"dependency relationships
https://www.postgresql.org/docs/9.5/catalog-pg-depend.html"
pg_catalog,pg_description,table,admin,NULL,permanent,prefix,"object comments
https://www.postgresql.org/docs/9.5/catalog-pg-description.html"
pg_catalog,pg_description_objoid_idx,index,admin,NULL,permanent,prefix,
pg_catalog,pg_enum,table,admin,NULL,permanent,prefix,"enum types and labels (empty - feature does not exist)
https://www.postgresql.org/docs/9.5/catalog-pg-enum.html"
pg_catalog,pg_event_trigger,table,admin,NULL,permanent,prefix,"event triggers (empty - feature does not exist)
Expand Down
2 changes: 2 additions & 0 deletions pkg/sql/catalog/nstree/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ type Catalog struct {
// immutable catalogs that are incomeplete and only contain comment information.
type CommentCatalog interface {
ForEachComment(fn func(key catalogkeys.CommentKey, cmt string) error) error
ForEachCommentOnDescriptor(
id descpb.ID, fn func(key catalogkeys.CommentKey, cmt string) error) error
LookupComment(key catalogkeys.CommentKey) (_ string, found bool)
}

Expand Down
12 changes: 0 additions & 12 deletions pkg/sql/crdb_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -5415,18 +5415,6 @@ CREATE TABLE crdb_internal.kv_catalog_comments (
if err != nil {
return err
}
// Delegate privilege check to system table.
{
sysTable, err := p.Descriptors().ByIDWithLeased(p.txn).Get().Table(ctx, systemschema.CommentsTable.GetID())
if err != nil {
return err
}
if ok, err := p.HasPrivilege(ctx, sysTable, privilege.SELECT, p.User()); err != nil {
return err
} else if !ok {
return nil
}
}
// Loop over all comment entries.
// NB if ever anyone were to extend this table to carry column
// comments, make sure to update pg_catalog.col_description to
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/information_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ https://www.postgresql.org/docs/9.5/infoschema-columns.html`,
schema: vtable.InformationSchemaColumns,
populate: func(ctx context.Context, p *planner, dbContext catalog.DatabaseDescriptor, addRow func(...tree.Datum) error) error {
// Get the collations for all comments of current database.
comments, err := getComments(ctx, p)
comments, err := getComments(ctx, p, descpb.InvalidID /* all comments */)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/logictest/testdata/logic_test/comment_on
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ CREATE TABLE t_99316(a INT);
statement ok
INSERT INTO system.comments VALUES (4294967122, 't_99316'::regclass::OID, 0, 'bar');

statement error pgcode XX000 select-comments: invalid comment type 4294967122
statement error pgcode XX000 internal error: select-comments: invalid comment type 4294967122
SELECT * FROM pg_catalog.pg_description WHERE objoid = 't'::regclass::OID;

statement ok
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ SELECT id, strip_volatile(descriptor) FROM crdb_internal.kv_catalog_descriptor
4294967093 {"table": {"columns": [{"id": 1, "name": "oid", "nullable": true, "type": {"family": "OidFamily", "oid": 26}}, {"id": 2, "name": "extname", "nullable": true, "type": {"family": 11, "oid": 19}}, {"id": 3, "name": "extowner", "nullable": true, "type": {"family": "OidFamily", "oid": 26}}, {"id": 4, "name": "extnamespace", "nullable": true, "type": {"family": "OidFamily", "oid": 26}}, {"id": 5, "name": "extrelocatable", "nullable": true, "type": {"oid": 16}}, {"id": 6, "name": "extversion", "nullable": true, "type": {"family": "StringFamily", "oid": 25}}, {"id": 7, "name": "extconfig", "nullable": true, "type": {"family": "StringFamily", "oid": 25}}, {"id": 8, "name": "extcondition", "nullable": true, "type": {"family": "StringFamily", "oid": 25}}], "formatVersion": 3, "id": 4294967093, "name": "pg_extension", "nextColumnId": 9, "nextConstraintId": 2, "nextIndexId": 2, "nextMutationId": 1, "primaryIndex": {"constraintId": 1, "foreignKey": {}, "geoConfig": {}, "id": 1, "interleave": {}, "partitioning": {}, "sharded": {}}, "privileges": {"ownerProto": "node", "users": [{"privileges": "32", "userProto": "public"}], "version": 2}, "replacementOf": {"time": {}}, "unexposedParentSchemaId": 4294967118, "version": "1"}}
4294967094 {"table": {"columns": [{"id": 1, "name": "evtname", "nullable": true, "type": {"family": 11, "oid": 19}}, {"id": 2, "name": "evtevent", "nullable": true, "type": {"family": 11, "oid": 19}}, {"id": 3, "name": "evtowner", "nullable": true, "type": {"family": "OidFamily", "oid": 26}}, {"id": 4, "name": "evtfoid", "nullable": true, "type": {"family": "OidFamily", "oid": 26}}, {"id": 5, "name": "evtenabled", "nullable": true, "type": {"family": "StringFamily", "oid": 18, "visibleType": 9, "width": 1}}, {"id": 6, "name": "evttags", "nullable": true, "type": {"arrayContents": {"family": "StringFamily", "oid": 25}, "arrayElemType": "StringFamily", "family": "ArrayFamily", "oid": 1009}}, {"id": 7, "name": "oid", "nullable": true, "type": {"family": "OidFamily", "oid": 26}}], "formatVersion": 3, "id": 4294967094, "name": "pg_event_trigger", "nextColumnId": 8, "nextConstraintId": 2, "nextIndexId": 2, "nextMutationId": 1, "primaryIndex": {"constraintId": 1, "foreignKey": {}, "geoConfig": {}, "id": 1, "interleave": {}, "partitioning": {}, "sharded": {}}, "privileges": {"ownerProto": "node", "users": [{"privileges": "32", "userProto": "public"}], "version": 2}, "replacementOf": {"time": {}}, "unexposedParentSchemaId": 4294967118, "version": "1"}}
4294967095 {"table": {"columns": [{"id": 1, "name": "oid", "nullable": true, "type": {"family": "OidFamily", "oid": 26}}, {"id": 2, "name": "enumtypid", "nullable": true, "type": {"family": "OidFamily", "oid": 26}}, {"id": 3, "name": "enumsortorder", "nullable": true, "type": {"family": "FloatFamily", "oid": 700, "visibleType": 5, "width": 32}}, {"id": 4, "name": "enumlabel", "nullable": true, "type": {"family": "StringFamily", "oid": 25}}], "formatVersion": 3, "id": 4294967095, "name": "pg_enum", "nextColumnId": 5, "nextConstraintId": 2, "nextIndexId": 2, "nextMutationId": 1, "primaryIndex": {"constraintId": 1, "foreignKey": {}, "geoConfig": {}, "id": 1, "interleave": {}, "partitioning": {}, "sharded": {}}, "privileges": {"ownerProto": "node", "users": [{"privileges": "32", "userProto": "public"}], "version": 2}, "replacementOf": {"time": {}}, "unexposedParentSchemaId": 4294967118, "version": "1"}}
4294967096 {"table": {"columns": [{"id": 1, "name": "objoid", "nullable": true, "type": {"family": "OidFamily", "oid": 26}}, {"id": 2, "name": "classoid", "nullable": true, "type": {"family": "OidFamily", "oid": 26}}, {"id": 3, "name": "objsubid", "nullable": true, "type": {"family": "IntFamily", "oid": 23, "width": 32}}, {"id": 4, "name": "description", "nullable": true, "type": {"family": "StringFamily", "oid": 25}}], "formatVersion": 3, "id": 4294967096, "name": "pg_description", "nextColumnId": 5, "nextConstraintId": 2, "nextIndexId": 2, "nextMutationId": 1, "primaryIndex": {"constraintId": 1, "foreignKey": {}, "geoConfig": {}, "id": 1, "interleave": {}, "partitioning": {}, "sharded": {}}, "privileges": {"ownerProto": "node", "users": [{"privileges": "32", "userProto": "public"}], "version": 2}, "replacementOf": {"time": {}}, "unexposedParentSchemaId": 4294967118, "version": "1"}}
4294967096 {"table": {"columns": [{"id": 1, "name": "objoid", "nullable": true, "type": {"family": "OidFamily", "oid": 26}}, {"id": 2, "name": "classoid", "nullable": true, "type": {"family": "OidFamily", "oid": 26}}, {"id": 3, "name": "objsubid", "nullable": true, "type": {"family": "IntFamily", "oid": 23, "width": 32}}, {"id": 4, "name": "description", "nullable": true, "type": {"family": "StringFamily", "oid": 25}}], "formatVersion": 3, "id": 4294967096, "indexes": [{"foreignKey": {}, "geoConfig": {}, "id": 2, "interleave": {}, "keyColumnDirections": ["ASC"], "keyColumnIds": [1], "keyColumnNames": ["objoid"], "name": "pg_description_objoid_idx", "partitioning": {}, "sharded": {}, "storeColumnIds": [2, 3, 4], "storeColumnNames": ["classoid", "objsubid", "description"], "version": 3}], "name": "pg_description", "nextColumnId": 5, "nextConstraintId": 2, "nextIndexId": 3, "nextMutationId": 1, "primaryIndex": {"constraintId": 1, "foreignKey": {}, "geoConfig": {}, "id": 1, "interleave": {}, "partitioning": {}, "sharded": {}}, "privileges": {"ownerProto": "node", "users": [{"privileges": "32", "userProto": "public"}], "version": 2}, "replacementOf": {"time": {}}, "unexposedParentSchemaId": 4294967118, "version": "1"}}
4294967097 {"table": {"columns": [{"id": 1, "name": "classid", "nullable": true, "type": {"family": "OidFamily", "oid": 26}}, {"id": 2, "name": "objid", "nullable": true, "type": {"family": "OidFamily", "oid": 26}}, {"id": 3, "name": "objsubid", "nullable": true, "type": {"family": "IntFamily", "oid": 23, "width": 32}}, {"id": 4, "name": "refclassid", "nullable": true, "type": {"family": "OidFamily", "oid": 26}}, {"id": 5, "name": "refobjid", "nullable": true, "type": {"family": "OidFamily", "oid": 26}}, {"id": 6, "name": "refobjsubid", "nullable": true, "type": {"family": "IntFamily", "oid": 23, "width": 32}}, {"id": 7, "name": "deptype", "nullable": true, "type": {"family": "StringFamily", "oid": 18, "visibleType": 9, "width": 1}}], "formatVersion": 3, "id": 4294967097, "name": "pg_depend", "nextColumnId": 8, "nextConstraintId": 2, "nextIndexId": 2, "nextMutationId": 1, "primaryIndex": {"constraintId": 1, "foreignKey": {}, "geoConfig": {}, "id": 1, "interleave": {}, "partitioning": {}, "sharded": {}}, "privileges": {"ownerProto": "node", "users": [{"privileges": "32", "userProto": "public"}], "version": 2}, "replacementOf": {"time": {}}, "unexposedParentSchemaId": 4294967118, "version": "1"}}
4294967098 {"table": {"columns": [{"id": 1, "name": "oid", "nullable": true, "type": {"family": "OidFamily", "oid": 26}}, {"id": 2, "name": "defaclrole", "nullable": true, "type": {"family": "OidFamily", "oid": 26}}, {"id": 3, "name": "defaclnamespace", "nullable": true, "type": {"family": "OidFamily", "oid": 26}}, {"id": 4, "name": "defaclobjtype", "nullable": true, "type": {"family": "StringFamily", "oid": 18, "visibleType": 9, "width": 1}}, {"id": 5, "name": "defaclacl", "nullable": true, "type": {"arrayContents": {"family": "StringFamily", "oid": 25}, "arrayElemType": "StringFamily", "family": "ArrayFamily", "oid": 1009}}], "formatVersion": 3, "id": 4294967098, "name": "pg_default_acl", "nextColumnId": 6, "nextConstraintId": 2, "nextIndexId": 2, "nextMutationId": 1, "primaryIndex": {"constraintId": 1, "foreignKey": {}, "geoConfig": {}, "id": 1, "interleave": {}, "partitioning": {}, "sharded": {}}, "privileges": {"ownerProto": "node", "users": [{"privileges": "32", "userProto": "public"}], "version": 2}, "replacementOf": {"time": {}}, "unexposedParentSchemaId": 4294967118, "version": "1"}}
4294967099 {"table": {"columns": [{"id": 1, "name": "setconfig", "nullable": true, "type": {"arrayContents": {"family": "StringFamily", "oid": 25}, "arrayElemType": "StringFamily", "family": "ArrayFamily", "oid": 1009}}, {"id": 2, "name": "setdatabase", "nullable": true, "type": {"family": "OidFamily", "oid": 26}}, {"id": 3, "name": "setrole", "nullable": true, "type": {"family": "OidFamily", "oid": 26}}], "formatVersion": 3, "id": 4294967099, "name": "pg_db_role_setting", "nextColumnId": 4, "nextConstraintId": 2, "nextIndexId": 2, "nextMutationId": 1, "primaryIndex": {"constraintId": 1, "foreignKey": {}, "geoConfig": {}, "id": 1, "interleave": {}, "partitioning": {}, "sharded": {}}, "privileges": {"ownerProto": "node", "users": [{"privileges": "32", "userProto": "public"}], "version": 2}, "replacementOf": {"time": {}}, "unexposedParentSchemaId": 4294967118, "version": "1"}}
Expand Down
11 changes: 4 additions & 7 deletions pkg/sql/opt/exec/execbuilder/testdata/explain
Original file line number Diff line number Diff line change
Expand Up @@ -436,14 +436,11 @@ vectorized: true
│ │ │ └── • virtual table
│ │ │ table: pg_class@pg_class_oid_idx
│ │ │
│ │ └── • sort
│ │ │ order: +objoid
│ │ └── • filter
│ │ │ filter: (objsubid = 0) AND (classoid = 4294967106)
│ │ │
│ │ └── • filter
│ │ │ filter: (objsubid = 0) AND (classoid = 4294967106)
│ │ │
│ │ └── • virtual table
│ │ table: pg_description@primary
│ │ └── • virtual table
│ │ table: pg_description@pg_description_objoid_idx
│ │
│ └── • virtual table
│ table: table_row_statistics@primary
Expand Down
Loading

0 comments on commit dc93f55

Please sign in to comment.