sigu
Postgres connection count always increasing, never drops
We have an application running on EC2 which connects to a database on RDS.
Over the past couple of weeks, we noticed the connections to the database were always filling up after which
we were not able to make new requests to the database with the following error
%Postgrex.Error{connection_id: nil, message: nil, postgres: %{code: :too_many_connections, file: "postinit.c", line: "803", message: "remaining connection slots are reserved for non-replication superuser connections", pg_code: "53300", routine: "InitPostgres", severity: "FATAL", unknown: "FATAL"}, query: nil}
The connections always go up in count and rarely drops down to a lower number. On rebooting the server the connection count is reset then it starts to increment again never dropping.
More information
The pool size is set to 10 on the prod.secret.exs.
We can reproduce this by uploading and processing a CSV file on the application.
Questions
- Why are the connections always increasing?
- Is there a way of closing the unused connections or restricting it to a maximum?
Most Liked
outlog
great!
what is the current postgrex code? sounds like you are opening up a db listener per client connection - think you are better off with one long-lived listener that then emits pubsub events…
I’ve used Boltun in the past which gives a nice API - https://github.com/bitgamma/boltun - but better/more recent solutions might be around…
kip
I’m sure more knowledgeable souls will leap in. But what your graph is showing that your RDS instance has run out of connections. If your Ecto Repo pool is 10 connections and you are running out of connections on the DB side at 80 then that suggests:
- Some other app(s) is/are connecting to your DB and using connections
- Your pool isn’t really 10 connections (but its unlikely to be 80!
- Something is causing the Repo to crash and restart but the old connections are still being held open on the DB side (this seems unlikely since you would see log messages). And it would have to crash and restart 8 times!
Maybe that at least gives you some angles to consider …
outlog
is ecto and especially postgrex latest versions?
do you have ec2 auto-scaling enabled? (this would fit the issue/symptoms exactly)…
mcrumm
You can use the terminate/2 callback in your LiveView to unsubscribe from notifications when the user navigates away from the page.








