报错:remaining connection slots are reserved for non-replication superuser connections
当有上述情况产生,可以通过Superuser账号连接实例,执行如下语句查看空闲连接是否过多。
select * from pg_stat_activity where backend_type = 'client backend' and state = 'idle';
如果查询结果显示空闲进程过多,并且确定是无用的空闲连接时,可以找到上述语句结果中的pid字段,并执行如下语句释放空闲连接:
select pg_cancel_backend(<pid>); -- cancel该连接上的queryselect pg_terminate_backend(<pid>); --杀掉对应的后台连接进程
--批量终止后台IDLE连接进程,释放连接
SELECT pg_terminate_backend(pid)
,query
,datname
,usename
,application_name
,client_addr
,client_port
,backend_start
,state
FROM pg_stat_activity
WHERE length(query) > 0
AND pid != pg_backend_pid()
AND backend_type = 'client backend'
AND state = 'idle'
AND application_name != 'hologres'
AND usename != 'holo_admin'
AND query not like '%pg_cancel_backend%';