SQL - Postgres Long running queries

Quick snippet to discover queries that have been running for over 1 minute on Postgres

  
  SELECT 
     pid, 
     usename, 
     wait_event_type, 
     query, 
     datname, 
     state 
  FROM 
     pg_stat_activity 
  WHERE 
      now() - query_start > INTERVAL '1' minute 
    AND 
      datname='database_name'
  

This gives us a break down of the query and the kind of event it's been waiting on.

Previous
Previous

Generating series with Postgres

Next
Next

Kill a running query - Postgres