表的分区关系存储在pg_inherits
中,其定义如下:
Table "pg_catalog.pg_inherits"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
-----------+---------+-----------+----------+---------+---------+--------------+-------------
inhrelid | oid | | not null | | plain | | 子表的 OID
inhparent | oid | | not null | | plain | | 父表的 OID
inhseqno | integer | | not null | | plain | | 直接继承多个父表时指定被继承column 的顺序
Indexes:
"pg_inherits_relid_seqno_index" UNIQUE, btree (inhrelid, inhseqno)
"pg_inherits_parent_index" btree (inhparent)
inhrelid
和inhparent
都是 pg_class
表中的隐藏列 oid
。
那么查询 parent_table_name 的分区表名字的SQL
如下:
select
c.relname
from
pg_class c
join pg_inherits i on i.inhrelid = c. oid
join pg_class d on d.oid = i.inhparent
where
d.relname = 'parent_table_name';
或者使用下面的SQL
:
SELECT
relname
from
pg_class
where
oid in (
SELECT inhrelid FROM pg_inherits WHERE inhparent in (
SELECT oid FROM pg_class WHERE relname = 'parent_table_name'
)
)