diff --git a/pkg/sql/create_stats.go b/pkg/sql/create_stats.go index eba98263e9bd..f6a63e209415 100644 --- a/pkg/sql/create_stats.go +++ b/pkg/sql/create_stats.go @@ -410,7 +410,12 @@ func createStatsDefaultColumns( // Generate stats for each column individually. for _, colID := range colIDs.Ordered() { - addIndexColumnStatsIfNotExists(colID, isInverted) + col, err := desc.FindColumnByID(colID) + if err != nil { + return nil, err + } + isInvertedCol := colinfo.ColumnTypeIsInvertedIndexable(col.Type) + addIndexColumnStatsIfNotExists(colID, isInvertedCol) } } } diff --git a/pkg/sql/logictest/testdata/logic_test/distsql_stats b/pkg/sql/logictest/testdata/logic_test/distsql_stats index 9031520dc5bd..5323345b00c6 100644 --- a/pkg/sql/logictest/testdata/logic_test/distsql_stats +++ b/pkg/sql/logictest/testdata/logic_test/distsql_stats @@ -1036,3 +1036,14 @@ SHOW STATISTICS USING JSON FOR TABLE greeting_stats statement ok ALTER TABLE greeting_stats INJECT STATISTICS '$stats' + +# Regression test for #63387. Stats collection should succeed when partial index +# predicates reference inverted-type columns. +statement ok +CREATE TABLE t63387 ( + i INT, + j JSONB, + INDEX (i) WHERE j->>'a' = 'b' +); +INSERT INTO t63387 VALUES (1, '{}'); +CREATE STATISTICS s FROM t63387;