10
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【PostgreSQL】実行中のプロセスを停止する方法

Last updated at Posted at 2018-09-14

何らかの理由で実行中のプロセスが溜まり、動作が遅くなる症状が発生した時の対応手順です。

##実行中のプロセスを確認
下記のSQLで実行中のプロセスを経過時間順に取得します。

実行中のプロセスIDを取得
SELECT
    procpid,
    start,
    now() - start AS lap,
    current_query
FROM
    (SELECT
        backendid,
        pg_stat_get_backend_pid(TMP1.backendid) AS procpid,
        pg_stat_get_backend_activity_start(TMP1.backendid) AS start,
        pg_stat_get_backend_activity(TMP1.backendid) AS current_query
    FROM
        (SELECT pg_stat_get_backend_idset() AS backendid) AS TMP1
    ) AS TMP2
WHERE
    current_query <> ''
ORDER BY
    lap DESC;

##プロセスを停止する
下記のSQLを実行し、プロセスを停止します。
[procpid]は上記のSQLで得られたprocpidを入れます。
lap(経過時間)とクエリの内容(current_query)を見て、停止しても問題ないプロセスを選びました。

プロセスを停止
SELECT pg_cancel_backend([procpid]);

または、

プロセスを強制的に停止
SELECT pg_terminate_backend([procpid]);

*処理の成否がbool値で返ってきます。

10
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?