您好,登錄后才能下訂單哦!
本篇文章為大家展示了正在執行的SQL語句如何在postgresql中結束,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
結束進程兩種方式:
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
上述內容就是正在執行的SQL語句如何在postgresql中結束,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。