Set statement_timeout per connection or per transaction
See original GitHub issueUsing peewee with postgres, you can normally set statement_timeout
like this:
conn = PostgresqlExtDatabase(
...
options='-c statement_timeout=1ms'
)
However, pgbouncer
doesn’t allow options
(you can ignore it, but it just doesn’t have effect then). So I’m planning to do something like:
database.connect()
database.execute_sql("SET statement_timeout = '1ms'")
This seems to work, but I wonder if it’s the correct approach(?)
Also, when you run pgbouncer
in transaction
mode, each transaction may get served a different connection. In this case, I think I should rather:
BEGIN
SET LOCAL statement_timeout = '1ms'
...
Is it possible to achieve this with peewee so that I wouldn’t need to manually do something like this:
with db.atomic():
db.execute_sql('SET ...')
(transaction
pooling mode is better than session
mode because connections don’t lie idle when connection is open but no DB activity is happening.)
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
How to set query_timeout in relation to statement_timeout?
Show activity on this post. We can set 2 timeouts for the Client: statement_timeout : number of milliseconds before a statement in query...
Read more >15: 20.11. Client Connection Defaults - PostgreSQL
The timeout is measured from the time a command arrives at the server until it is completed by the server. If multiple SQL...
Read more >Control Runaway Postgres Queries With Statement Timeout
A statement timeout for your own protection. Postgres allows you to set a database timeout. · Per session changes. Now most of your...
Read more >statement_timeout parameter - PostgreSQL Documentation
A value of zero (the default) disables the timeout. The timeout is measured from the time a command arrives at the server until...
Read more >How to set statement timeout per user? - DBA Stack Exchange
I would like to set up different statement timeouts for different users. Eg: Guest 5 minutes and Admin 10 minutes. Is it possible...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This might also help: http://initd.org/psycopg/docs/usage.html#transactions-control
Yes, that’s correct.
The transactions are managed behind-the-scenes by
psycopg2
. You can see in the peewee code that it callsrollback()
orcommit()
on the underlying psycopg2 connection object. The managing transactions document is worth reading if you’re unclear on Peewee’s APIs or how to use them.Peewee always runs in autocommit mode by default. That is: Peewee will issue commit unless there is an active manual_commit or atomic block wrapping the code. If you want to explicitly manage transactions/commit yourself, you wrap the corresponding code in the
manual_commit()
context manager. If you want to wrap multiple operations in a transaction or savepoint, you wrap the corresponding code in anatomic()
context manager. Otherwise each statement is effectively in its own transaction.