标签:style blog http io color os ar 使用 for
1、use pgbouncer
可以利用pgbouncer的server_idle_timeout参数
server_idle_timeout:
;; Close server connection if its not been used in this time. ;; Allows to clean unnecessary connections from pool after peak. ;server_idle_timeout = 60
去掉前面的注释,限定idle状态超过60s的自动断开连接。当然这里设定的连接模式是session。
2、结合pg_stat_activity中的state和state_change字段
SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = ‘regress‘ AND pid <> pg_backend_pid() AND state = ‘idle‘ AND state_change < current_timestamp - INTERVAL ‘5‘ MINUTE;
有的可能使用pg_stat_activity中的query_start字段,但有时这个字段是空的,即用户只是连接进来但没有执行操作,此时该字段显示空,所以尽量使用state_change较稳妥,
使用pgbouncer就更方便了。
参考:
http://stackoverflow.com/questions/13236160/is-there-a-timeout-for-idle-postgresql-connections
http://www.postgresql.org/docs/9.3/static/monitoring-stats.html#PG-STAT-ACTIVITY-VIEW
Is it possible to configure PostgreSQL to automatically close idle connections?
标签:style blog http io color os ar 使用 for
原文地址:http://www.cnblogs.com/xiaotengyi/p/4068877.html