Skip to content

Commit

Permalink
sql: Define more columns in pg_catalog.pg_statistic_ext
Browse files Browse the repository at this point in the history
`pg_catalog.pg_statistic_ext` is now
populated with more data.

Release note (bug fix): The `stxnamespace`, `stxkind` and
`stxstattarget` columns are now defined in
`pg_statistics_ext`.
  • Loading branch information
e-mbrown committed Dec 12, 2022
1 parent 30fce41 commit f07577c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
11 changes: 9 additions & 2 deletions pkg/sql/logictest/testdata/logic_test/pg_catalog
Original file line number Diff line number Diff line change
Expand Up @@ -4371,7 +4371,13 @@ statement ok
CREATE TABLE stxtbl(a INT, b INT, c INT);
CREATE STATISTICS stxobj ON b, c FROM stxtbl;

query TTOOTTT colnames
statement ok
CREATE SCHEMA test;
CREATE TABLE test.stxtbl2(a INT, b INT, c INT);
CREATE STATISTICS stxobj2 ON a, c FROM test.stxtbl2;


query TTOOITT colnames
SELECT
relname,
stxname,
Expand All @@ -4384,7 +4390,8 @@ FROM pg_statistic_ext
JOIN pg_class ON pg_statistic_ext.stxrelid = pg_class.oid
----
relname stxname stxnamespace stxowner stxstattarget stxkeys stxkind
stxtbl stxobj NULL NULL NULL {2,3} NULL
stxtbl stxobj 105 NULL -1 {2,3} {d}
stxtbl2 stxobj2 189 NULL -1 {1,3} {d}

# Test that pg_shadow doesn't include roles that can't login
query B colnames
Expand Down
34 changes: 24 additions & 10 deletions pkg/sql/pg_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,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/descs"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/schemadesc"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/schemaexpr"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
Expand Down Expand Up @@ -3506,35 +3507,48 @@ var pgCatalogStatisticExtTable = virtualSchemaTable{
comment: `pg_statistic_ext has the statistics objects created with CREATE STATISTICS
https://www.postgresql.org/docs/13/catalog-pg-statistic-ext.html`,
schema: vtable.PgCatalogStatisticExt,
populate: func(ctx context.Context, p *planner, _ catalog.DatabaseDescriptor, addRow func(...tree.Datum) error) error {
query := `SELECT "statisticID", name, "tableID", "columnIDs" FROM system.table_statistics;`
populate: func(ctx context.Context, p *planner, db catalog.DatabaseDescriptor, addRow func(...tree.Datum) error) error {

// '{d}' refers to Postgres code for n-distinct statistics or multi-column
// statistics.
query := `SELECT "tableID", name, "columnIDs", "statisticID", '{d}'::"char"[] FROM system.table_statistics;`

rows, err := p.ExtendedEvalContext().ExecCfg.InternalExecutor.QueryBuffered(
ctx, "read-statistics-objects", p.txn, query,
)
if err != nil {
return err
}
h := makeOidHasher()
statTgt := tree.NewDInt(-1)

for _, row := range rows {
statisticsID := tree.MustBeDInt(row[0])
name := tree.MustBeDString(row[1])
tableID := tree.MustBeDInt(row[2])
columnIDs := tree.MustBeDArray(row[3])
tableID := tree.MustBeDInt(row[0])
columnIDs := tree.MustBeDArray(row[2])
statisticsID := tree.MustBeDInt(row[3])
statisticsKind := tree.MustBeDArray(row[4])

// The statisticsID is generated from unique_rowid() so it won't fit in a
// uint32.
h.writeUInt64(uint64(statisticsID))
statisticsOID := h.getOid()

tn, err := descs.GetTableNameByID(ctx, p.Txn(), p.descCollection, descpb.ID(tableID))
if err != nil {
return err
}

statSchema := db.GetSchemaID(tn.Schema())

if err := addRow(
statisticsOID, // oid
tableOid(descpb.ID(tableID)), // stxrelid
&name, // stxname
tree.DNull, // stxnamespace
row[1], // stxname
schemaOid(statSchema), // stxnamespace
tree.DNull, // stxowner
tree.DNull, // stxstattarget
statTgt, // stxstattarget
columnIDs, // stxkeys
tree.DNull, // stxkind
statisticsKind, // stxkind
); err != nil {
return err
}
Expand Down

0 comments on commit f07577c

Please sign in to comment.