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. 解密SQL SERVER 2005加密存储过程,函数

    在SQL SERVER 2005中必须用专用管理连接才可以查看过程过程中用到的表 EG:sqlcmd -A 1>use test 2>go 1>sp_decrypt 'p_testa ...

  2. 利用烧鹅制作简单BadUSB,插谁谁怀孕

    所用硬件设备为烧鹅,烧鹅是RadioWar基于Teensy++ 2.0 AT90USB1286芯片设计的USB Rubber Ducky类开发板. 使用veil编码meterpreter生成paylo ...

  3. 《view programming guide for iOS 》之可以使用动画效果的属性

    frame—Use this to animate position and size changes for the view.  ,框架,可以视图动态改变大小和位置 bounds—Use this ...

  4. 计算几何----判断空间点是否在一个四面体(tetrahedron)内部

    DESCRIPTION: 判断空间点 P(x, y, z)是否在一个四面体的内部? Let the tetrahedron have vertices V1 = (x1, y1, z1) V2 = ( ...

  5. Git使用详细教程

    参考网址: http://www.admin10000.com/document/5374.html http://blog.sina.com.cn/s/blog_4f3b79d0010166ab.h ...

  6. Windows 2008 R2防火墙,允许被ping

    netsh firewall set icmpsetting 8 1.         准备 1)         原因 出于安全因素考虑,在Windows 2008 R2上是不允许从外部对其Ping ...

  7. CodeForces 416D (贪心)

    Problem Population Size 题目大意 给一个长度为n的序列,由 -1 和正整数组成,-1表示任意的正整数. 将序列分成若干段,使得任意段都是等差数列,求最少段数. 解题分析 可以发 ...

  8. python学习札记(1)

    首先给大家推荐一个很好的python入门网站,感觉比<python基础>之类的书更容易懂,廖雪峰小站,希望有学习资源同学们也能多多交流.下面是今天所学: 下面总结一些非常有特色的函数及其应 ...

  9. 转:服务器控件的 ID,ClientID,UniqueID 的区别

    动态加载用户控件的怪问题 动态加载用户控件的时候,会因为调用一些控件的一些属性和方法而造成控件命名混乱. 因为add 一个用户控件或者 loadcontrol 的时候 如果没有指定控件的id,clie ...

  10. 转:DataTable.Compute()用法

    转自:http://www.cnblogs.com/fanyf/archive/2012/05/11/2495919.html一.DataTable.Compute()方法說明如下 作用: 计算用来传 ...