開発者ドキュメント

PostgreSQLのハングクエリを強制終了/終了する方法

今日、私は、SQLクエリの1つがPostgreSQLにぶら下がり、決してそれ自体をリリースしないことを発見しました。 “Cancel query”コマンドは役に立ちません。クエリがそこにぶら下がり、 ”

idle in transaction

“ステータスを表示します。それは私に選択肢を残しましたが、手動で終了させるために “kill”コマンドを発行するDebianターミナルに行きました。

PostgreSQLでクエリがハングしている、または応答していないのは、Webアプリケーションでトランザクションマネージャを適切に処理しなかったためです。誤ってシステムがシャットダウンした場合、実行中のクエリはPostgreSQLで停止し、トランザクションマネージャ(例えば、

DataSourceTransactionManager

)は、進行中のトランザクションをロールバックすることはできません。

ただしここでは、ハングするSQLクエリをターミナルする方法を示します。 PostgreSQLでは、すべてのハングしているクエリは ”

idle in transaction

“と表示されます。

まず、すべての既存のPostgreSQLプロセスをリストアウトし、kill terminateコマンドを発行して、ハングするクエリを手動で終了する必要があります。

1.すべてのプロセスを一覧表示する

問題 “ps -ef | grep postgres`コマンドを実行すると、既存のプロセスがすべてpostgresユーザーに属していることが表示されます。

mkyong:~# ps -ef | grep postgres
postgres 13648     1  0 11:04 ?        00:00:00/var/lib/postgresql/PostgresPlus8.3xxxxxxx
postgres 13651 13648  0 11:04 ?        00:00:00 postgres: logger process
postgres 13653 13648  0 11:04 ?        00:00:00 postgres: writer process
postgres 13654 13648  0 11:04 ?        00:00:00 postgres: wal writer process
postgres 13655 13648  0 11:04 ?        00:00:00 postgres: autovacuum launcher process
postgres 13656 13648  0 11:04 ?        00:00:00 postgres: stats collector process
postgres 13668 13648  0 11:04 ?        00:00:00 postgres: postgres postgres 10.70.1.27(3734) idle
postgres 13689 13648  0 11:05 ?        00:00:00 postgres: usrdba db__test 10.70.1.67(4164) idle
postgres 13714 13648  0 11:06 ?        00:00:00 postgres: usrdba db__test 10.70.0.61(57586) idle in transaction
postgres 13721 13648  0 11:06 ?        00:00:16 postgres: usrdba db__test 10.70.1.67(4165) idle
postgres 13832 13648  0 11:10 ?        00:00:00 postgres: usrdba db__test 10.70.0.61(57592) idle
postgres 13833 13648  0 11:10 ?        00:00:00 postgres: usrdba db__test 10.70.0.61(57593) idle
postgres 13834 13648  0 11:10 ?        00:00:00 postgres: usrdba db__test 10.70.0.61(57594) idle
postgres 13835 13648  0 11:10 ?        00:00:00 postgres: usrdba db__test 10.70.0.61(57595) idle
postgres 13836 13648  0 11:10 ?        00:00:00 postgres: usrdba db__test 10.70.0.61(57596) idle
postgres 14201 13648  0 11:23 ?        00:00:00 postgres: nrsdba postgres 10.70.1.8(4419) idle
postgres 14202 13648  0 11:23 ?        00:00:00 postgres: usrdba db__test 10.70.1.8(4420) idle
postgres 14207 13648  0 11:24 ?        00:00:00 postgres: usrdba db__test 10.70.1.8(4421) idle
root     18030 17992  0 13:46 pts/0    00:00:00 grep postgres
mkyong:~#

2.アイドル状態のトランザクション+ Killを検索する

プロセスID「

13714、トランザクションのアイドル

」を通知します。これはPostgreSQLのハング・クエリです。 “kill”コマンドを発行してPostgreSQLプロセスを手動で終了します。

mkyong:~# kill 13714

または

mkyong:~# kill -TERM 13714

または

mkyong:~# kill -15 13714

3.殺された!完了しました.

完了すると、ハングするクエリはなくなりました。

mkyong:~# ps -ef | grep postgres
postgres 13648     1  0 11:04 ?        00:00:00/var/lib/postgresql/PostgresPlus8.3xxxxxxx
postgres 13651 13648  0 11:04 ?        00:00:00 postgres: logger process
postgres 13653 13648  0 11:04 ?        00:00:00 postgres: writer process
postgres 13654 13648  0 11:04 ?        00:00:00 postgres: wal writer process
postgres 13655 13648  0 11:04 ?        00:00:00 postgres: autovacuum launcher process
postgres 13656 13648  0 11:04 ?        00:00:00 postgres: stats collector process
postgres 13668 13648  0 11:04 ?        00:00:00 postgres: postgres postgres 10.70.1.27(3734) idle
postgres 13689 13648  0 11:05 ?        00:00:00 postgres: usrdba db__test 10.70.1.67(4164) idle
postgres 13721 13648  0 11:06 ?        00:00:16 postgres: usrdba db__test 10.70.1.67(4165) idle
postgres 13832 13648  0 11:10 ?        00:00:00 postgres: usrdba db__test 10.70.0.61(57592) idle
postgres 13833 13648  0 11:10 ?        00:00:00 postgres: usrdba db__test 10.70.0.61(57593) idle
postgres 13834 13648  0 11:10 ?        00:00:00 postgres: usrdba db__test 10.70.0.61(57594) idle
postgres 13835 13648  0 11:10 ?        00:00:00 postgres: usrdba db__test 10.70.0.61(57595) idle
postgres 13836 13648  0 11:10 ?        00:00:00 postgres: usrdba db__test 10.70.0.61(57596) idle
postgres 14201 13648  0 11:23 ?        00:00:00 postgres: nrsdba postgres 10.70.1.8(4419) idle
postgres 14202 13648  0 11:23 ?        00:00:00 postgres: usrdba db__test 10.70.1.8(4420) idle
postgres 14207 13648  0 11:24 ?        00:00:00 postgres: usrdba db__test 10.70.1.8(4421) idle
root     18030 17992  0 13:46 pts/0    00:00:00 grep postgres
mkyong:~#
モバイルバージョンを終了