如何使用 ON CONFLICT with Exclusion Constraint?

How to use ON CONFLICT with Exclusion Constraint?

例如: 按 useridsdateedate

索引
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

select version() 显示:

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

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

您必须使用 the documentation.

示例 43.2 中的代码