Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
43715: sql: pg_catalog.pg_am needs to be populated r=rafiss a=jasobrown

This patch adds inverted index to the access methods listed
in pg_am.

Additionally, this patch sets pg_class.relam to zero for sequences and views,
which is more consistent with postgres.

Fixes cockroachdb#27766

Release note (sql change): more accurate pg_catalog access method information.

Co-authored-by: Jason Brown <[email protected]>
  • Loading branch information
craig[bot] and jasobrown committed Jan 28, 2020
2 parents 6b3809f + 769cc47 commit 20908ab
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 24 deletions.
7 changes: 4 additions & 3 deletions pkg/sql/logictest/testdata/logic_test/pg_catalog
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ oid relname relnamespace reltype reloftype relowner relam
57 t3 2332901747 0 0 NULL 2631952481 0 0
969972501 primary 2332901747 0 0 NULL 2631952481 0 0
969972502 t3_a_b_idx 2332901747 0 0 NULL 2631952481 0 0
58 v1 2332901747 0 0 NULL 2631952481 0 0
58 v1 2332901747 0 0 NULL 0 0 0

query TIRIOBBT colnames
SELECT relname, relpages, reltuples, relallvisible, reltoastrelid, relhasindex, relisshared, relpersistence
Expand Down Expand Up @@ -534,8 +534,9 @@ query OTIIBBBBBBBBBBBOOOOOOOOOOOOOOOOOT colnames
SELECT *
FROM pg_catalog.pg_am
----
oid amname amstrategies amsupport amcanorder amcanorderbyop amcanbackward amcanunique amcanmulticol amoptionalkey amsearcharray amsearchnulls amstorage amclusterable ampredlocks amkeytype aminsert ambeginscan amgettuple amgetbitmap amrescan amendscan ammarkpos amrestrpos ambuild ambuildempty ambulkdelete amvacuumcleanup amcanreturn amcostestimate amoptions amhandler amtype
2631952481 prefix 0 0 true false true true true true true true false false false 0 NULL NULL 0 0 NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL i
oid amname amstrategies amsupport amcanorder amcanorderbyop amcanbackward amcanunique amcanmulticol amoptionalkey amsearcharray amsearchnulls amstorage amclusterable ampredlocks amkeytype aminsert ambeginscan amgettuple amgetbitmap amrescan amendscan ammarkpos amrestrpos ambuild ambuildempty ambulkdelete amvacuumcleanup amcanreturn amcostestimate amoptions amhandler amtype
2631952481 prefix 0 0 true false true true true true true true false false false 0 NULL NULL 0 0 NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL i
4004609370 inverted 0 0 false false false false false false false true false false false 0 NULL NULL 0 0 NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL i

## pg_catalog.pg_attrdef

Expand Down
96 changes: 75 additions & 21 deletions pkg/sql/pg_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ var (
)

const (
cockroachIndexEncoding = "prefix"
indexTypeForwardIndex = "prefix"
indexTypeInvertedIndex = "inverted"
defaultCollationTag = "en-US"
)

Expand All @@ -58,13 +59,8 @@ const (
indoptionNullsFirst = 0x02
)

var cockroachIndexEncodingOid *tree.DOid

func init() {
h := makeOidHasher()
h.writeStr(cockroachIndexEncoding)
cockroachIndexEncodingOid = h.getOid()
}
var forwardIndexOid = stringOid(indexTypeForwardIndex)
var invertedIndexOid = stringOid(indexTypeInvertedIndex)

