Skip to content

Instantly share code, notes, and snippets.

@pwilken
Created July 9, 2019 10:18
Show Gist options
  • Save pwilken/ea54787187268e393ad93f9003913998 to your computer and use it in GitHub Desktop.
Save pwilken/ea54787187268e393ad93f9003913998 to your computer and use it in GitHub Desktop.
PostgreSQL Performance Problems
-- Check active queries
SELECT * FROM pg_stat_activity WHERE state = 'active';
-- Terminate active queries with ProcessID
select pg_terminate_backend(pid);
-- Get all queries which are older then 5 minutes (idle or active)
SELECT pid, now() - pg_stat_activity.query_start AS duration, query, state
FROM pg_stat_activity WHERE (now() - pg_stat_activity.query_start) > interval '5 minutes';
-- Cancel all processes older then 5 minutes
SELECT pg_terminate_backend(queries.pid) from (
SELECT pid, now() - pg_stat_activity.query_start AS duration, query, state
FROM pg_stat_activity WHERE (now() - pg_stat_activity.query_start) > interval '5 minutes'
) AS queries;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment