Override Postgresql backend to use AsyncConnectionPool

Hey folks,

We recently migrated to Ninja for Async APIs, and we faced issues where connections weren’t being closed by django, leading to connection errors on postgres, with 5.1m Django offers a ConnectionPool layer to use pooling but it doesnt support Async contexts, Psycopg does expose an AsyncConnectionPool wrapper but django has no option to use it currently.

Looking at the source code, I just need to override pool method in DatabaseWrapper and then override load_backend method in djanggo/db/utils

My question is, is this a good way to override django’s db setup for async connection pooling? are there any unforseen issues we havent taken into account and is there anything else we can do?

Remaining options for us are:

  1. Celery task which calls close_old_connections
  2. use pg_bouncer which will manage this for us
1 Like

I doubt that this would work as you whish. Because the close_old_connections will be executed in the worker (process) which runs the task, if you have any leftover connection from any other source it will still be hanging where it is.

I am also interested in away to handle this in a way which please both django and postgres (without using pg-bouncer).

yeah you are right, I just took a look at the source code, its at worker level, not at db level, One thing that might help is idle_session_timeout in postgres.conf in your use case