// pgCatalog contains a set of system tables mirroring PostgreSQL's pg_catalog schema.
// This code attempts to comply as closely as possible to the system catalogs documented
Expand Down Expand Up @@ -299,18 +295,58 @@ CREATE TABLE pg_catalog.pg_am (
amtype CHAR
)`,
populate: func(_ context.Context, p *planner, _ *DatabaseDescriptor, addRow func(...tree.Datum) error) error {
return addRow(
cockroachIndexEncodingOid, // oid - all versions
tree.NewDName(cockroachIndexEncoding), // amname - all versions
// add row for forward indexes
if err := addRow(
forwardIndexOid, // oid - all versions
tree.NewDName(indexTypeForwardIndex), // amname - all versions
zeroVal, // amstrategies - < v9.6
zeroVal, // amsupport - < v9.6
tree.DBoolTrue, // amcanorder - < v9.6
tree.DBoolFalse, // amcanorderbyop - < v9.6
tree.DBoolTrue, // amcanbackward - < v9.6
tree.DBoolTrue, // amcanunique - < v9.6
tree.DBoolTrue, // amcanmulticol - < v9.6
tree.DBoolTrue, // amoptionalkey - < v9.6
tree.DBoolTrue, // amsearcharray - < v9.6
tree.DBoolTrue, // amsearchnulls - < v9.6
tree.DBoolFalse, // amstorage - < v9.6
tree.DBoolFalse, // amclusterable - < v9.6
tree.DBoolFalse, // ampredlocks - < v9.6
oidZero, // amkeytype - < v9.6
tree.DNull, // aminsert - < v9.6
tree.DNull, // ambeginscan - < v9.6
oidZero, // amgettuple - < v9.6
oidZero, // amgetbitmap - < v9.6
tree.DNull, // amrescan - < v9.6
tree.DNull, // amendscan - < v9.6
tree.DNull, // ammarkpos - < v9.6
tree.DNull, // amrestrpos - < v9.6
tree.DNull, // ambuild - < v9.6
tree.DNull, // ambuildempty - < v9.6
tree.DNull, // ambulkdelete - < v9.6
tree.DNull, // amvacuumcleanup - < v9.6
tree.DNull, // amcanreturn - < v9.6
tree.DNull, // amcostestimate - < v9.6
tree.DNull, // amoptions - < v9.6
tree.DNull, // amhandler - > v9.6
tree.NewDString("i"), // amtype - > v9.6
); err != nil {
return err
}

// add row for inverted indexes
if err := addRow(
invertedIndexOid, // oid - all versions
tree.NewDName(indexTypeInvertedIndex), // amname - all versions
zeroVal, // amstrategies - < v9.6
zeroVal, // amsupport - < v9.6
tree.DBoolTrue, // amcanorder - < v9.6
tree.DBoolFalse, // amcanorder - < v9.6
tree.DBoolFalse, // amcanorderbyop - < v9.6
tree.DBoolTrue, // amcanbackward - < v9.6
tree.DBoolTrue, // amcanunique - < v9.6
tree.DBoolTrue, // amcanmulticol - < v9.6
tree.DBoolTrue, // amoptionalkey - < v9.6
tree.DBoolTrue, // amsearcharray - < v9.6
tree.DBoolFalse, // amcanbackward - < v9.6
tree.DBoolFalse, // amcanunique - < v9.6
tree.DBoolFalse, // amcanmulticol - < v9.6
tree.DBoolFalse, // amoptionalkey - < v9.6
tree.DBoolFalse, // amsearcharray - < v9.6
tree.DBoolTrue, // amsearchnulls - < v9.6
tree.DBoolFalse, // amstorage - < v9.6
tree.DBoolFalse, // amclusterable - < v9.6
Expand All @@ -333,7 +369,10 @@ CREATE TABLE pg_catalog.pg_am (
tree.DNull, // amoptions - < v9.6
tree.DNull, // amhandler - > v9.6
tree.NewDString("i"), // amtype - > v9.6
)
); err != nil {
return err
}
return nil
},
}

Expand Down Expand Up @@ -626,13 +665,17 @@ CREATE TABLE pg_catalog.pg_class (
h := makeOidHasher()
return forEachTableDesc(ctx, p, dbContext, virtualMany,
func(db *sqlbase.DatabaseDescriptor, scName string, table *sqlbase.TableDescriptor) error {
// The only difference between tables, views and sequences is the relkind column.
// The only difference between tables, views and sequences are the relkind and relam columns.
relKind := relKindTable
relAm := forwardIndexOid
if table.IsView() {
relKind = relKindView
relAm = oidZero
} else if table.IsSequence() {
relKind = relKindSequence
relAm = oidZero
}

namespaceOid := h.NamespaceOid(db, scName)
if err := addRow(
defaultOid(table.ID), // oid
Expand All @@ -641,7 +684,7 @@ CREATE TABLE pg_catalog.pg_class (
oidZero, // reltype (PG creates a composite type in pg_type for each table)
oidZero, // reloftype (PG creates a composite type in pg_type for each table)
tree.DNull, // relowner
cockroachIndexEncodingOid, // relam
relAm, // relam
oidZero, // relfilenode
oidZero, // reltablespace
tree.DNull, // relpages
Expand Down Expand Up @@ -676,14 +719,19 @@ CREATE TABLE pg_catalog.pg_class (

// Indexes.
return forEachIndexInTable(table, func(index *sqlbase.IndexDescriptor) error {
indexType := forwardIndexOid
if index.Type == sqlbase.IndexDescriptor_INVERTED {
indexType = invertedIndexOid
}

return addRow(
h.IndexOid(table.ID, index.ID), // oid
tree.NewDName(index.Name), // relname
namespaceOid, // relnamespace
oidZero, // reltype
oidZero, // reloftype
tree.DNull, // relowner
cockroachIndexEncodingOid, // relam
indexType, // relam
oidZero, // relfilenode
oidZero, // reltablespace
tree.DNull, // relpages
Expand Down Expand Up @@ -2988,3 +3036,9 @@ func (h oidHasher) OperatorOid(name string, leftType, rightType, returnType *tre
func defaultOid(id sqlbase.ID) *tree.DOid {
return tree.NewDOid(tree.DInt(id))
}

func stringOid(s string) *tree.DOid {
h := makeOidHasher()
h.writeStr(s)
return h.getOid()
}

0 comments on commit 20908ab

Please sign in to comment.