How to Find and Stop Running Queries on PostgreSQL

  • This process has saved me from an outage more than once.

    I keep a dashboard of running pids, and get alerts when any of them take more than N minutes (excepting autovacuum). Once every few months I have to psql into prod and cancel one.

  • Quote: "Windows does not have a kill command..."

    Yes, she does. It's called "taskkill"

  • https://github.com/dalibo/pg_activity ('a top-like application for PostgreSQL server activity monitoring') is quite useful.

  • This is really nice, are there any preventative measures one could take as well? For example setting a max query run time or something like that?

  • Ooohh. That pg_blocking_pids(blockedpid) function is a nice one to add to the arsenal.

  • How can you find and stop users which are accessing a certain table?

  • There’s always “sudo reboot”, too :)