Skip to content

Instantly share code, notes, and snippets.

@mlanett
Last active June 10, 2025 16:47
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
with queries as (
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
from pg_stat_activity
where 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
)
select
*
-- , pg_terminate_backend(pid) -- uncomment to kill
from queries
where query like '%' -- edit as necessary to match your query
-- and age > interval '5 minutes' -- edit as necessary to match your query
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment