Created
December 17, 2019 06:30
-
-
Save stultus/afc9816676638ae6446c0ed0e8c11d87 to your computer and use it in GitHub Desktop.
Handle idle trasnactions in postgress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
See transactions that are idle for 15 mins | |
----------------------------------------- | |
SELECT | |
pid, | |
now() - pg_stat_activity.query_start AS duration, | |
query, | |
state | |
FROM pg_stat_activity | |
WHERE (now() - pg_stat_activity.query_start) > interval '15 minutes'; | |
Terminate transactions that are idle for 15 mins | |
--------------------------------------------------- | |
SELECT pg_terminate_backend(pid) | |
FROM pg_stat_activity | |
WHERE datname = 'entri' | |
AND pid <> pg_backend_pid() | |
AND state in ('idle', 'idle in transaction', 'idle in transaction (aborted)', 'disabled') | |
AND state_change < current_timestamp - INTERVAL '15' MINUTE; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment