As part of troubleshooting performance problems in PostgreSQL, you may need to terminate a query which is taking a long time to run.
To do this, find the Postgres process which is currently executing the query:
SELECT procpid, current_query, query_start
FROM pg_stat_activity;
The procpid
column displays the Operating System's process ID that can then be used to send the INT
signal which will cause Postgres to roll the query back and move on to the next one:
kill -INT procpid
If this happens regularly, you can also tell Postgres to kill any query which takes longer than a given number of milliseconds (say 1 hour) by using this setting:
statement_timeout = 3600000
Or written in a less confusing way:
statement_timeout = 1h
query:
select / some_UUID_here / ... from ...
Then, to cancel, you could issue:
with q as (select pid from pg_stat_activity where query ilike '% some_UUID_here %') select pg_cancel_backend(pid) from q