将唯一索引然后添加 PK 到表分区上已具有该 PK 的分区表时,PK 创建失败,并显示
multiple primary keys for table X are not allowed
为什么会有这个限制和奇怪的错误消息?这不应该有效吗?
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
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
奇怪的错误消息似乎是 postgres 需要改进的地方。可以显示更清晰的错误消息。
但总的来说,分区表的 PG 中还没有使用现有索引创建主键的功能。
参见 https://www.postgresql.org/docs/14/sql-altertable.html,章节
https://www.postgresql.org/docs/14/sql-altertable.html和
他们明确指出分区表尚无法进行此类操作。