Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
    
Show the output of pg_stat_activity –  ETL Feb 23 at 18:02

2 Answers 2

You can increase the max number of connections by changing your max shared memory settings, however if the problem is that your connections are not being closed, then you should really resolve that. If the software is out of your control and is buggy by not closing connections, you can use some cron job like:

select pg_terminate_backend(procpid)
from pg_stat_activity
where usename = 'yourusername'
 and current_query = '<IDLE>'
 and query_start < current_timestamp - interval '5 minutes'
;

That's what I do to kill leaking connections from a similar buggy software.

Alternatively, you may be able to run your buggy software through a connection pool which has a similar functionality to kill idle connections such as pgpool.

share|improve this answer

PG document on managing kernel resources . This should help you increase the memory limit in kernel.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.