更新时间:2024-03-06 GMT+08:00

PG_PARTITION

PG_PARTITION系统表存储数据库内所有分区表(partitioned table)、分区(table partition)、分区上toast表和分区索引(index partition)四类对象的信息。分区表索引(partitioned index)的信息不在PG_PARTITION系统表中保存。

表1 PG_PARTITION字段

名称

类型

描述

relname

name

分区表、分区、分区上toast表和分区索引的名称。

parttype

"char"

对象类型:

  • 'r':partitioned table
  • 'p':table partition
  • 'x':index partition
  • 't':toast table

parentid

oid

当对象为分区表或分区时,此字段表示分区表在PG_CLASS中的OID。

当对象为index partition时,此字段表示所属分区表索引(partitioned index)的OID。

rangenum

integer

保留字段。

intervalnum

integer

保留字段。

partstrategy

"char"

分区表分区策略,现在仅支持:

'r':范围分区。

'v':数值分区。

'l':列表分区。

relfilenode

oid

table partition、index partition、分区上toast表的物理存储位置。

reltablespace

oid

table partition、index partition、分区上toast表所属表空间的OID。

relpages

double precision

统计信息:table partition、index partition的数据页数量。

reltuples

double precision

统计信息:table partition、index partition的元组数。

relallvisible

integer

统计信息:table partition、index partition的可见数据页数。

reltoastrelid

oid

table partition所对应toast表的OID。

reltoastidxid

oid

table partition所对应toast表的索引的OID。

indextblid

oid

index partition对应table partition的OID。

indisusable

boolean

分区索引是否可用。

reldeltarelid

oid

Delta表的OID。

reldeltaidx

oid

Delta表的索引表的OID。

relcudescrelid

oid

CU描述表的OID。

relcudescidx

oid

CU描述表的索引表的OID。

relfrozenxid

xid32

冻结事务ID号。

为保持前向兼容,保留此字段,新增relfrozenxid64用于记录此信息。

intspnum

integer

间隔分区所属表空间的个数。

partkey

int2vector

分区键的列号。

intervaltablespace

oidvector

间隔分区所属的表空间,间隔分区以round-robin方式落在这些表空间内。

interval

text[]

间隔分区的间隔值。

boundaries

text[]

范围分区和间隔分区的上边界。

transit

text[]

间隔分区的跳转点。

reloptions

text[]

设置partition的存储属性,与pg_class.reloptions的形态一样,用"keyword=value"格式的字符串来表示 ,目前用于在线扩容的信息搜集。

relfrozenxid64

xid

冻结事务ID号。

boundexprs

pg_node_tree

分区边界表达式。

  • 对于范围分区来说是分区上边界表达式。
  • 对于列表分区来说是分区边界枚举值集合。

pg_node_tree数据类型是不可读的,可用如下表达式pg_get_expr把当前字段单翻译为可读信息。

1
2
3
4
5
6
SELECT pg_get_expr(boundexprs, 0) FROM pg_partition
WHERE relname = 'country_202201';
pg_get_expr
---------------------------------------------------------------
ROW(202201, 'city1'::text), ROW(202201, 'city2'::text)
(1 row)

应用示例

查询分区表web_returns_p2的分区信息。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
CREATE TABLE web_returns_p2
(
    wr_returned_date_sk       integer,
    wr_returned_time_sk       integer,
    wr_item_sk                integer NOT NULL,
    wr_refunded_customer_sk   integer
)
WITH (orientation = column)
DISTRIBUTE BY HASH (wr_item_sk)
PARTITION BY RANGE(wr_returned_date_sk)
(
    PARTITION p2016 START(20161231) END(20191231) EVERY(10000),
    PARTITION p0 END(maxvalue)
);

SELECT oid FROM pg_class WHERE relname ='web_returns_p2';
  oid
-------
 97628

SELECT relname,parttype,parentid,boundaries FROM pg_partition WHERE parentid = '97628';
    relname     | parttype | parentid | boundaries
----------------+----------+----------+------------
 web_returns_p2 | r        |    97628 |
 p2016_0        | p        |    97628 | {20161231}
 p2016_1        | p        |    97628 | {20171231}
 p2016_2        | p        |    97628 | {20181231}
 p2016_3        | p        |    97628 | {20191231}
 p0             | p        |    97628 | {NULL}
(6 rows)