web-dev-qa-db-ja.com

PostgreSQLエラー:これ以上の接続は許可されません

クライアントアプリケーションによって適切に閉じられていないPostgreSQL接続をどのように解放しますか?

マルチプロセスを起動するデータマイニングアプリがあります。すべてローカルPostgreSQL 9.1データベースに接続してデータを取得します。数時間は正常に動作しますが、エラーで終了します。

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

これを調査すると、アプリが接続を適切に閉じていないことが原因である可能性が高いことがわかります。ただし、アプリが強制終了された場合でも、これらの接続が解放されることはありません。 PostgreSQLが自動的に接続を閉じるようなタイムアウトはありませんか?

また、Postgresのmax_connectionsを100から200に増やしてみましたが、再起動するとエラーが発生しました。

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.

私のシステムはUbuntu 12.04で、8 GBのメモリがあり、他のすべてのPG設定はデフォルトであるため、システムに十分なメモリがないと思われる理由がわかりません。

次に、pgbouncerを使用して接続をプールして再利用しようとしました。これは少しうまく機能するように見えましたが、これでも結局接続が不足し、エラーが発生しました:

ERROR:  no more connections allowed

この問題をさらに診断して修正するにはどうすればよいですか?

20
Cerin

最大共有メモリ設定を変更することで最大接続数を増やすことができますが、接続が閉じられないことが問題である場合は、実際に解決する必要があります。ソフトウェアが制御不能であり、接続を閉じないためバグがある場合は、次のようなcronジョブを使用できます。

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

これは、同様のバグのあるソフトウェアからのリーク接続を殺すために行うことです。

または、pgpoolなどのアイドル接続を強制終了する同様の機能を持つ接続プールを介してバグのあるソフトウェアを実行できる場合があります。

:Postgresの新しいバージョンでは、列名が少し異なります。

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

新しいバージョンの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'
;

上記は、アイドル接続を終了するのに役立ちます。私は同じ問題を抱えていましたが、私のFlaskとデータベースに接続するSQLAlchemyの方法に問題があることが判明しました。

* senameはタイプミスではありません

4
gogasca

PGドキュメント カーネルリソースの管理について。 これ は、カーネルのメモリ制限を増やすのに役立ちます。

0
vedic