Skip to content

sql: column indnkeyatts in pg_index does not report key columns properly in primary key #88106

@knz

Description

@knz

Needed for #88061.

Describe the problem

CockroachDB does not include primary key columns in the indnkeyatts attribute of pg_index.

To Reproduce

Initialize with:

create table mytable(x int, y int);

Then use the following query (replace the 3 occurrences of XXXX by mytables' primary index OID):

WITH obj AS (
   SELECT c.oid, c.relchecks, c.relkind, c.relhasindex, c.relhasrules,
          c.relhastriggers, c.relrowsecurity, c.relforcerowsecurity,
          false AS relhasoids, c.relispartition, c.reltablespace,
          CASE
          WHEN c.reloftype = 0 THEN ''
          ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END AS reloftype,
          c.relpersistence, c.relreplident, am.amname
     FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)
LEFT JOIN pg_catalog.pg_am am    ON (c.relam = am.oid)
    WHERE c.oid = XXXXXXX), cols AS (
 SELECT a.attname,
        pg_catalog.format_type(a.atttypid, a.atttypmod) AS typname,
 CASE WHEN a.attnum <= (
  SELECT i.indnkeyatts
    FROM pg_catalog.pg_index i
   WHERE i.indexrelid = XXXXXXX) THEN 'yes' ELSE 'no' END AS is_key,
         pg_catalog.pg_get_indexdef(a.attrelid, a.attnum, TRUE) AS indexdef
    FROM pg_catalog.pg_attribute a
   WHERE a.attrelid = XXXXXXX AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum)
SELECT attname AS "Column",
       typname AS "Type",
       is_key AS "Key?",
       indexdef AS "Definition"
  FROM cols

(observe, the column rowid is not marked as Key because indnkeyatts is zero)

cc @rafiss for triage.

Jira issue: CRDB-19690

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