[Answered ]-API design: can I stop users from making queries that are extremely slow?

2πŸ‘

βœ…

I don’t think Django or geodjango offer any support for killing queries that take too long.

A quick search on the postgresql docs gave me the following:
http://www.postgresql.org/docs/9.3/static/runtime-config-client.html#GUC-STATEMENT-TIMEOUT

statement_timeout (integer)

Abort any statement that takes more than the specified number of milliseconds, starting from the time the command arrives at the server
from the client. If log_min_error_statement is set to ERROR or lower,
the statement that timed out will also be logged. A value of zero (the
default) turns this off.

Setting statement_timeout in postgresql.conf is not recommended because it would affect all sessions.

But this would affect all queries so you could break other functionality which takes longer for some reason.

The suggested way is to create a special user, e.g. web_user, on the database which runs these queries and limit the time for that user:

ALTER ROLE web_user SET statement_timeout = 10000;
πŸ‘€EWit

0πŸ‘

In django, you can implement the statement_timeout (milliseconds) for a postgresql db in settings.py like this:

'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'NAME': 'dbname',
    'USER': 'username',
    'PASSWORD': '',
    'HOST': '',
    'PORT': '',
    'OPTIONS': {
        'options': '-c statement_timeout=1000'
    }
},
πŸ‘€Andrew Bird

Leave a comment