create table a (
b text not null unique,
c text not null
);
-- Sleeps for 1 second as expected.
insert into a (b, c) values ('d', pg_sleep(1));
-- Sleeps for 1 second even though record is not inserted.
insert into a (b, c) values ('d', pg_sleep(1));
无论如何,是否要重构第二个语句,以便pg_sleep
仅在没有违反基于的约束时才运行(或我调用的实际密集函数)b
?请注意,c
(函数调用的结果)与约束无关。
夫妻解决方法:
insert
如果发生且没有错误,则仅使用b
和update
c
在下一条语句中。insert
缺点是我必须放松/删除not null
的约束c
,因为它将暂时为空。在咨询锁下,检查是否已存在
a
相同的b
,如果不存在,则执行insert
. 缺点是我手动复制唯一性检查,并对性能产生负面影响。