列出Amazon Redshift中具有索引的列

时间:2015-04-28 22:13:34

标签: postgresql amazon-redshift

我需要查询Redshift元数据以获取表列的列表,其中包含列是否是主键的一部分的信息。

已经有一个帖子List columns with indexes in PostgreSQL有一个PostgreSQL的答案,但不幸的是,它在Redshift上失败了" ERROR:42809:op ANY / ALL(数组)需要右侧数组&#34 ;

2 个答案:

答案 0 :(得分:0)

使用以下查询:

select * from pg_table_def where tablename = 'mytablename'

这将为您提供表的所有列及其数据类型,编码以及是否具有排序键或dist键。

答案 1 :(得分:0)

以下对我有用:

    SELECT   n.nspname as schema_name,
       t.relname as table_name,
       i.relname as index_name,
       c.contype as index_type,
       a.attname as column_name,
       a.attnum AS column_position
    FROM  pg_class t
    INNER JOIN  pg_index      AS ix ON t.oid = ix.indrelid
    INNER JOIN  pg_constraint AS c  ON ix.indrelid = c.conrelid
    INNER JOIN  pg_class      AS i  ON i.oid = ix.indexrelid
    INNER JOIN  pg_attribute  AS a  ON a.attrelid = t.oid
    AND a.attnum= ANY(string_to_array(textin(int2vectorout(ix.indkey)),' ')::int[])
  INNER JOIN  pg_namespace  AS n  ON n.oid = t.relnamespace;