表“subscriber_historization”上的唯一约束缺少作为分区键一部分的列“processing_date”

Yus*_*fmm 4 postgresql

我想使用约束,以便我可以使用 upsert。因为我不想在 customer_identifier_value 上重复输入。

on conflict (customer_identifier_value) do nothing

[42P10] ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification
Run Code Online (Sandbox Code Playgroud)

当我创建约束时

alter table subscriber_historization
add constraint customer_identifier_value_unique unique (customer_identifier_value);

[0A000] ERROR: insufficient columns in UNIQUE constraint definition
Detail: UNIQUE constraint on table "subscriber_historization" lacks column "processing_date" which is part of the partition key.
Run Code Online (Sandbox Code Playgroud)

这是 DDL。

-- auto-generated definition
create table subscriber_historization
(
    customer_identifier_value text not null,
    product_value             text,
    contract_date_end         date,
    processing_date           date not null,
    constraint subscriber_historization_pk
        primary key (processing_date, customer_identifier_value)
)
    partition by RANGE (processing_date);
Run Code Online (Sandbox Code Playgroud)

如果我使用

ON CONFLICT ON CONSTRAINT subscriber_historization_pk DO NOTHING
Run Code Online (Sandbox Code Playgroud)

如果 process_date 不同,则将插入该行。那么 customer_identifier_value 上将会有重复的条目。

那么如何使用upsert呢?

感谢您的帮助。

Lau*_*lbe 5

您无法通过分区表来阻止这种情况,因为所有唯一索引都必须包含分区键。

唯一的出路是SERIALIZABLE始终使用事务隔离并使用触发器验证约束。然而,这会对性能造成影响。

这是分区的限制。