ablog

不器用で落着きのない技術者のメモ

Redshift で pg_table_def に問い合わせてテーブルのソートキーを調べる

Redshift で pg_table_def に問い合わせるとテーブルのソートキーを調べることができる。
調べたいスキーマを set search_path to <スキーマ名>; で指定してやる必要がある。スキーマ一覧は psql なら \dn でリストを表示できる。

クエリ

\pset pager
set search_path to '$user', public, schema1, schema2;

select * from pg_table_def
where sortkey <> 0
and schemaname  not in ('pg_catalog')
order by schemaname, tablename, sortkey;

結果

 schemaname |   tablename    |  column  |          type          | encoding | distkey | sortkey | notnull
------------+----------------+----------+------------------------+----------+---------+---------+---------
 public     | table1 | url  | character varying(768) | none     | f       |       1 | f
 public     | table1 | device   | character varying(60)  | none     | f       |       2 | f
 public     | table1 | log_date | date                   | none     | f       |       3 | t
 public     | table1 | uid     | character varying(78)  | none     | f       |       1 | f
 public     | table2 | url  | character varying(768) | none     | f       |       1 | f
 public     | table2 | device   | character varying(60)  | none     | f       |       2 | f
 public     | table2 | log_date | date                   | none     | f       |       3 | t
(7 rows)