0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【postgreSQL】ユーザ定義型の一覧を取得するSQL

Last updated at Posted at 2021-12-18

はじめに

現在担当しているPJTでpostgreSQLのユーザ定義の型なるものを色々作成していたのですが、
利用しているクライアントツール(A5:SQL Mk-2)だと画面から作成したユーザ定義型の一覧がわからなかったため、抽出用のクエリを作成しました。

※補足
DBeaverだと「スキーマ名」>「データ型」から作成したユーザ定義型の一覧が確認できるようです。
スクリーンショット 2021-12-18 16.49.40.png

検証用データ

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の内部情報を調べる際はシステムカタログに大変お世話になります・・!

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?