Atomic INSERT WHERE NOT EXISTS (Postgres 9.6.3)

匿名 (未验证) 提交于 2019-12-03 01:44:01

问题:

The following statement suffers a race condition which I can reliably demonstrate by executing it concurrent in very quick succession. Is there a way to remove this race condition from the below of do I need to take a different approach altogether?

INSERT INTO scheduled_event_log ("key")  SELECT 'test'      WHERE NOT EXISTS(          SELECT AGE(now() at time zone 'utc', timestamp_utc)          FROM scheduled_event_log          WHERE "key" = 'test'          AND age(now() at time zone 'utc', timestamp_utc) < '6s' FOR UPDATE); 

Table as follows:

CREATE TABLE scheduled_event_log (   "key" varchar(64) NOT NULL,   "timestamp_utc" timestamp without time zone default (now() at time zone 'utc') ); 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!