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. JavaScript常用检测脚本(正则表达式)

    转自:http://www.cnblogs.com/skylaugh/archive/2006/09/25/514492.html 文件名称:check.js 说明:JavaScript脚本,用于检查 ...

  2. (转)iphone数据存储之-- Core Data的使用

    原文:http://www.cnblogs.com/xiaodao/archive/2012/10/08/2715477.html iphone数据存储之-- Core Data的使用(一)   一. ...

  3. PHP中的错误处理和异常处理

    错误处理:    1.语法错误    2.运行时的错误    3.逻辑错误 错误报告:        错误E_ERROR        警告E_WARNING        注意E_NOTICE 开发 ...

  4. JS手机定位地理位置

    /** * 以下为html5代码,获取地理位置 */ /** * 设置地址 */ function setAddress(json) { var position = document.getElem ...

  5. web字体

    <span style="font-family:sans-serif">Lorem Ipsum</span> <span style="f ...

  6. [转]MMORPG服务器架构

    MMORPG服务器架构 一.摘要 1.网络游戏MMORPG整体服务器框架,包括早期,中期,当前的一些主流架构2.网络游戏网络层,包括网络协议,IO模型,网络框架,消息编码等.3.网络游戏的场景管理,A ...

  7. Autoresizing和AutoLayout

    1 使用Autoresizing的方式进行界面布局 1.1 问题 Autoresizing是IOS旧版的自动布局技术,现在仍然被很多企业使用.本案例将学习如何使用Autoresizing完成界面的布局 ...

  8. Ubuntu12.04更新openssl使用源码

    下载openssl新版本源码和编译安装 wget ftp://ftp.openssl.org/source/openssl-1.0.2f.tar.gz tar zxf ./openssl-1.0.2f ...

  9. LeetCode Permutations II (全排列)

    题意: 给出n个元素(可能有重复的),请产生出所有的全排列. 思路: 同版本1的有点不同,这次有可能含有重复的元素,很容易就TLE,节省时间才是关键点. 如果将一个序列中两个相同的元素交换,这个序列是 ...

  10. Event Logging

    编号:1001 时间:2016年3月29日16:24:33 功能:Event Logging 技术简介 URL:http://blog.csdn.net/colorknight/article/det ...