PostgreSQL如何控制客户端连接超时

时间:2025-02-09 16:53:36

1.空闲长事务连接

使用idle_in_transaction_session_timeout参数,默认值是0,不开启,例如配置为5分钟。

idle_in_transaction_session_timeout = 5min

注意:该参数对idle正常连接无影响。

2.空闲连接

可使用扩展插件pg_timeout插件,配置

shared_preload_libraries = 'pg_timeout'
pg_timeout.naptime=60 #单位为秒
pg_timeout.idle_session_timeout=60 #单位为秒

3.异常连接

客户端异常断开的连接,可配置

tcp_keepalives_idle = 60
tcp_keepalives_interval = 20 
tcp_keepalives_count = 3 

注意:不配置会使用操作系统默认值。