My Cloud SQL instance is showing many more connections than I would expect:
I have a python cloud function that I've set the following properties for when connecting to the Postgres Cloud Sql instance:
connection = sqlalchemy.create_engine(
# Equivalent URL:
# postgresql+pg8000://<db_user>:<db_pass>@/<db_name>
# ?unix_sock=<socket_path>/<cloud_sql_instance_name>/.s.PGSQL.5432
sqlalchemy.engine.url.URL.create(
drivername="postgresql+pg8000",
username=db_user,
password=db_pass,
database=db_name,
query={
"unix_sock": "{}/{}/.s.PGSQL.5432".format(
db_socket_dir, # e.g. "/cloudsql"
instance_connection_name) # i.e "<PROJECT-NAME>:<INSTANCE-REGION>:<INSTANCE-NAME>"
}
),
# Pool size is the maximum number of permanent connections to keep.
pool_size=1,
# Temporarily exceeds the set pool_size if no connections are available.
max_overflow=0,
# echo=True
)
I believe since the pool size is set to 1, the # of connections on the Cloud SQL side, should be roughly the same as the cloud function instance count:
My two other cloud functions are PHP which according to these two stack overflow posts: Connection pooling in PHP How to establish PHP PDO connection with pooling
don't have connection pooling.
The Instance Count charts for these look like the following:
In total, I would expect a max connection count of around 20 but it is consistently getting up to 50+, so I'm looking for any ideas on other causes.