分区表 是一种特殊的表,它使用 声明式分区 提供的方法之一将其划分为分区。无法直接向分区表本身插入数据;所有插入操作都必须能够路由到其某个分区。
分区表 自 PostgreSQL 10 起就已存在,届时添加了声明式分区。
示例
创建来自 PostgreSQL 分区文档 的示例表
postgres=# CREATE TABLE measurement (
city_id INT NOT NULL,
logdate DATE NOT NULL,
peaktemp INT,
unitsales INT
) PARTITION BY RANGE (logdate);
CREATE TABLE
postgres=# \dt
List of relations
Schema | Name | Type | Owner
--------+-------------+-------------------+----------
public | measurement | partitioned table | postgres
(1 row)
postgres=# \d measurement
Partitioned table "public.measurement"
Column | Type | Collation | Nullable | Default
-----------+---------+-----------+----------+---------
city_id | integer | | not null |
logdate | date | | not null |
peaktemp | integer | | |
unitsales | integer | | |
Partition key: RANGE (logdate)
Number of partitions: 0
由于不存在分区,因此无法向此表插入数据
postgres=# INSERT INTO measurement VALUES(1, CURRENT_DATE, 25, 3); ERROR: no partition of relation "measurement" found for row DETAIL: Partition key of the failing row contains (logdate) = (2021-11-08).
参考资料
- PostgreSQL 文档: 声明式分区
