查找从主表“继承”的所有分区表

时间:2015-10-15 18:31:07

标签: postgresql reverse-engineering partitioning

假设我有一个表“foo”,分区表“foo1”,“foo2”和“foo3”。但是目前我所知道的是有一些继承自表“foo”的分区表。我如何才能发现foo有3个分区,foo1,foo2和foo3?

4 个答案:

答案 0 :(得分:2)

使用pg_inherits。例如:

create table my_parent_table (id int);
create table my_child_table_no_1 (check (id < 10)) inherits (my_parent_table);
create table my_child_table_no_2 (check (id >= 10)) inherits (my_parent_table);

select relname
from pg_inherits i
join pg_class c on c.oid = inhrelid
where inhparent = 'my_parent_table'::regclass

       relname       
---------------------
 my_child_table_no_1
 my_child_table_no_2
(2 rows)    

您还可以使用pg_constraint选择检查约束:

select relname "child table", consrc "check"
from pg_inherits i
join pg_class c on c.oid = inhrelid
join pg_constraint on c.oid = conrelid
where contype = 'c'
and inhparent = 'my_parent_table'::regclass

     child table     |   check    
---------------------+------------
 my_child_table_no_1 | (id < 10)
 my_child_table_no_2 | (id >= 10)
(2 rows)

答案 1 :(得分:1)

要列出所有分区(子表)-经过PG v9-v13测试:

SELECT c.relname FROM pg_inherits i JOIN pg_class p ON i.inhparent = p.oid
JOIN pg_class c ON i.inhrelid = c.oid WHERE p.relname='parentTableName';

答案 2 :(得分:0)

我深入研究这些目录信息的方法是使用psql。使用-eE选项启动psql:

psql -eE <your database>

然后显示你的表格:

\d <your table>

这将列出psql生成的所有查询以从目录中获取信息。这包括继承的表。

请记住,目录可能会从一个主要版本更改为另一个主要版本 - 尽管这种基本功能不太可能。

答案 3 :(得分:0)

从 Postgres 12 开始,有一个 built-in function

select *
from  pg_partition_tree('the_table'::regclass)
where parentrelid is not null;