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, after which Heroku terminates the request without allowing django to gracefully roll-back any transactions which have not yet returned. This can leave outstanding transactions open on postgres. You could configure a timeout in the database, but that would also limit non-web-related queries such as maintenance scripts analytics etc. In this case setting a timeout via the middleware (or via django) would be preferable.
Looks like PostgreSQL 9.6 added idle transaction timeouts. See:
PostgreSQL 9.6 is also supported in Heroku so you should be able to use this.
You can set a per-statement timeout at any time using SQL. For example:
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.
You can set the timeout at connection time using the options parameter. The syntax is a bit weird:
it can also be set using an env variable: