首页 > 数据库技术 > 详细

Is it possible to configure PostgreSQL to automatically close idle connections?

时间:2014-11-02 12:20:38      阅读:161      评论:0      收藏:0      [点我收藏+]

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?

原文:http://www.cnblogs.com/xiaotengyi/p/4068877.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!