表的分区关系存储在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'
)
)
本文介绍如何通过查询 PostgreSQL 的 pg_inherits 表来获取分区表的名字。提供了两种 SQL 查询方式,一种是通过 JOIN 操作来直接查询分区表名,另一种是通过子查询获取分区表的 OID,再进一步查询表名。
1694

被折叠的 条评论
为什么被折叠?



