Postgresql幾個和執行語句相關的timeout參數

在執行相關SQL的時候,相關的幾個timeout說明一下,以免在生產環境中誤用

statement_timeout  #語句執行時間超過這個設置時間,終止執行SQL,0爲禁用
idle_in_transaction_session_timeout  #一個空閒的事物中,空閒時間超過這個值,將視爲超時,0爲禁用
lock_timeout #獲取一個表,索引,行上的鎖超過這個時間,直接報錯,不等待,0爲禁用
deadlock_timeout #死鎖時間超過這個值將直接報錯,不會等待,默認設置1s

statement_timeout設置實例:

statement_timeout設置爲1,參數單位是ms
postgres=# show statement_timeout;
 statement_timeout 
-------------------
 1ms
postgres=# \d tb1
ERROR:  canceling statement due to statement timeout
查看日誌
tail -f postgresql-2020-06-15_165900.csv
2020-06-15 17:00:47.054 CST,"postgres","postgres",15267,"[local]",5ee73856.3ba3,22,"SELECT",2020-06-15 16:59:02 CST,3/36,0,ERROR,57014,"canceling statement due to statement timeout",,,,,,"SELECT ...

可見設置爲1ms,表結構的查詢都無法執行,statement_timeout參數個人不建議設置,當然可以根據需求自行設定

idle_in_transaction_session_timeout設置實例

同樣設置設置爲1ms,開啓一個事物,在事物中查看錶tb1
postgres=# show idle_in_transaction_session_timeout;
 idle_in_transaction_session_timeout 
-------------------------------------
 1ms
(1 row)
postgres=# begin;
BEGIN
postgres=*# select * from tb1;
FATAL:  terminating connection due to idle-in-transaction timeout
server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.
The connection to the server was lost. Attempting reset: Succeeded.
查看日誌已經報以下錯誤:
2020-06-15 17:12:49.880 CST,"postgres","postgres",15601,"[local]",5ee73b73.3cf1,2,"idle in transaction",2020-06-15 17:12:19 CST,3/18,0,FATAL,25P03,"terminating connection due to idle-in-transaction timeout",,,,,,,,,"psql","client backend"

lock_timeout設置實例

設置lock_timeout爲1ms,建表,然後在不同事物更新同一行
session A:
postgres=# create table tb1 (a int,b name);
CREATE TABLE
postgres=# insert into tb1 values (1,'hank'),(2,'dazuiba');
INSERT 0 2
postgres=# show lock_timeout;
 lock_timeout 
--------------
 1ms
(1 row)
postgres=# begin;
BEGIN
postgres=*# update tb1 set b='dazuiba' where a=1;
UPDATE 1

session B:
postgres=# update tb1 set b='dazuiba' where a=1;
ERROR:  canceling statement due to lock timeout
CONTEXT:  while updating tuple (0,13) in relation "tb1"
查看日誌報錯內容如下
2020-06-15 17:22:43.212 CST,"postgres","postgres",15995,"[local]",5ee73dc3.3e7b,1,"UPDATE",2020-06-15 17:22:11 CST,4/2,500,ERROR,55P03,"canceling statement due to lock timeout",,,,,"while updating tuple (0,13) in relation ""tb1""","update tb1 set b='dazuiba' where a=1;",,,"psql","client backend"

deadlock_timeout實例

設置deadlock_timeout爲1s,死鎖檢測需要開啓兩個session,接下來看以下步驟:
postgres=# show deadlock_timeout;
 deadlock_timeout 
------------------
 1s
(1 row)
session A:
postgres=# begin;
BEGIN
postgres=*# update tb1 set b='dazuiba' where a=1;   #執行第一個UPDATE
UPDATE 1
postgres=*# update tb1 set b='hank' where a=2;     #執行第三個UPDATE,在第四個UPDATE之前這裏是等待狀態,第四個UPDATE報死鎖錯誤後,這裏UPDATE成功
UPDATE 1


session B:
postgres=# begin;
BEGIN
postgres=*# update tb1 set b='dazuiba' where a=2;    #執行第二個UPDATE
UPDATE 1
postgres=*# update tb1 set b='hank' where a=1;       #執行第四個UPDATE造成死鎖報錯
ERROR:  deadlock detected
DETAIL:  Process 16212 waits for ShareLock on transaction 501; blocked by process 16211.
Process 16211 waits for ShareLock on transaction 502; blocked by process 16212.
HINT:  See server log for query details.
CONTEXT:  while updating tuple (0,13) in relation "tb1"

日誌報錯如下:
2020-06-15 17:34:10.485 CST,"postgres","postgres",16212,"[local]",5ee7404f.3f54,1,"UPDATE",2020-06-15 17:33:03 CST,4/2,502,ERROR,40P01,"deadlock detected","Process 16212 waits for ShareLock on transaction 501; blocked by process 16211.
Process 16211 waits for ShareLock on transaction 502; blocked by process 16212.
Process 16212: update tb1 set b='hank' where a=1;
Process 16211: update tb1 set b='hank' where a=2;","See server log for query details.",,,"while updating tuple (0,13) in relation ""tb1""","update tb1 set b='hank' where a=1;",,,"psql","client backend"

以上就是四個參數的實例說明解釋,稍微複雜的就是deadlock_timeout實例,如果明白死鎖的概念,這裏也很好理解。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章