I went into this issue on purpose by scaling up a dockerized Python application 50x using this command docker-compose up --scale app=50 app
:
This app connects to a single PostgreSQL database (also as a Docker container).
As monitored by pgAdmin4, the server activity is as follow:
I've read that PostgreSQL allows a number of maximum concurrent connections of 100
by default. This should be fine because my app is "only" scaled by a factor of x50.
But I have plenty of such messages from Python:
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) FATAL: sorry, too many clients already
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) FATAL: sorry, too many clients already
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) FATAL: sorry, too many clients already
plus this one sometimes:
psycopg2.errors.DiskFull: could not resize shared memory segment "/PostgreSQL.1997913688" to 196736 bytes: No space left on device
The computer on which I'm running this application has 256 GB of RAM and 2x 1TB almost empty hard drives. I've also noticed that the RAM is only used at ~30%-40% max when the app is actively writing data to the database.
I can then change the max_connections
parameters but I went through a nice article on this topic, and it seems tricky to set up correctly, so I prefer not to touch the PostgreSQL default's configuration (unless absolutely needed of course): https://www.cybertec-postgresql.com/en/tuning-max_connections-in-postgresql/
But prior to changing anything in the config, or studying more about pgBouncer which may be better suited here(?), I need to understand why when scaling up my app by a factor of "only" 50x, the PostgreSQL max_connections
value, which is 100
, is overtaken?
Any hints on this?
Version: I am using postgis/postgis:13-3.1
docker image for the database.
docker-compose up --scale app=50 app
Main post modified accordingly. Thanks.