dj-database-url icon indicating copy to clipboard operation
dj-database-url copied to clipboard

Support connection pooling kwargs

Open ddelange opened this issue 10 months ago • 5 comments

Hi 👋

We would like to migrate to dj-database-url (more precisely environs[django]), but we are currently leveraging the psycopg v3 database connection pool functionality (new in django v5.1).

Is there a generic mechanic in place to migrate the following code to the dj-database-url parse constructor?

"OPTIONS": {
    # TODO replace with ?sslmode=require in DATABASE_URL
    "sslmode": env("DJANGO_DB_SSLMODE", "require"),
    # https://www.psycopg.org/psycopg3/docs/api/pool.html#psycopg_pool.ConnectionPool    
    "pool": {
        "min_size": env.int("DJANGO_DB_POOL_MIN_SIZE", 4),
        "max_size": env.int("DJANGO_DB_POOL_MAX_SIZE", 32),
        "max_lifetime": env.int("CONN_MAX_AGE", 60 * 60),
        "max_idle": env.int("CONN_MAX_IDLE", 10 * 60),
    },
},

ddelange avatar Feb 18 '25 13:02 ddelange

Relatedly, somewhat, do you know if it's possible to include ["OPTIONS"]["pool"] in a DB URL as querystring param?

mecampbellsoup avatar May 03 '25 16:05 mecampbellsoup

@mattseymour would you accept a PR that follows double underscore syntax like ?pool__min_size=42&pool__max_idle=42?

if the conn_max_age kwarg is passed and not 0, django init will crash ref https://github.com/django/django/pull/17914#discussion_r1914650364. do you want to catch that beforehand or do you find django's error message verbose enough? I just opened a PR to clarify this behaviour in docs: https://github.com/django/django/pull/19442

ddelange avatar May 03 '25 18:05 ddelange

I have a different reason for the same request as the issue title. We use this package and deploy to Heroku that manages the DATABASE_URL for you, so I can't use the querystring param to add pool=True. I was only looking at using True/False for now for testing this new Django feature, so haven't thought as far as OP about the dictionary option.

jgillard avatar Jun 20 '25 10:06 jgillard

+1 to include a pool option.

radoshi avatar Jun 22 '25 05:06 radoshi

Okay let me look into this, but likewise happy to accept a PR if someone would like to pick it up. It shouldn't be too hard to implement.

mattseymour avatar Jun 23 '25 07:06 mattseymour