Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - PostgreSQL set statement_timeout

I'm using Django 1.10 and PostgreSQL DB. I'm trying to figure out whether I can set statement_timeout from Django. Seems like I can't do it the same way as for connect_timeout (in settings.py):

DATABASES[DEFAULT]['OPTIONS'] = {
    'connect_timeout': float(os.getenv('DEFAULT_DB_OPTIONS_TIMEOUT', 5))
}

I saw something like this, but I can't find a way to verify it actually works:

DATABASES[DEFAULT]['OPTIONS'] = {
    'options': '-d statement_timeout=700'
}

I know I can set it directly from the DB like:

set statement_timeout=5000

but I'm looking for a way to do it from Django.

like image 597
user2880391 Avatar asked Jul 16 '18 11:07

user2880391


1 Answers

There is no other "Django" way of doing this. The Django way is to use your settings.py like you indicate, except your example is not quite right. It should be:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        ...
        'OPTIONS': {
            'options': '-c statement_timeout=5000',
            ...
        }
    }
}

The 'OPTIONS' dictionary is for arguments to the database connection string. The list of Postgres arguments can be found here. The options connection string argument allows you to specify command-line arguments for postgres. The -c option is explained here.

Note that the timeout is measured in milliseconds, so the 5000 above means 5 seconds.

like image 180
expz Avatar answered Oct 22 '22 04:10

expz