获取postgresql数据库中的分区数

时间:2011-12-26 08:38:17

标签: sql postgresql inheritance database-partitioning

获取数据库中创建的分区数量的最有效方法是什么? 我正在使用* postgresq * l APi for c ++。

2 个答案:

答案 0 :(得分:9)

这是您可以选择表分区的所有名称的方法:

SELECT
    nmsp_parent.nspname AS parent_schema,
    parent.relname      AS parent,
    nmsp_child.nspname  AS child,
    child.relname       AS child_schema
FROM pg_inherits
    JOIN pg_class parent        ON pg_inherits.inhparent = parent.oid
    JOIN pg_class child         ON pg_inherits.inhrelid   = child.oid
    JOIN pg_namespace nmsp_parent   ON nmsp_parent.oid  = parent.relnamespace
    JOIN pg_namespace nmsp_child    ON nmsp_child.oid   = child.relnamespace

也可以用来计算:

SELECT
    nmsp_parent.nspname     AS parent_schema,
    parent.relname          AS parent,
    COUNT(*)
FROM pg_inherits
    JOIN pg_class parent        ON pg_inherits.inhparent = parent.oid
    JOIN pg_class child     ON pg_inherits.inhrelid   = child.oid
    JOIN pg_namespace nmsp_parent   ON nmsp_parent.oid  = parent.relnamespace
    JOIN pg_namespace nmsp_child    ON nmsp_child.oid   = child.relnamespace
GROUP BY
    parent_schema,
    parent;

答案 1 :(得分:2)

这个问题很老了。但是a new question on dba.SE引用了它,我觉得有必要添加一个更简单的解决方案。

根据问题,要......

  

获取在数据库中创建的分区数:

使用inheritance和特定的父表,我假设:

SELECT count(*) AS partitions
FROM   pg_inherits i
WHERE  i.inhparent = 'my_schema.my_parent_tbl'::regclass;

关于dba.SE的相关答案的更多细节:
Get all partition names for a table