はじめに
現在担当しているPJTでpostgreSQLのユーザ定義の型なるものを色々作成していたのですが、
利用しているクライアントツール(A5:SQL Mk-2)だと画面から作成したユーザ定義型の一覧がわからなかったため、抽出用のクエリを作成しました。
※補足
DBeaverだと「スキーマ名」>「データ型」から作成したユーザ定義型の一覧が確認できるようです。
検証用データ
createtypeサンプル
-- 複合型のユーザ定義
create type PLPGSQL.DATA_TYPE1 as (
param1 numeric(1,0)
,param2 text
,param3 bytea
);
-- 列挙型のユーザ定義
create type PLPGSQL.DATA_TYPE2 as enum (
'spade'
,'club'
,'diamond'
,'heart'
);
ユーザ定義型の一覧取得SQL
ユーザ定義型の一覧取得SQL
select
t1.typname
, case
when t1.typcategory = 'C' then '複合型'
when t1.typcategory = 'E' then '列挙型'
else 'other'
end
from
pg_catalog.pg_type t1
left outer join pg_catalog.pg_namespace t2
on t1.typnamespace = t2.oid
left outer join pg_catalog.pg_tables t3
on t1.typname = t3.tablename
left outer join pg_catalog.pg_sequences t4
on t1.typname = t4.sequencename
where t2.nspname = 'plpgsql' -- スキーマ名を指定
and ( t1.typcategory = 'C' or t1.typcategory = 'E' )
and t3.tablename is null
and t4.sequencename is null
order by t1.typname;
実行結果
実行結果
typname | case
------------+--------
data_type1 | 複合型
data_type2 | 列挙型
参考
postgreSQLの内部情報を調べる際はシステムカタログに大変お世話になります・・!