表“ subscriber_historization”上的UNIQUE约束缺少列“ processing_date”,该列是分区键的一部分

时间:2019-11-26 05:47:37

标签: postgresql

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

IrradFromTiltE

创建约束时


                         Gef      Bef      Def  
 2017-01-01 04:50:36   0.0000   0.0000  0.00000  
 2017-01-01 04:51:36   0.0000   0.0000  0.00000  
 2017-01-01 04:52:36   0.0000   0.0000  0.00000  
 2017-01-01 04:53:36   0.0000   0.0000  0.00000  
 2017-01-01 04:54:36   0.0000   0.0000  0.00000  
 2017-01-01 04:55:36       NA       NA       NA  
2017-01-01 04:56:36       NA       NA       NA  
2017-01-01 04:57:36       NA       NA       NA  
2017-01-01 04:58:36       NA       NA       NA  
2017-01-01 04:59:36       NA       NA       NA  
2017-01-01 05:00:36       NA       NA       NA  
2017-01-01 05:01:36       NA       NA       NA  
2017-01-01 05:02:36       NA       NA       NA  
2017-01-01 05:03:36       NA       NA       NA  
2017-01-01 05:04:36       NA       NA       NA  
2017-01-01 05:05:36 438.4437 411.0645 27.11184  
2017-01-01 05:06:36 375.3661 337.1296 37.98122  
2017-01-01 05:07:36 338.4409 293.0288 45.15567  
2017-01-01 05:08:36 368.4516 325.6663 42.49433  
2017-01-01 05:09:36 415.7088 379.9627 35.40693  
2017-01-01 05:10:36 425.7758 391.6909 33.71889  

这里是DDL。

on conflict (customer_identifier_value) do nothing

[42P10] ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification

如果我使用

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.

如果process_date不同,将插入该行。然后在customer_identifier_value上将有重复的条目。

那么如何使用upsert?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您不能用分区表来防止这种情况,因为所有唯一索引都必须包含分区键。

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

这是分区的限制。