Skip to content

Commit

Permalink
Merge #93274
Browse files Browse the repository at this point in the history
93274: sql: Define more columns in `pg_catalog.pg_statistic_ext` r=rafiss a=e-mbrown

Fixes: #88108
Fixes: #93430

This commit makes sure the `stxnamespace`, `stxkind` and `stxstattarget` columns are defined in `pg_statistics_ext`.

Release note: The `stxnamespace`, `stxkind` and
`stxstattarget` columns are now defined in
`pg_statistics_ext`.  Also `pg_statistics_ext` 
no longer crashes when stxname is null.

Co-authored-by: e-mbrown <[email protected]>
  • Loading branch information
craig[bot] and e-mbrown committed Dec 20, 2022
2 parents 9890516 + 5fb4c60 commit 56d249d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
15 changes: 13 additions & 2 deletions pkg/sql/logictest/testdata/logic_test/pg_catalog
Original file line number Diff line number Diff line change
Expand Up @@ -4382,7 +4382,14 @@ 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;
CREATE TABLE stx(stx) AS SELECT generate_series(1,100);
ANALYZE stx;

query TTOOITT colnames
SELECT
relname,
stxname,
Expand All @@ -4395,7 +4402,10 @@ 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}
stx NULL 105 NULL -1 {2} {d}
stx NULL 105 NULL -1 {1} {d}

# Test that pg_shadow doesn't include roles that can't login
query B colnames
Expand Down Expand Up @@ -4775,3 +4785,4 @@ WHERE
AND t.typname LIKE 'myt%';
----
0

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 @@ -3529,35 +3530,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 56d249d

Please sign in to comment.