Skip to content

Instantly share code, notes, and snippets.

@marcotc
Last active February 7, 2018 17:02
Show Gist options
  • Save marcotc/6655b8af1ed2d5fd69856e7cf020a03f to your computer and use it in GitHub Desktop.
Save marcotc/6655b8af1ed2d5fd69856e7cf020a03f to your computer and use it in GitHub Desktop.
Find long running queries (an kill them if you wish)
ActiveRecord::Base.connection.exec_query("SELECT pid, age(query_start, clock_timestamp()), state, query FROM pg_stat_activity WHERE query != '<IDLE>' AND query NOT ILIKE '%pg_stat_activity%' and state = 'active' ORDER BY age(query_start, clock_timestamp())")
#ActiveRecord::Base.connection.exec_query("select pg_cancel_backend(123456)") # Kill by PID
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment