Opened 4 years ago

Last modified 4 years ago

#31518 closed Bug

Postgres contrib app creates a connection for _all_ configured DBs on ready. — at Version 2

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 (2)

comment:1 by Curtis Maloney, 4 years ago

Description: modified (diff)

comment:2 by Curtis Maloney, 4 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top