\dTコマンドでデータ型一覧を確認する
PostgreSQLではCREATE TYPEで列挙型(ENUM)や複合型を独自に定義できる。作成済みのデータ型一覧を確認するには、psqlコマンドラインツールの\dTコマンドを使う。
testdb=# \dT public.*
List of data types
Schema | Name | Description
--------+--------------+-------------
public | positive_int |
public | status |
(2 rows)
\dTは自作の型(この例ではドメインpositive_intとENUMstatus)だけを表示し、テーブルごとに自動生成される行型や配列型は表示されない。
\dT+でENUMの選択肢も表示する
+を付けると、内部名やサイズに加えてENUM型の場合は選択肢の一覧も表示される。
testdb=# \dT+ status
List of data types
Schema | Name | Internal name | Size | Elements | Owner | Access privileges | Description
--------+--------+---------------+------+----------+-------+-------------------+-------------
public | status | status | 4 | pending +| postgres | |
| | | | shipped +| | |
| | | | done | | |
(1 row)
Elements列にENUMの選択肢が改行区切りで表示される。CREATE TYPE ... AS ENUMで定義した順序がそのまま比較順序になる。
\dDコマンドでドメイン一覧を確認する
ドメインは既存の型に制約を追加した別名の型である。ドメインだけを確認したい場合は\dDを使う。
testdb=# \dD positive_int
List of domains
Schema | Name | Type | Collation | Nullable | Default | Check
--------+--------------+---------+-----------+----------+---------+-------------------
public | positive_int | integer | | | | CHECK (VALUE > 0)
(1 row)
positive_intはinteger型にCHECK (VALUE > 0)という制約を加えたドメインである。列の型にドメインを指定すると、テーブル定義のたびに同じCHECK制約を書く手間を省ける。
information_schema.domainsでドメイン一覧を確認する
SQLでドメイン一覧を取得する場合はinformation_schema.domainsを参照する。
testdb=# SELECT domain_name, data_type, domain_default
testdb-# FROM information_schema.domains
testdb-# WHERE domain_schema = 'public';
domain_name | data_type | domain_default
--------------+-----------+----------------
positive_int | integer |
(1 row)
CHECK制約の内容自体はinformation_schema.domainsには含まれない。制約の内容まで確認したい場合は\dDか、information_schema.domain_constraintsとinformation_schema.check_constraintsを結合して調べる。
\dCコマンドで型キャスト一覧を確認する
異なる型同士を暗黙的または明示的に変換できるかどうかは\dCで確認できる。
testdb=# \dC int4
List of casts
Source type | Target type | Function | Implicit?
------------------+------------------+--------------------+---------------
"char" | integer | int4 | no
bigint | integer | int4 | in assignment
...
integer | numeric | numeric | yes
integer | oid | (binary coercible) | yes
...
(42 rows)
パターンは1つだけ指定でき、変換元・変換先どちらかの型名にマッチする行がすべて表示される。第2引数を指定してもエラーにはならず単に無視される。
testdb=# \dC int4 numeric
\dC: extra argument "numeric" ignored
Implicit?列がyesの場合、SELECT integer_col + numeric_colのように型を明示的にキャストしなくても自動的に変換される。noの場合はCAST(値 AS 型)または値::型で明示的に変換する必要がある。
\dT・\dD・\dCが内部的に参照するカタログ
これら3つのメタコマンドは、いずれもpg_typeまたはpg_castシステムカタログを参照している。
testdb=# SELECT t.typname, t.typtype
testdb-# FROM pg_type t
testdb-# JOIN pg_namespace n ON n.oid = t.typnamespace
testdb-# WHERE n.nspname = 'public'
testdb-# ORDER BY t.typname;
typname | typtype
----------------+---------
_docs | b
_positive_int | b
_status | b
...
docs | c
positive_int | d
status | e
users | c
(18 rows)
typtypeはbが基本型・配列型、cがテーブルやビューに対応する複合型(行型)、dがドメイン、eがENUM型を表す。\dTはこのうちc(テーブルの行型)と_から始まる配列型を除外して表示している。pg_typeを直接調べる場合は、こうした自動生成された型が混ざる点に注意する。
参考
- PostgreSQL Documentation: pg_type
- PostgreSQL Documentation: pg_cast
- PostgreSQL Documentation: CREATE DOMAIN
