Opened 5 years ago

Closed 5 years ago

#31518 closed Bug (invalid)

Postgres contrib app creates a connection for _all_ configured DBs on ready.

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 Curtis Maloney)

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.

Change History (3)

comment:1 by Curtis Maloney, 5 years ago

Description: modified (diff)

comment:2 by Curtis Maloney, 5 years ago

Description: modified (diff)

comment:3 by Curtis Maloney, 5 years ago

Resolution: invalid
Status: assignedclosed

Seems I misinterpreted this code, and it's creating DatabaseWrapper instances, but not necessarily connections. (calling it conn confused me).

Note: See TracTickets for help on using tickets.
Back to Top