Skip to content

Instantly share code, notes, and snippets.

@timothee-alby
Created August 2, 2022 09:52
Show Gist options
  • Save timothee-alby/0601fdcb7a3c7206519f3d8218a02b3e to your computer and use it in GitHub Desktop.
Save timothee-alby/0601fdcb7a3c7206519f3d8218a02b3e to your computer and use it in GitHub Desktop.
Postgres Emergency Commands

With Heroku's pg-extra

On Heroku, the pg-extras CLI Plugin is available.

$ heroku pg:long-running-queries

List currently running queries that have been running for longer than 5 minutes. Heroku dynos have a 30-seconds disconnect so queries still running after 5 mins are usually problematic and can slow down the entire DB.

$ heroku pg:blocking

List queries that are currently holding locks that other queries are waiting to be released. Long-running locks can block an entire table.

$ heroku pg:locks

List queries that have taken out an exclusive lock on a relation.

> SELECT pg_cancel_backend(<pid of the process>)

Kill query.

> SELECT pg_terminate_backend(<pid of the process>)

Force-kill query.

With pg-top

On any DB, use my pg-top CLI Plugin.

$ pgtop queries:long-running

List currently running queries that have been running for longer than 10 seconds.

$ pgtop queries:blocking

List queries that are currently holding locks that other queries are waiting to be released.

$ pgtop queries:idle

List idle queries.

$ pgtop queries:cancel PID

Kill query.

$ pgtop queries:terminate PID

Force-kill query.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment