I'm new to PostgreSQL and I need to convert an existing non-partitioned table to a partitioned table. Since you can't alter an existing table to be partitioned, I created a new partitioned table. According to the PostgreSQL documentation, you can't add constraints to a partitioned table. Constraints must be added to partitions. When I try to add Primary Key constraints to the partitions, I get an error. Can you suggest a way to add constraints to this partition?
purchase_history_within_10_months
is the new partition name.
SQL query:
ALTER TABLE ONLY purchase_history_within_10_months ADD CONSTRAINT purchase_history_pkey PRIMARY KEY (transactionid);
Error Statement:
ERROR: relation "purchase_history_pkey" already exists
SQL state: 42P07
According to the error message, there is an existing constraint with the same name for this partition.
I have checked the existing constraints for this partition.
SQL query:
appxxxx=# SELECT constraint_name, constraint_type, table_name FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE table_name = 'purchase_history_within_10_months'; constraint_name | constraint_type | table_name
------------------------+-----------------+----------------------------------- 2200_30127_1_not_null | CHECK | purchase_history_within_10_months 2200_30127_14_not_null | CHECK | purchase_history_within_10_months 2200_30127_16_not_null | CHECK | purchase_history_within_10_months 2200_30127_36_not_null | CHECK | purchase_history_within_10_months (4 rows)appxxxx=# appxxxx=#
According to this, there is no constraint with the same name on this partition. So, what is causing this issue?