Created
May 2, 2018 20:08
-
-
Save afbio/577f58bb6dd429d5d6c3eb66365b359c to your computer and use it in GitHub Desktop.
Get and Set Statement Timeout to PostgreSQL using django
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StatementTimeout(object): | |
@classmethod | |
def _execute_query(cls, query, db_alias): | |
from django.db import connections | |
cur = connections[db_alias].cursor() | |
try: | |
cur.execute(query) | |
return cls._as_dict(cur) if cur.rowcount > 0 else True | |
finally: | |
cur.close() | |
@classmethod | |
def set_timeout(cls, timeout, db_alias): | |
""" | |
:param timeout: in miliseconds | |
""" | |
query = 'set statement_timeout to {}'.format(timeout) | |
return cls._execute_query(query, db_alias) | |
@classmethod | |
def get_timeout(cls, db_alias): | |
query = 'show statement_timeout' | |
return cls._execute_query(query, db_alias) | |
@staticmethod | |
def _as_dict(cursor): | |
desc = cursor.description | |
return [ | |
dict(zip([col[0] for col in desc], row)) | |
for row in cursor.fetchall() | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment