Introduction
From time to time we need to investigate if there is any query running indefinitely on our PostgreSQL database. These long running queries may interfere on the overall database performance and probably they are stuck on some background process.
How-To
In order to find them you can use the following query:
- SELECT
- pid,
- now() - pg_stat_activity.query_start AS duration,
- query,
- state
- FROM pg_stat_activity
- WHERE (now() - pg_stat_activity.query_start) > interval '5 minutes';
Notes.
In order to cancel these long running queries you should execute:
- SELECT pg_cancel_backend(__pid__);
- SELECT pg_terminate_backend(__pid__);
Supplement
* PostgreSQL Doc - System Administration Functions
沒有留言:
張貼留言