報錯信息
psycopg2.databaseerror: terminating connection due to idle-session timeout
官網文檔說明
文檔
官方不推薦在postgresql.conf中設置statement_timeout,因為它會影響所有會話。
statement_timeout:
中止任何使用了超過指定毫秒數的語句,從命令到達服務器開始計時。如果log_min_error_statement被設置為ERROR或更低,語句如果超時也會被記錄。一個零值(默認)將關閉這個參數。
idle_in_transaction_session_timeout:
終止任何已經閑置超過這個參數所指定的時間(以毫秒計)的打開事務的會話。 這使得該會話所持有的任何鎖被釋放,并且其所持有的連接槽可以被重用, 它也允許只對這個事務可見的元組被清理。
解決方案
在執行的sql前設置對于的值
=
和to
都一樣
單位ms
,s
,min
,h
, andd
-- 查看
show statement_timeout;
-- 設置(臨時)
set statement_timeout to '4h';
-- 查看
show idle_in_transaction_session_timeout;
-- 設置(臨時)
set idle_in_transaction_session_timeout='1h';