原文
https://stackoverflow.com/questions/19963954/set-transaction-query-timeout-in-psycopg2
设置方式
1.代码中添加options
代码语言:javascript复制>>> 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
2.添加到系统环境变量中(PGOPTIONS)
代码语言:javascript复制>>> 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
相关文章参考
https://postgresqlco.nf/doc/en/param/statement_timeout/
https://blog.crunchydata.com/blog/control-runaway-postgres-queries-with-statement-timeout
https://iotespresso.com/how-to-configure-statement-timeout-in-psycopg2/