以下语句遇到竞争条件,我可以通过非常快速地连续执行它来可靠地证明。有没有办法从下面删除这种竞争条件我是否需要采取不同的方法?
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);
表如下:
CREATE TABLE scheduled_event_log (
"key" varchar(64) NOT NULL,
"timestamp_utc" timestamp without time zone default (now() at time zone 'utc')
);