debugsqlMinor
ERROR: insufficient columns in PRIMARY KEY constraint definition
Viewed 0 times
definitionerrorprimarycolumnsinsufficientconstraintkey
Problem
I recently upgraded the database from PostgreSQL v9.6 to v11.7. we have some partitioned table with
Table DDL:
When I try the create table, it's throwing below error:
Is it mandatory/necessary that the
If the
If I receive billing_account_guid, ban combination with different load_dttm then it will end up with duplicate keys.
Could some please help me to understand this scenario?
Thanks.
inherence and planning to migrate them to the declaration.Table DDL:
CREATE TABLE c_account_p
(
billing_account_guid character varying(40) NOT NULL,
ingestion_process_id bigint NOT NULL DEFAULT '-1'::integer,
load_dttm timestamp(6) without time zone NOT NULL,
ban integer NOT NULL,
CONSTRAINT billing_account_pkey PRIMARY KEY (billing_account_guid, ban)
) PARTITION by RANGE(load_dttm);When I try the create table, it's throwing below error:
ERROR: insufficient columns in the PRIMARY KEY constraint definition
DETAIL: PRIMARY KEY constraint on table "l_billing_account_p" lacks column "load_dttm" which is part of the partition key.
SQL state: 0A000Is it mandatory/necessary that the
partition column should be a primary key? cause if I include load_dttm as PK then its working fine.db<>fiddleIf the
partition column should be supposed to be a PK, it's challenging to create a partition by range with the date column, cause the load_dttm column chances to have duplicate if data loaded COPY or INSERT.INSERT INTO c_account_p SELECT * from c_account_p_bkp ON CONFLICT (billing_account_guid,ban,load_dttm) DO UPDATE SET 'some stuff..'If I receive billing_account_guid, ban combination with different load_dttm then it will end up with duplicate keys.
Could some please help me to understand this scenario?
Thanks.
Solution
Yes, this is required. There is no concept of an index over all
partitions in PostgreSQL. The requirement of having the partition key
a subset of the primary key allows us to implement primary keys by
just having individual unique indexes on each partition.
There's mention in section 5.10.2.3. "Unique constraints on
partitioned tables must include all the partition key columns. This
limitation exists because PostgreSQL can only enforce uniqueness in
each partition individually.
partitions in PostgreSQL. The requirement of having the partition key
a subset of the primary key allows us to implement primary keys by
just having individual unique indexes on each partition.
There's mention in section 5.10.2.3. "Unique constraints on
partitioned tables must include all the partition key columns. This
limitation exists because PostgreSQL can only enforce uniqueness in
each partition individually.
CREATE TABLE c_account_p
(
billing_account_guid character varying(40) NOT NULL,
ingestion_process_id bigint NOT NULL DEFAULT '-1'::integer,
load_dttm timestamp(6) without time zone NOT NULL,
ban integer NOT NULL,
CONSTRAINT billing_account_pkey PRIMARY KEY (billing_account_guid,ban,load_dttm)
) PARTITION by RANGE(load_dttm);Code Snippets
CREATE TABLE c_account_p
(
billing_account_guid character varying(40) NOT NULL,
ingestion_process_id bigint NOT NULL DEFAULT '-1'::integer,
load_dttm timestamp(6) without time zone NOT NULL,
ban integer NOT NULL,
CONSTRAINT billing_account_pkey PRIMARY KEY (billing_account_guid,ban,load_dttm)
) PARTITION by RANGE(load_dttm);Context
StackExchange Database Administrators Q#276291, answer score: 7
Revisions (0)
No revisions yet.