Skip to content

sql: pg_catalog.pg_statistic_ext is incompletely defined #88108

@knz

Description

@knz

Found while working on #88061

Describe the problem

The vtable pg_statistic_ext is not empty, but the data populated inside it is unusable.

  • stxnamespace is missing.
  • stxkind is missing.
  • stxstattarget is missing (could be -1)

To Reproduce

The following SQL query should report the statistics for a table:

WITH stat AS (
SELECT oid,
       stxrelid::pg_catalog.regclass AS tb,
       stxnamespace::pg_catalog.regnamespace AS nsp,
       stxname,
       pg_get_statisticsobjdef_columns(oid) AS columns,
       'd' = any(stxkind) AS hasndist,
       'f' = any(stxkind) AS hasdeps,
       'm' = any(stxkind) AS hasmcv,
       stxstattarget
  FROM pg_catalog.pg_statistic_ext stat
 WHERE stxrelid = %[1]s)

  SELECT '"'||nsp||'.'||stxname||'"'||
         IF((hasndist OR hasdeps OR hasmcv) AND NOT (hasndist AND hasdeps AND hasmcv),
            '('||
            IF(hasndist,
               'ndistinct' || IF(hasdeps OR hasmcv, ', ', ''),
               '')||
            IF(hasdeps, 'dependencies' || IF(hasmcv, ', ', ''), '')||
            IF(hasmcv, 'mcv', '')||
            ')',
           '')||
         ' ON ' || columns || ' FROM ' || tb ||
         IF(stxstattarget <> -1 AND stxstattarget IS NOT NULL,
            '; STATISTICS ' || stxstattarget::STRING, '')
         AS "Statistics objects"
    FROM stat
ORDER BY stat.oid

Alternatively, instead of pg_get_statisticsobjdef_columns the following should also work:

                    (SELECT pg_catalog.string_agg(pg_catalog.quote_ident(attname),', ')
                  FROM pg_catalog.unnest(stxkeys) s(attnum)
                JOIN pg_catalog.pg_attribute a ON (stxrelid = a.attrelid AND
                a.attnum = s.attnum AND NOT attisdropped)) AS columns

cc @rafiss for triage

Jira issue: CRDB-19692

Epic: CRDB-23454

Metadata

Metadata

Assignees

Labels

A-sql-pgcatalogA-sql-pgcompatSemantic compatibility with PostgreSQLA-sql-vtablesVirtual tables - pg_catalog, information_schema etcC-bugCode not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior.T-sql-foundationsSQL Foundations Team (formerly SQL Schema + SQL Sessions)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions