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


19

クライアントアプリケーションによって適切に閉じられていない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で、8GBのメモリがあり、他のすべてのPG設定はデフォルトです。そのため、システムに十分なメモリがないと考える理由はわかりません。

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

ERROR:  no more connections allowed

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


pg_stat_activityの出力を表示
ETL

回答:


8

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

これは9.6で動作しますか?私は私が取得しようとするとpostgres=> select pg_terminate_backend(procpid) from pg_stat_activity where current_query = '<IDLE>' and query_start < current_timestamp - interval '5 minutes'; ERROR: column "procpid" does not exist LINE 1: select pg_terminate_backend(procpid) from pg_stat_activity ...
魔術

それはだpidの代わりに、procpid新しいバージョンで。
ETL

4

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のデータベース接続方法に問題があることが判明しました。

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


弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.