In the dashboard I see there are currently 22 open connections to the DB instance, blocking new connections with the error:
remaining connection slots are reserved for non-replication superuser connections.
I'm accessing the DB from web service API running on EC2 instance and always keep the best practise of:
Connection connection = DriverManager.getConnection(URL, USER_NAME, PASSWORD); Class.forName(DB_CLASS); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(SQL_Query_String); ... resultSet.close(); statement.close(); connection.close();
Can I do something else in the code?
Should I do something else in the DB management?
Is there a way to periodically close connections?
Go to /opt/lce/db/postgresql and edit the postgresql. conf file. Look for max_connections parameter (should be 40 as default). Increase it to 50, reboot the server and try to run list-client again.
By default, PostgreSQL supports 115 concurrent connections, 15 for superusers and 100 connections for other users. However, sometimes you may need to increase max connections in PostgreSQL to support greater concurrency.
If you want to see how many idle connections you have that have an open transaction, you could use: select * from pg_stat_activity where (state = 'idle in transaction') and xact_start is not null; This will provide a list of open connections that are in the idle state, that also have an open transaction.
PgBouncer is a light-weight connection pool manager for Greenplum and PostgreSQL. PgBouncer maintains a pool for connections for each database and user combination. PgBouncer either creates a new database connection for a client or reuses an existing connection for the same user and database.
Amazon has to set the number of connections based on each model's right to demand a certain amount of memory and connections
MODEL max_connections innodb_buffer_pool_size --------- --------------- ----------------------- t1.micro 34 326107136 ( 311M) m1-small 125 1179648000 ( 1125M, 1.097G) m1-large 623 5882511360 ( 5610M, 5.479G) m1-xlarge 1263 11922309120 (11370M, 11.103G) m2-xlarge 1441 13605273600 (12975M, 12.671G) m2-2xlarge 2900 27367833600 (26100M, 25.488G) m2-4xlarge 5816 54892953600 (52350M, 51.123G)
But if you want you can change the max connection size to custom value by
From RDS Console > Parameter Groups > Edit Parameters,
You can change the value of the max_connections parameter to a custom value.
For closing the connections periodically you can setup a cron job some thing like this.
select pg_terminate_backend(procpid) from pg_stat_activity where usename = 'yourusername' and current_query = '<IDLE>' and query_start < current_timestamp - interval '5 minutes';
I'm using Amazon RDS, SCALA, Postgresql & Slick. First of all - number of available connections in RDS depends on the amount of available RAM - i.e. size of the RDS instance. It's best not to change the default conn number.
You can check the max connection number by executing the following SQL statement on your RDS DB instance:
show max_connections;
Check your SPRING configuration to see how many threads you're spawning:
database { dataSourceClass = org.postgresql.ds.PGSimpleDataSource properties = { url = "jdbc:postgresql://test.cb1111.us-east-2.rds.amazonaws.com:6666/dbtest" user = "youruser" password = "yourpass" } numThreads = 90 }
All of the connections ARE made upon SRING BOOT initialization so beware not to cross the RDS limit. That includes other services that connect to the DB. In this case the number of connections will be 90+.
The current limit for db.t2.small is 198 (4GB of RAM)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With