1、use pgbouncer

As new connections/transactions/statements arrive, the pool will increase in size up to the
defined user maximums. Those connections will stay around for at most server_idle_timeout
before the pool releases those connections.
pgbouncer also releases sessions every server_lifetime . This allows the server to free
backends in rotation to avoid issues with very long-lived session connections.

可以利用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 =
server_lifetime:
;; Close server connection if its been connected longer.
;server_lifetime = 1200

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 '' 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

http://2ndquadrant.com/en/books/postgresql-9-administration-cookbook/

Is it possible to configure PostgreSQL to automatically close idle connections?的更多相关文章

  1. PostgreSQL源码安装文档

    This document describes the installation of PostgreSQL using the source    code distribution. (If yo ...

  2. postgreSqL的序列sequence

    PostgreSQL uses sequences to generate values for serial columns and serial columns are generally wha ...

  3. Dockerize PostgreSQL

    Dockerize PostgreSQL Installing PostgreSQL on Docker Assuming there is no Docker image that suits yo ...

  4. Install your Application into RaspberryPi2 and automatically start up

    如何安装和卸载应用程序到RaspberryPi2? 如何配置应用程序在RaspberryPi2开机后自动启动? How to install your app into RaspberryPi2? H ...

  5. Step 4: Install and Configure Databases

    https://www.cloudera.com/documentation/enterprise/latest/topics/cm_ig_installing_configuring_dbs.htm ...

  6. How To Use PostgreSQL with Your Ruby on Rails Application on Ubuntu 14.04

    How To Use PostgreSQL with Your Ruby on Rails Application on Ubuntu 14.04 链接来自于:https://www.digitalo ...

  7. Configure Security Settings for Remote Desktop(RDP) Services Connections

    catalogue . Configure Server Authentication and Encryption Levels . Configure Network Level Authenti ...

  8. nginx HttpLuaModule

    http://wiki.nginx.org/HttpLuaModule#Directives Name ngx_lua - Embed the power of Lua into Nginx This ...

  9. CentOS7系列--5.1CentOS7中配置和管理KVM

    CentOS7配置和管理KVM 安装与配置虚拟化软件KVM ( Kernel-based Virtual Machine ) + QEMU,它要求计算机的CPU支持Intel VT or AMD-V功 ...

随机推荐

  1. poj1992 数论

    //Accepted 168 KB 969 ms //n!中含有质因数p的个数为t=n/p+n/p^2+n/p^3+... #include <cstdio> #include <c ...

  2. libimobiledevice安装步骤

    https://github.com/libimobiledevice/libimobiledevice libimobiledevice安装指南,你还不知道libimobiledevice为何物,赶 ...

  3. 在某个目录下的所有文件中查找包含某个字符串的Windows命令

    findstr可以完成这个工作.   上面的命令表示,当前目录以及当前目录的所有子目录下的所有文件中查找"string"这个字符串. *.*表示所有类型的文件. /s 表示当前目录 ...

  4. iOS 获取IP地址

    一.获取本机IP地址 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #import <ifadd ...

  5. BZOJ 1630/2023 Ant Counting 数蚂蚁

    DP. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> ...

  6. Reason we use Camel

    Camel is mainly for integration purpose, in our project we also use it inside the single component t ...

  7. Entityframework更新数据和插入数据

    public bool UpdateEmloyeeInfo(EmployeInfo employeInfo) { bool flg = false; try { using (UserManageDB ...

  8. win7共享wifi

    已管理员身份打开 命令提示符 netsh wlan set hostednetwork mode=allow ssid=abcde key=123456 netsh wlan start hosted ...

  9. Sqlserver CheckPoint 在三种恢复模式中的不同表现

    准备: 日志截断在下列情况下发生: 1.执行完 BACKUP LOG 语句时.2.在每次处理检查点时(如果数据库使用的是简单恢复模式).这包括 CHECKPOINT 语句所产生的显式检查点和系统生成的 ...

  10. Sqlserver 远程连接的 TCP/IP 和 Named Pipes的区别

    TCP/IP:  TCP/IP是 Internet 上广泛使用的通用协议.它与互连网络中硬件结构和操作系统各异的计算机进行通信.TCP/IP包括路由网络流量的标准,并能够提供高级安全功能.它是目前在商 ...