大网数据广告活动
服务器运维问答

PostgreSQL ERROR: no more connections allowed

· 52CE 我爱测

How do you free up PostgreSQL connections that aren't properly being closed by client applications? I have a data-mining app that spins up multi processes, all connection to a local PostgreSQL 9.1 database to retrieve data. It ru…

问题原文

原帖链接 · Cerin · CC BY-SA 3.0

How do you free up PostgreSQL connections that aren't properly being closed by client applications?

I have a data-mining app that spins up multi processes, all connection to a local PostgreSQL 9.1 database to retrieve data. It runs fine for a few hours, but then dies with the error:

FATAL:  remaining connection slots are reserved for non-replication superuser connections

Researching this shows that this is most likely caused by the app not properly closing it's connections. However, even with the app killed, these connections are never freed. Isn't there some sort of timeout where PostgreSQL will automatically close a connection?

I also tried increasing Postgres's max_connections from 100 to 200, but restarting gave me the error:

2014-02-23 10:51:15 EST FATAL:  could not create shared memory segment: Invalid argument
2014-02-23 10:51:15 EST DETAIL:  Failed system call was shmget(key=5432001, size=36954112, 03600).
2014-02-23 10:51:15 EST HINT:  This error usually means that PostgreSQL's request for a shared memory segment exceeded your kernel's SHMMAX parameter.  You can either reduce the request size or reconfigure the kernel with larger SHMMAX.  To reduce the request size (currently 36954112 bytes), reduce PostgreSQL's shared memory usage, perhaps by reducing shared_buffers or max_connections.
    If the request size is already small, it's possible that it is less than your kernel's SHMMIN parameter, in which case raising the request size or reconfiguring SHMMIN is called for.
    The PostgreSQL documentation contains more information about shared memory configuration.

My system is Ubuntu 12.04 and has 8GB of memory, and all other PG settings are defaults, so I'm not sure why it thinks the system doesn't have enough memory.

I then tried to use pgbouncer to pool and reuse connections. This seemed to work a little better, but even this eventually ran out of connections, giving me the error:

ERROR:  no more connections allowed

How do I further diagnose and fix this problem?

社区高分解答

原帖链接 · gogasca · CC BY-SA 4.0

For newer versions of PostgreSQL:

select pg_terminate_backend(pid)
from pg_stat_activity
where usename = 'YOUR_DATABASE_USERNAME*'
 and state = 'idle'
 and query_start < current_timestamp - interval '5 minutes'
;

The above will help you terminate your idle connections. I had the same issue but it turned out to be a problem with my Flask and SQLAlchemy way to connect to database.

*usename is not a typo

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2014-02-23)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。