Postgres raise `remaining connection slots are reserved for non-replication superuser connections` error
The Reporting service raised the following error today:
FATAL: remaining connection slots are reserved for non-replication superuser connections
I check the database activity and relevant configurations via the following commands:
check current using connection number:
select count(1) from pg_stat_activity;
check all the connections(not include current client connection):
select datname, pid, application_name, state from pg_stat_activity; where pid<>pg_backend_pid();
show max connections limit
show max_connections;
show system user reserved connections
show superuser_reserved_connections;
Solutions
-
kill all the idel connection in the database:
select pg_terminate_backend(pid) from pg_stat_activity where pid<>pg_backend_pid() and state='idle';
-
Modify to increase the max_connections in Postgresql configuration file
in /var/lib/pgsql/{version}/data/postgresql.conf
change:
max_connections = 200 # 100 is default value
shared_buffers = 50MB # 24 is default value, a reasonable starting value is 25% of the memory in the system -
Increase the kernel max segment size slightly larger than shared_buffers
in /etc/sysctl.conf, set:
kernel.shmmax=62914560 #(60MB)
this takes effect when postgresql reboot