postgresql - 仅显示没有子分区的表列表

标签 postgresql psql partitioning

我只想显示 PostgreSQL 中没有子分区表的顶级表列表。 (目前使用 PostgreSQL 12。)

\dt 在 psql 中给我所有的表,包括表的分区。我看到了这个:

postgres=# \dt

                         List of relations

 Schema |             Name             |       Type        | Owner  
--------+------------------------------+-------------------+--------  
public  | tablea                       | table             | me
public  | partitionedtable1            | partitioned table | me
public  | partitionedtable1_part1      | table             | me
public  | partitionedtable1_part2      | table             | me
public  | partitionedtable1_part3      | table             | me
public  | tableb                       | table             | me

但我想要一个这样的列表,没有父分区表的子分区:

                         List of relations

 Schema |             Name             |       Type        | Owner  
--------+------------------------------+-------------------+--------  
public  | tablea                       | table             | me
public  | partitionedtable1            | partitioned table | me
public  | tableb                       | table             | me

最佳答案

查询得到所有普通表,包括根分区表,但排除所有非根分区表:

SELECT n.nspname AS "Schema"
     , c.relname AS "Name"
     , CASE c.relkind
         WHEN 'p' THEN 'partitioned table'
         WHEN 'r' THEN 'ordinary table'
         -- more types?
         ELSE 'unknown table type'
       END AS "Type"
     , pg_catalog.pg_get_userbyid(c.relowner) AS "Owner"
FROM   pg_catalog.pg_class c
JOIN   pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE  c.relkind = ANY ('{p,r,""}')  -- add more types?
AND    NOT c.relispartition          -- exclude child partitions
AND    n.nspname !~ ALL ('{^pg_,^information_schema$}') -- exclude system schemas
ORDER  BY 1, 2;

The manual about relispartition:

... True if table or index is a partition

pg_get_userbyid()有助于获取拥有角色的名称。

Postgres 12 中有更多类型的“表”。The manual about relkind:

r = ordinary table, i = index, S = sequence, t = TOAST table, v = view, m = materialized view, c = composite type, f = foreign table, p = partitioned table, I = partitioned index



Postgres 12 还添加了元命令 \dPpsql:

The manual:

\dP[itn+] [ pattern ]

Lists partitioned relations. If pattern is specified, only entries whose name matches the pattern are listed. The modifiers t (tables) and i (indexes) can be appended to the command, filtering the kind of relations to list. By default, partitioned tables and indexes are listed.

If the modifier n (“nested”) is used, or a pattern is specified, then non-root partitioned relations are included, and a column is shown displaying the parent of each partitioned relation.

因此 \dPt 获取所有根分区表 - 但不是普通表。

关于postgresql - 仅显示没有子分区的表列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58242380/

相关文章:

java - 一起使用 PartioningBy 和 groupingBy 时无法获得所需的输出

database - 数据库中两个表之间更灵活的关系?

postgresql - 无法连接到 AWS RDS 还原实例

bash - 每次使用 psql 手动输入密码的问题

postgresql - 如何创建 PostgreSQL 分区序列?

SQL 查询 - 当我们在 where 子句中使用主键时更新/删除语句的性能

postgresql - Odoo (OpenERP8) 与 Postgre 的连接失败

ruby - 为 Sinatra 应用程序连接到现有的 Heroku Postgres DB

sql - PL/pgSQL : Add static column to query result

postgresql - 尝试在 Postgres 中导出 CSV 时权限被拒绝