1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PostgreSQL オブジェクト識別子データ型

Last updated at Posted at 2024-11-01

システムカタログで管理されているオブジェクトはidで不便

特にpg_classですが、列にrelownerrelnamespaceというカラムありますが、idなので
確認するときは、わざわざ別カタログから名称を取得してました。。

こんな感じで

select 
    r.rolname as owner,     -- オーナ名
    n.nspname as schema,    -- スキーマ名
    c.relname as name       
from pg_class c
    inner join pg_roles r on r.oid = c.relowner
    inner join pg_namespace n on n.oid = c.relnamespace
where
    r.rolname = 'postgres'
    and c.relkind = 'r'

これを「オブジェクト識別子データ型」を使うとこうなります。

select
    c.relowner::regrole as owner,               -- オーナ名
    c.relnamespace::regnamespace as schema,     -- スキーマ名
    c.relname as name 
from pg_class c
where
    c.relowner = 'postgres'::regrole
    and c.relkind = 'r'

めちゃくちゃスッキリした!!

以上です。

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?