Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save andreyshuster/eaa9f542da1e832a4ce4571540866e72 to your computer and use it in GitHub Desktop.
Save andreyshuster/eaa9f542da1e832a4ce4571540866e72 to your computer and use it in GitHub Desktop.
Batch kill queries by condition

There is two commands to kill query Soft kill: pg_cancel_backend(pid) Hard kill pg_terminate_backend(pid)

SELECT
	pg_terminate_backend(pid)
FROM
	pg_stat_activity
WHERE
	pid <> pg_backend_pid()
	AND query LIKE '%<CONDITION>%'
	AND state = 'active'
	and(now() - pg_stat_activity.query_start) > interval '5 minutes';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment