postgresql 如何在PSQL中查找从另一个表继承的子表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1461722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 23:52:03  来源:igfitidea点击:

How to find child tables that inherit from another table in PSQL

postgresql

提问by Watusimoto

In PSQL, is there a good way of finding all the tables that inherit from another table? Ideally, I could get the data from a SQL query, but at this point, I'd be happy with any reliable method.

在 PSQL 中,是否有一种查找从另一个表继承的所有表的好方法?理想情况下,我可以从 SQL 查询中获取数据,但此时,我对任何可靠的方法都感到满意。

回答by Michael Krelin - hacker

What do you mean "from sql query"? Does it mean SELECTing from pg_inheritsis not good enough for you?

你是什​​么意思“从sql查询”?这是否意味着SELECTing frompg_inherits对你来说不够好?

SELECT pg_inherits.*, c.relname AS child, p.relname AS parent
FROM
    pg_inherits JOIN pg_class AS c ON (inhrelid=c.oid)
    JOIN pg_class as p ON (inhparent=p.oid);

回答by Jeroen

If you also need the schema names:

如果您还需要架构名称:

SELECT cn.nspname AS schema_child, c.relname AS child, pn.nspname AS schema_parent, p.relname AS parent
FROM pg_inherits 
JOIN pg_class AS c ON (inhrelid=c.oid)
JOIN pg_class as p ON (inhparent=p.oid)
JOIN pg_namespace pn ON pn.oid = p.relnamespace
JOIN pg_namespace cn ON cn.oid = c.relnamespace
WHERE p.relname = 'your table name' and pn.nspname = 'your schema name'

回答by user3612491

If you want to find all child's from the master partition you can simply execute it like:

如果您想从主分区中找到所有子分区,您可以简单地执行它,如下所示:

SELECT relname
FROM pg_class,pg_inherits
WHERE pg_class.oid=pg_inherits.inhrelid
AND inhparent
IN (SELECT oid FROM pg_class WHERE relname='your_master_partition')
ORDER BY relname;