我正在寻求澄清如何确保在PLPGSQL功能的原子事务,并在隔离级别设置为这个特殊的变化到数据库。
在下面显示的PLPGSQL功能,我要确保,无论是删除和插入成功。 我得到一个错误,当我尝试将它们包装在一个单独的事务:
ERROR: cannot begin/end transactions in PL/pgSQL
。
如果另一个用户已增加了对环境默认行为(“RAIN”,“夜景”,“45英里每小时”) 后,此功能已删除的自定义行下面的函数的执行过程中会发生什么,但之前有机会插入自定义行? 是否有一个隐含的交易包裹的插入和删除,使两者都回滚如果另一个用户已更改:该函数引用的行吗? 我可以设置此功能的隔离级别?
create function foo(v_weather varchar(10), v_timeofday varchar(10), v_speed varchar(10),
v_behavior varchar(10))
returns setof CUSTOMBEHAVIOR
as $body$
begin
-- run-time error if either of these lines is un-commented
-- start transaction ISOLATION LEVEL READ COMMITTED;
-- or, alternatively, set transaction ISOLATION LEVEL READ COMMITTED;
delete from CUSTOMBEHAVIOR
where weather = 'RAIN' and timeofday = 'NIGHT' and speed= '45MPH' ;
-- if there is no default behavior insert a custom behavior
if not exists
(select id from DEFAULTBEHAVIOR where a = 'RAIN' and b = 'NIGHT' and c= '45MPH') then
insert into CUSTOMBEHAVIOR
(weather, timeofday, speed, behavior)
values
(v_weather, v_timeofday, v_speed, v_behavior);
end if;
return QUERY
select * from CUSTOMBEHAVIOR where ... ;
-- commit;
end
$body$ LANGUAGE plpgsql;