分區(qū)表支持任意列作為主鍵
在PostgreSQL中,分區(qū)表的主鍵只能建立在分區(qū)鍵上,且不能作為外鍵引用。PolarDB PostgreSQL版(兼容Oracle)支持分區(qū)表使用任意列作為主鍵或者外鍵引用。
前提條件
支持的PolarDB PostgreSQL版(兼容Oracle)的版本如下:
Oracle 2.0(內(nèi)核小版本2.0.14.17.0及以上)
您可通過如下語句查看PolarDB PostgreSQL版(兼容Oracle)的內(nèi)核小版本的版本號:
SHOW polar_version;
參數(shù)說明
polar_pk_in_non_partition_column_mode
:用于指定非分區(qū)鍵上的主鍵所使用的索引類型,取值如下:
none
/local_pk
:不支持主鍵建立在非分區(qū)鍵上,否則會提示錯誤。global_index
:在非分區(qū)鍵上創(chuàng)建主鍵時,會使用全局索引作為約束。
使用限制
指定的主鍵如果包含所有分區(qū)鍵,則默認(rèn)使用局部索引作為主鍵,否則使用全局索引作為主鍵。
指定的約束如果包含所有分區(qū)鍵,則默認(rèn)使用局部索引作為唯一約束,否則使用全局索引作為唯一約束。
分區(qū)表的添加主鍵語法只能使用全局索引,因為局部索引不一定能滿足約束,添加唯一約束語法也是如此。
分區(qū)表被外鍵引用的行不允許發(fā)生跨分區(qū)更新,因為跨分區(qū)更新本質(zhì)上是一次刪除和一次插入,在執(zhí)行刪除時會檢測此行是否被外鍵引用。
示例
設(shè)置polar_pk_in_non_partition_column_mode
為none
,并在非分區(qū)鍵上創(chuàng)建主鍵。
SET polar_pk_in_non_partition_column_mode = none;
CREATE TABLE pt1 (a int, b int primary key, c varchar) PARTITION BY RANGE(a);
ERROR: unique constraint on partitioned table must include all partitioning columns
DETAIL: PRIMARY KEY constraint on table "pt1" lacks column "a" which is part of the partition key.
設(shè)置polar_pk_in_non_partition_column_mode
為global_index
,并在非分區(qū)鍵上創(chuàng)建主鍵。
SET polar_pk_in_non_partition_column_mode = global_index;
CREATE TABLE pt1 (a int, b int primary key, c varchar) PARTITION BY RANGE(a);
CREATE TABLE pt1_p1 PARTITION OF pt1 FOR VALUES FROM (0) TO (1);
\d pt1
Partitioned table "public.pt1"
Column | Type | Collation | Nullable | Default
--------+-------------------+-----------+----------+---------
a | integer | | |
b | integer | | not null |
c | character varying | | |
Partition key: RANGE (a)
Indexes:
"pt1_pkey" PRIMARY KEY, btree (b) GLOBAL
Number of partitions: 1 (Use \d+ to list them.)
如果分區(qū)表的主鍵包含了所有分區(qū)鍵,則使用局部索引作為主鍵,無論參數(shù)polar_pk_in_non_partition_column_mode
的取值是什么,此為PostgreSQL的默認(rèn)行為。
CREATE TABLE pt2 (a int primary key, b int, c varchar) PARTITION BY RANGE(a);
CREATE TABLE pt2_p1 PARTITION OF pt2 FOR VALUES FROM (0) TO (1);
\d pt2
Partitioned table "public.pt2"
Column | Type | Collation | Nullable | Default
--------+-------------------+-----------+----------+---------
a | integer | | not null |
b | integer | | |
c | character varying | | |
Partition key: RANGE (a)
Indexes:
"pt2_pkey" PRIMARY KEY, btree (a)
Number of partitions: 1 (Use \d+ to list them.)
如果必須使用全局索引作為主鍵,可以使用修改表主鍵的語法。
ALTER TABLE pt2 DROP CONSTRAINT pt2_pkey;
CREATE UNIQUE INDEX pt2_pkey ON pt2 (a) GLOBAL;
ALTER TABLE pt2 ADD PRIMARY KEY USING INDEX pt2_pkey;
\d pt2
Partitioned table "public.pt2"
Column | Type | Collation | Nullable | Default
--------+-------------------+-----------+----------+---------
a | integer | | not null |
b | integer | | |
c | character varying | | |
Partition key: RANGE (a)
Indexes:
"pt2_pkey" PRIMARY KEY, btree (a) GLOBAL
Number of partitions: 1 (Use \d+ to list them.)