Skip to content

Instantly share code, notes, and snippets.

@pguelpa
Created March 30, 2015 22:52
Show Gist options
  • Save pguelpa/5935df7e90a46d49f681 to your computer and use it in GitHub Desktop.
Save pguelpa/5935df7e90a46d49f681 to your computer and use it in GitHub Desktop.
PostgreSQL Reaper
DO $$DECLARE row record;
BEGIN
FOR row IN SELECT pid, query FROM pg_stat_activity WHERE query_start < NOW() - interval '30 minutes' LOOP
RAISE NOTICE 'Killing query: %s', row.query;
PERFORM pg_cancel_backend(row.pid);
END LOOP;
END$$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment