查询当前连接
bash
select * from pg_stat_activity;
select count(1) from pg_stat_activity;
select pid, datname, application_name, client_addr, state from pg_stat_activity where state in ('idle', 'active');
查询最大连接数
bash
show max_connections;
SELECT name, setting FROM pg_settings WHERE name = 'max_connections';
释放空闲连接
bash
SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE state='idle';
bash
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE state = 'idle' -- 或者 'idle in transaction'
AND pid <> pg_backend_pid(); -- 避免断开自己的连接