#31518: Postgres contrib app creates a connection for _all_ configured DBs on
ready.
----------------------------------+--------------------------------------
     Reporter:  Curtis Maloney    |                    Owner:  (none)
         Type:  Bug               |                   Status:  assigned
    Component:  contrib.postgres  |                  Version:  3.0
     Severity:  Normal            |               Resolution:
     Keywords:                    |             Triage Stage:  Unreviewed
    Has patch:  0                 |      Needs documentation:  0
  Needs tests:  0                 |  Patch needs improvement:  0
Easy pickings:  0                 |                    UI/UX:  0
----------------------------------+--------------------------------------
Description changed by Curtis Maloney:

Old description:

> 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:
>
> 1. document this behavor - we should do this anyway
> 2. make the `ready` function get DB Backends and test their `vendor`
> attribute _before_ getting a connection.
> 3. 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.

New description:

 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 backend 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:

 1. document this behavior - we should do this anyway
 2. make the `ready` function _close_ connections once done.

 I have begun a patch, and will implement the rest once someone agrees with
 this solution.

--

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31518#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/066.ca2bd489d02bdb9093a8d11ab1dc555e%40djangoproject.com.

Reply via email to