pg_catalog.pg_classテーブルからの導出
pg_catalog.pg_classテーブルを軸に見れば出力できます。ここでのテーブルとは、一般のテーブルと、パーティションテーブル、そして外部テーブル(postgres_fdwモジュールを使い外部テーブルへアクセスする際に作成する)です。
テーブル一覧出力SQL
オブジェクトオーナー(rolname)の部分を適宜変更してお使いください。
SELECT
r.rolname AS owner,
n.nspname AS schema,
c.relname AS relname,
CASE
WHEN c.relkind = 'r' THEN 'table'
WHEN c.relkind = 'p' THEN 'partitioned table'
WHEN c.relkind = 'f' THEN 'foreign table'
ELSE CONCAT(c.relkind, '')
END AS relkind_dec,
CASE
WHEN p.inhparent IS NOT NULL THEN pp.relname
ELSE NULL
END AS partitioned_table, -- パーティションテーブルの場合、親テーブル名を表示
CASE
WHEN c.relpersistence = 'p' THEN '永続'
WHEN c.relpersistence = 'u' THEN 'ログ無し'
WHEN c.relpersistence = 't' THEN '一時'
ELSE CONCAT(c.relpersistence, '')
END AS relpersistence_dec,
pg_catalog.array_to_string(c.relacl, ',') AS relacl -- アクセス権限
FROM
pg_class c
INNER JOIN
pg_namespace n ON c.relnamespace = n.oid
INNER JOIN
pg_roles r ON c.relowner = r.oid
LEFT JOIN
pg_inherits p ON c.oid = p.inhrelid
LEFT JOIN
pg_class pp ON p.inhparent = pp.oid
WHERE
c.relkind IN ('r', 'p', 'f') -- オブジェクトタイプ
and r.rolname ='sooni'
ORDER BY
owner, schema, partitioned_table,relname;
一覧出力例
owner | schema | relname | relkind_dec | partitioned_table | relpersistence_dec | relacl
-------+--------+--------------+-------------------+-------------------+--------------------+-----------------------------------------
sooni | public | ex01 | table | | 永続 |
sooni | public | kudamono | foreign table | | 永続 |
sooni | sooni | aichi | table | address_list | 永続 |
sooni | sooni | chiba | table | address_list | 永続 |
sooni | sooni | fukushima | table | address_list | 永続 |
sooni | sooni | address_list | partitioned table | | 永続 |
sooni | sooni | csv_sjis | foreign table | | 永続 |
sooni | sooni | ex01 | table | | 永続 |
sooni | sooni | ex02 | table | | 永続 | sooni=arwdDxt/sooni,online_user=a/sooni
sooni | sooni | ex400 | table | | 永続 |
sooni | sooni | ex500 | table | | ログ無し | sooni=arwdDxt/sooni,udonman=r/sooni
(11 行)
myposdb=#
外部テーブルの外部サーバまで出力したい場合はinformation_schema.foreign_tablesも結合するとわかります。
テーブル一覧出力(TOASTテーブルも出力する場合)
select
r.rolname as owner
,n.nspname as schema
,c.relname as relname
,case
when c.relkind = 'r' then 'table'
when c.relkind = 'p' then 'partitioned table'
when c.relkind = 'f' then 'foreign table'
when c.relkind = 't' then 'toast table'
else concat(c.relkind, '')
end as relkind_dec
,case
when p.inhparent is not null then pp.relname
else null
end as partitioned_table -- パーティションテーブルの場合、親テーブル名を表示
,case when c.relkind = 't' -- TOASTテーブルの場合メインテーブルを表示
then t.relname
else null
end as main_table
,case
when c.relpersistence = 'p' then '永続'
when c.relpersistence = 'u' then 'ログ無し'
when c.relpersistence = 't' then '一時'
else concat(c.relpersistence, '')
end as relpersistence_dec
,pg_catalog.array_to_string(c.relacl, ',') as relacl -- アクセス権限
from
pg_class c
left outer join
pg_class t on t.reltoastrelid = c.oid
inner join
pg_namespace n on c.relnamespace = n.oid
inner join
pg_roles r on c.relowner = r.oid
left join
pg_inherits p on c.oid = p.inhrelid
left join
pg_class pp on p.inhparent = pp.oid
where
c.relkind in ('r', 'p', 'f','t') -- オブジェクトタイプ
and r.rolname ='sooni'
order by
owner, schema, partitioned_table,relname;
おまけ
psql メタコマンド ¥dt で出力する際も条件指定できます
¥dtに限らず¥dfや¥diなど¥d始まりメタコマンドで利用できます。
-- ワイルドカード使えます
--
myposdb=# \dt sooni.gunma*
List of relations
Schema | Name | Type | Owner
--------+---------------+-------+-------
sooni | gunma | table | sooni
sooni | gunma_address | table | sooni
(2 rows)
-- 正規表現も使えます
--
myposdb=# \dt sooni.(fruit|hokkaido)
List of relations
Schema | Name | Type | Owner
--------+----------+-------+-------
sooni | fruit | table | sooni
sooni | hokkaido | table | sooni
(2 rows)
myposdb=#


