Skip to content

Commit

Permalink
make pg_table_metadata CockroachDB compatible
Browse files Browse the repository at this point in the history
CockroachDB doesn't support `index` as a table alias, but PostgreSQL
does. Since `idx` is supported by both, change the metadata query
accordingly.

Refs cockroachdb/cockroach#72407
  • Loading branch information
otan committed Nov 8, 2021
1 parent f15b595 commit 84dbcec
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions server/src-rsr/pg_table_metadata.sql
Original file line number Diff line number Diff line change
Expand Up @@ -59,28 +59,28 @@ LEFT JOIN LATERAL
'constraint', jsonb_build_object('name', class.relname, 'oid', class.oid :: integer),
'columns', coalesce(columns.info, '[]')
) AS info
FROM pg_catalog.pg_index index
FROM pg_catalog.pg_index idx
JOIN pg_catalog.pg_class class
ON class.oid = index.indexrelid
ON class.oid = idx.indexrelid
LEFT JOIN LATERAL
( SELECT jsonb_agg("column".attname) AS info
FROM pg_catalog.pg_attribute "column"
WHERE "column".attrelid = "table".oid
AND "column".attnum = ANY (index.indkey)
AND "column".attnum = ANY (idx.indkey)
) AS columns ON true
WHERE index.indrelid = "table".oid
AND index.indisprimary
WHERE idx.indrelid = "table".oid
AND idx.indisprimary
) primary_key ON true

-- unique constraints
LEFT JOIN LATERAL
( SELECT jsonb_agg(jsonb_build_object('name', class.relname, 'oid', class.oid :: integer)) AS info
FROM pg_catalog.pg_index index
FROM pg_catalog.pg_index idx
JOIN pg_catalog.pg_class class
ON class.oid = index.indexrelid
WHERE index.indrelid = "table".oid
AND index.indisunique
AND NOT index.indisprimary
ON class.oid = idx.indexrelid
WHERE idx.indrelid = "table".oid
AND idx.indisunique
AND NOT idx.indisprimary
) unique_constraints ON true

-- foreign keys
Expand Down

0 comments on commit 84dbcec

Please sign in to comment.