当向表分区上已有该PK的分区表添加唯一索引,然后添加PK时,PK创建失败,错误为multiple primary keys for table X are not allowed
为什么会出现这种限制和奇怪的错误消息?不是应该管用吗?
场景1:索引然后主键-失败
CREATE TABLE tst_t (
c1 int4 NOT NULL,
c2 int8 NOT NULL,
c3 int8 NOT NULL
)
PARTITION BY LIST (c1);
CREATE TABLE tst_t_988 PARTITION OF tst_t (
CONSTRAINT pk_tst_t_988 PRIMARY KEY (c1)
) FOR VALUES IN (988);
create unique index pk_tst_t on tst_t using btree (c1);
alter table tst_t add primary key (c1);
-- SQL Error [42P16]: ERROR: multiple primary keys for table "tst_t_988" are not allowed
场景2:没有索引则PK-Works
drop table tst_t;
CREATE TABLE tst_t (
c1 int4 NOT NULL,
c2 int8 NOT NULL,
c3 int8 NOT NULL
)
PARTITION BY LIST (c1);
CREATE TABLE tst_t_988 PARTITION OF tst_t (
CONSTRAINT pk_tst_t_988 PRIMARY KEY (c1)
) FOR VALUES IN (988);
-- this time not creating the index
--create unique index pk_tst_t on tst_t using btree (c1);
alter table tst_t add primary key (c1);
-- works
场景3:只对已分区的表进行索引,然后使用PK-Works
drop table tst_t;
CREATE TABLE tst_t (
c1 int4 NOT NULL,
c2 int8 NOT NULL,
c3 int8 NOT NULL
)
PARTITION BY LIST (c1);
CREATE TABLE tst_t_988 PARTITION OF tst_t (
CONSTRAINT pk_tst_t_988 PRIMARY KEY (c1)
) FOR VALUES IN (988);
-- this time index on ONLY partitioned table
create unique index pk_tst_t on only tst_t using btree (c1);
alter table tst_t add primary key (c1);
-- works