【问题标题】:How to use ON CONFLICT with Exclusion Constraint?如何在具有排除约束的情况下使用 ON CONFLICT?
【发布时间】:2019-09-19 00:36:08
【问题描述】:

例如: 索引userid, sdate, edate

userid  sdate       edate
001     2019-01-01  2019-01-30

如果我插入新数据,例如:

userid  sdate       edate           
001     2019-01-03  2019-01-20  
   or
001     2019-01-13  2019-02-10  
   or
001     2019-02-01  2019-02-15  

我在下面尝试使用 GIST,但是如何使用 ON CONFLICT 组合它?

CREATE EXTENSION btree_gist

CREATE TABLE test(
   ID INT PRIMARY KEY     NOT NULL,
   USERID     CHAR(5),
   SDATE      DATE,
   EDATE      DATE,
   EXCLUDE USING gist
   (USERID WITH =,
   daterange(SDATE, EDATE, '[]') WITH &&)
);


Insert Into test (usersid, sdate, edate)
Values  (@UsersId, @SDate, @EDate) 
ON Conflict ON CONSTRAINT  test_userid_daterange_excl 
Do Update 
   Set sdate = @SDate, edate = @EDate

我明白了:

ERROR:  ON CONFLICT DO UPDATE not supported with exclusion constraints

基于上述情况,我预计以下情况:

userid  sdate       edate       I_EXPECT    
001     2019-01-03  2019-01-20  UPDATE because it is in range
001     2019-01-13  2019-02-10  UPDATE because it is in range
001     2019-02-01  2019-02-15  INSERT because it is not in range

选择版本()显示:

PostgreSQL 10.6 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.3 20140911 (Red Hat 4.8.3-9), 64-bit

【问题讨论】:

  • 我打算建议你需要一个触发器来处理这个问题,但也许马的建议也可以。
  • 我得到:错误:ON CONFLICT DO UPDATE 不支持排除约束

标签: postgresql upsert exclusion-constraint


【解决方案1】:

您不能将INSERT ... ON CONFLICT 与排除索引一起使用,如错误消息所述。

您必须使用the documentation 中的示例 43.2 中的代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-06
    • 1970-01-01
    • 2021-12-21
    • 2019-08-03
    • 2012-12-06
    • 2023-02-14
    相关资源
    最近更新 更多