Opened 5 years ago
Last modified 5 years ago
#31518 closed Bug
Postgres contrib app creates a connection for _all_ configured DBs on ready. — at Version 1
Reported by: | Curtis Maloney | Owned by: | |
---|---|---|---|
Component: | contrib.postgres | Version: | 3.0 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
Whilst helping a client debug why a fresh deploy had created hundreds of connections, I stumbled upon this.
In django.contrib.postgres.apps.PostgresConfig
the ready
function iterates django.db.connections
, checking if each connection has vendor == 'postgres'.
This creates a connection per configured DB. It then does nothing to close them afterwards.
This means the first connection to all DBs is once Django's setup is complete, not the first request.
The issue of not closing connections seems irrelevant, since django _should_ close them itself after requests; however, this is not the case for use of the ORM from other jobs [for instance, task queues].
It's also not unreasonable to believe there are cases where a system is built on the assumption DB connections will not be created until the first request. This expectation is violated by this code.
## Possible steps:
- document this behavor - we should do this anyway
- make the
ready
function get DB Backends and test theirvendor
attribute _before_ getting a connection. - make the
ready
function _close_ connections once done.
I have begun a patch for (2), and will implement the rest once someone agrees with this solution.