postgresql10以前版本的分区表是通过继承追加制约实现的,postgresql10新追加PARTITION BY可以直接定义list和range分区,检证如下:
postgres=# CREATE TABLE test(id integer,name text) PARTITION BY LIST (id);
postgres=# CREATE TABLE test_p1 PARTITION OF test FOR VALUES IN (10);
postgres=# CREATE TABLE test_p20 PARTITION OF test FOR VALUES IN (20);
postgres=# \d+ test;
Table “public.test”
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
--------±--------±----------±---------±--------±---------±-------------±------------
id | integer | | | | plain | |
name | text | | | | extended | |
Partition key: LIST (id)
Partitions: test_p1 FOR VALUES IN (10),
test_p20 FOR VALUES IN (20)
postgres=# \d+ test_p1;
Table “public.test_p1”
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
--------±--------±----------±---------±--------±---------±-------------±------------
id | integer | | | | plain | |
name | text | | | | extended | |
Partition of: test FOR VALUES IN (10)
Partition constraint: ((id IS NOT NULL) AND (id = ANY (ARRAY[10])))
postgres=#
postgres=# insert into test values (10,‘aaaa’);
INSERT 0 1
postgres=# insert into test values (20,‘bbbb’);
INSERT 0 1
postgres=# insert into test values (30,‘cccc’);
ERROR: no partition of relation “test” found for row
DETAIL: Partition key of the failing row contains (id) = (30).
postgres=#
postgres=# select * from test;
id | name
----±-----
10 | aaaa
20 | bbbb
(2 rows)
postgres=# select * from test_p1;
id | name
----±-----
10 | aaaa
(1 row)
postgres=# select * from test_p20;
id | name
----±-----
20 | bbbb
(1 row)
postgres=#