Skip to content

Instantly share code, notes, and snippets.

@mlanett
Last active October 18, 2022 02:42
Show Gist options
  • Save mlanett/42b6ad06aabffcffe6a5937ad467f1f2 to your computer and use it in GitHub Desktop.
Save mlanett/42b6ad06aabffcffe6a5937ad467f1f2 to your computer and use it in GitHub Desktop.
Find and Kill Queries
SELECT pid,
age(clock_timestamp(), query_start),
usename,
state,
-- get rid of newlines, runs of spaces
regexp_replace(query, E'[\\n\\r ]+', ' ', 'g' ) as query
-- , pg_terminate_backend(pid) -- UNCOMMENT TO KILL
FROM pg_stat_activity
WHERE query LIKE '%' -- Edit as necessary to match your query
AND query NOT LIKE 'SELECT pid,%' -- Don't kill the query-killing query…
AND state != 'idle' -- Idle connections are innocent.
AND usename != 'rdsadmin' -- Can't kill these, don't try.
ORDER BY age DESC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment