-1

I have a Flask Application and I am using SQLAlchemy as an ORM. Unfortunately, Im running into the problem that I get the error: "psql: FATAL: remaining connection slots are reserved for non-replication superuser connections".

I have increased the max_connections to 25 but the problem remains. I check the pg_stat_activity and I am seeing that the connections are idling so I kill them which temporarily solves the problem until I hit the wall again.

My question is - How can I start debugging this? I am a bit lost where to look for hints.

Al Nikolaj
  • 305
  • 4
  • 13
  • 1
    Probably sessions are not being closed correctly. But it's impossible to tell without a [mre]. – snakecharmerb Jun 05 '21 at 18:22
  • @snakecharmerb Thank you for your reply. I would love to add example, unfortunately I wouldn't know what part of my code to post (the project is already quite big), the issue happens at random and I wouldn't know what queries are leaving the DB connection occupied. Do you happen to know how I could approach this to find at least more information to post for you guys? – Al Nikolaj Jun 05 '21 at 18:33
  • where did you set `max_connections` on postgres? – Talha Junaid Jun 06 '21 at 06:18
  • @TalhaJunaid, yes - on the managed Postgres Database under pool connection I have maxed out what is possible. – Al Nikolaj Jun 06 '21 at 09:09

1 Answers1

0

you may want to restrict the number of connections. in flask_sqlalchemy you can specify that using SQLALCHEMY_ENGINE_OPTIONS in engine options.

SQLALCHEMY_ENGINE_OPTIONS = {
        # The number of long-running connections to the database to keep open
        'pool_size':  10,
        # This setting causes the pool to recycle connections after the given number of seconds
        # has passed. It defaults to -1, or no timeout.
        'pool_recycle': 10 * 60)
}
Talha Junaid
  • 2,351
  • 20
  • 29