Skip to content

Instantly share code, notes, and snippets.

@checkaayush
Last active November 30, 2022 09:27
Show Gist options
  • Save checkaayush/15029ddc9baa2861df8290fefb4a88f5 to your computer and use it in GitHub Desktop.
Save checkaayush/15029ddc9baa2861df8290fefb4a88f5 to your computer and use it in GitHub Desktop.
Finding and killing long running PostgreSQL queries
-- List all long-running queries
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';
-- Soft kill query by process ID
SELECT pg_cancel_backend(<pid of the process>);
-- Hard kill query by process ID
SELECT pg_terminate_backend(<pid of the process>);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment