Skip to content

Instantly share code, notes, and snippets.

@seahrh
Created October 26, 2018 16:48
Show Gist options
  • Save seahrh/4b19e2f0bca38f3fb246529d380a403f to your computer and use it in GitHub Desktop.
Save seahrh/4b19e2f0bca38f3fb246529d380a403f to your computer and use it in GitHub Desktop.
pg see and close connections
SELECT
pg_terminate_backend(pid)
FROM
pg_stat_activity
WHERE
-- don't kill my own connection!
pid <> pg_backend_pid()
-- don't kill the connections to other databases
AND datname = 'analytics'
;
SELECT * FROM pg_stat_activity where datname='analytics' order by state;
/*pga4dash*/
SELECT
pid,
datname,
usename,
application_name,
client_addr,
to_char(backend_start, 'YYYY-MM-DD HH24:MI:SS TZ') AS backend_start,
state,
wait_event_type || ': ' || wait_event AS wait_event,
pg_blocking_pids(pid) AS blocking_pids,
query,
to_char(state_change, 'YYYY-MM-DD HH24:MI:SS TZ') AS state_change,
to_char(query_start, 'YYYY-MM-DD HH24:MI:SS TZ') AS query_start
FROM
pg_stat_activity
ORDER BY pid
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment