set transaction\query timeout in psycopg2?

后端 未结 3 1868
甜味超标
甜味超标 2020-12-15 05:32

Is there a way to set a timeout in psycopg2 for db transactions or for db queries?

A sample use-case:
Heroku limits django web requests to 30sec

相关标签:
3条回答
  • 2020-12-15 05:34

    You can set the timeout at connection time using the options parameter. The syntax is a bit weird:

    >>> import psycopg2
    >>> cnn = psycopg2.connect("dbname=test options='-c statement_timeout=1000'")
    >>> cur = cnn.cursor()
    >>> cur.execute("select pg_sleep(2000)")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    psycopg2.extensions.QueryCanceledError: canceling statement due to statement timeout
    

    it can also be set using an env variable:

    >>> import os
    >>> os.environ['PGOPTIONS'] = '-c statement_timeout=1000'
    >>> import psycopg2
    >>> cnn = psycopg2.connect("dbname=test")
    >>> cur = cnn.cursor()
    >>> cur.execute("select pg_sleep(2000)")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    psycopg2.extensions.QueryCanceledError: canceling statement due to statement timeout
    
    0 讨论(0)
  • 2020-12-15 05:36

    You can set a per-statement timeout at any time using SQL. For example:

    SET statement_timeout = '2s'
    

    will abort any statement (following it) that takes more than 2 seconds (you can use any valid unit as 's' or 'ms'). Note that when a statement timeouts, psycopg raises an exception and it is your care to catch it and act appropriately.

    0 讨论(0)
  • 2020-12-15 05:46

    Looks like PostgreSQL 9.6 added idle transaction timeouts. See:

    • https://www.postgresql.org/docs/9.6/static/runtime-config-client.html#GUC-IDLE-IN-TRANSACTION-SESSION-TIMEOUT for reference.
    • http://blog.dbi-services.com/a-look-at-postgresql-9-6-killing-idle-transactions-automatically/ as an example.

    PostgreSQL 9.6 is also supported in Heroku so you should be able to use this.

    0 讨论(0)
提交回复
热议问题