1)查询当前所有连接的状态
select datname,pid,application_name,state from pg_stat_activity;
2)关闭当前state为 idle 空闲状态的连接
查看数据库剩余连接数:
select max_conn-now_conn as resi_conn from (select setting::int8 as max_conn,(select count(*) from pg_stat_activity) as now_conn from pg_settings where name = 'max_connections') t;
查看为超级用户保留的连接数:
show superuser_reserved_connections;
psql: FATAL: 53300: sorry, too many clients already
数据库连接已满,无法建立新的连接。
1、关闭空闲连接
select datname,pid,application_name,state from pg_stat_activity;
--查看目前所有的连接的进程id、应用名称、状态。
select pg_terminate_backend(pid) from pg_stat_activity;
--通过pid终止空闲连接
当前总共正在使用的连接数:
select count(1) from pg_stat_activity;
显示系统允许的最大连接数
show max_connections;
显示系统保留的用户数
show superuser_reserved_connections ;
–按照用户分组查看
select usename, count(*) from pg_stat_activity group by usename order by count(*) desc;
结束连接的进程
SELECT pg_terminate_backend(15278);
--杀死所有idle的进程:
postgres=# select pg_terminate_backend(pid) from pg_stat_activity where state=’idle’;
pg_terminate_backend
t
t
(2 rows)