结束进程两种方式:
SELECT pg_cancel_backend(PID)
取消后台操作,回滚未提交事物 (select);
SELECT pg_terminate_backend(PID)
中断session,回滚未提交事物(select、update、delete、drop);
SELECT * FROM pg_stat_activity;
根据datid=10841
SELECT pg_terminate_backend (10841);
补充:PostgreSQL无法在PL / pgSQL中开始/结束事务
我正在寻求澄清如何确保plpgsql函数中的原子事务,以及为数据库进行此特定更改设置了隔离级别.
在下面显示的plpgsql函数中,我想确保BOTH的删除和插入成功.当我尝试将它们包装在一个事务中时,我收到一个错误:
错误:无法在PL / pgSQL中开始/结束事务.
如果另一个用户在此功能已删除自定义记录之后,但在此函数有机会插入自定义记录之前,为情况(RAIN,NIGHT,45MPH)添加了默认行为,下面的函数执行过程中会发生什么?是否有一个隐式事务包装插入和删除,以便如果另一个用户已经更改了此函数引用的任何一个行,两者都将回滚?我可以设置此功能的隔离级别吗?
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
一个plpgsql函数在事务中自动运行.这一切都成功了,一切都失败了.
我引用the manual on plpgsql functions:
Functions and trigger procedures are always executed within a transaction established by an outer query — they cannot start or commit that transaction, since there would be no context for them to execute in. However, a block containing an EXCEPTION clause effectively forms a subtransaction that can be rolled back without affecting the outer transaction.
所以,如果你需要,你可以捕获理论上可能发生的异常(但是不大可能).
Details on trapping errors in the manual.
您的功能审查和简化:
CREATE FUNCTION foo(v_weather text , v_timeofday text , v_speed text , v_behavior text) RETURNS SETOF custombehavior AS $body$ BEGIN DELETE FROM custombehavior WHERE weather = 'RAIN' AND timeofday = 'NIGHT' AND speed = '45MPH'; INSERT INTO custombehavior (weather, timeofday, speed, behavior) SELECT v_weather, v_timeofday, v_speed, v_behavior WHERE NOT EXISTS ( SELECT 1 FROM defaultbehavior WHERE a = 'RAIN' AND b = 'NIGHT' AND c = '45MPH' ); RETURN QUERY SELECT * FROM custombehavior WHERE ... ; END $body$LANGUAGE plpgsql
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
更新日志
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]