when adding a unique index and then a PK to a partitioned table that already has that PK on a table partition, the PK creation fails with multiple primary keys for table X are not allowed
Why this limitation and the strange error message? Shouldn’t that just work?
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
2
Answers
The weird error message seems to be an area of improvement for postgres. A more clear error message could be displayed instead.
But in general the feature of creating a primary key using an existing index is just not there yet in PG for partitioned tables.
See https://www.postgresql.org/docs/14/sql-altertable.html, chapters
https://www.postgresql.org/docs/14/sql-altertable.html and
https://www.postgresql.org/docs/16/sql-altertable.html#SQL-ALTERTABLE-DESC-ADD-TABLE-CONSTRAINT-USING-INDEX.
They clearly state that such operations are not yet possible for partitioned tables.
If you already have a primary key on the partition, you can add a primary key on the partitioned table by first adding an invalid primary key on only the partitioned table, then attaching the primary key index on the partitions as partitions to that index. Once the index has a partition on all table partitions, the primary key becomes valid automatically: