Jon*_*lla 8 postgresql partitioning psql
我只想显示 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
Run Code Online (Sandbox Code Playgroud)
但我想要一个这样的列表,没有父分区表的子分区:
List of relations
Schema | Name | Type | Owner
--------+------------------------------+-------------------+--------
public | tablea | table | me
public | partitionedtable1 | partitioned table | me
public | tableb | table | me
Run Code Online (Sandbox Code Playgroud)
查询获取所有普通表,包括根分区表,但排除所有非根分区表:
\nSELECT n.nspname AS "Schema"\n , c.relname AS "Name"\n , CASE c.relkind\n WHEN \'p\' THEN \'partitioned table\'\n WHEN \'r\' THEN \'ordinary table\'\n -- more types?\n ELSE \'unknown table type\'\n END AS "Type"\n , pg_catalog.pg_get_userbyid(c.relowner) AS "Owner"\nFROM pg_catalog.pg_class c\nJOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\nWHERE c.relkind = ANY (\'{p,r,""}\') -- add more types?\nAND NOT c.relispartition -- exclude child partitions\nAND n.nspname !~ ALL (\'{^pg_,^information_schema$}\') -- exclude system schemas\nORDER BY 1, 2;\n
Run Code Online (Sandbox Code Playgroud)\n\n\n\n... 如果表或索引是分区,则为 True
\n
pg_get_userbyid()
对于获取所属角色的名称很有帮助。
Postgres 12 中有更多类型的“表”。有关手册relkind
:
\n\n\n
r
= 普通表、i
= 索引、S
= 序列、t
= TOAST 表、\nv
= 视图、m
= 物化视图、c
= 复合类型、f
=\n外部表、p
= 分区表、I
= 分区索引
Postgres 12 还在psql\\dP
中添加了meta-命令:
\n\n\n
\\dP[itn+] [
pattern
]
列出分区关系。如果指定了模式,则仅列出名称与该模式匹配的条目。修饰符
\nt
(表)和i
(索引)可以附加到命令中,过滤要列出的关系类型。默认情况下,列出分区表和索引。如果修饰符
\nn
(\xe2\x80\x9cnested\xe2\x80\x9d),或指定模式,则包含非根分区关系,并显示一列,显示每个分区关系的父级。
所以\\dPt
获取所有根分区表 - 但不是普通表。
我现在无法测试这一点,但这应该只能为您提供已分区的顶级表
select relname
from pg_class
where oid in (select partrelid from pg_partitioned_table);
Run Code Online (Sandbox Code Playgroud)
您应该能够细化/扩展它以获得更多详细信息。
这是一个冗长可笑的解决方案:
with
partition_parents as (
select relnamespace::regnamespace::text as schema_name,
relname as table_name,
'partition_parent' as info,
*
from pg_class
where relkind = 'p'), -- The parent table is relkind 'p', the partitions are regular tables, relkind 'r'
unpartitioned_tables as (
select relnamespace::regnamespace::text as schema_name,
relname as table_name,
'unpartitioned_table' as info,
*
from pg_class
where relkind = 'r'
and not relispartition
) -- Regular table
select * from partition_parents where schema_name not in ('information_schema','pg_catalog','api','extensions') -- Whatever you've got for schemas
union
select * from unpartitioned_tables where schema_name not in ('information_schema','pg_catalog','api','extensions') -- Whatever you've got for schemas
order by 1,2,3
Run Code Online (Sandbox Code Playgroud)
您应该能够缩小该尺寸以符合您真正想要的。这里真正获得系统目录的人应该能够提供更简洁的版本。在加号列中,最好添加“partition_parent”与“unpartitioned_table”详细信息,如上所述。
归档时间: |
|
查看次数: |
6662 次 |
最近记录: |