question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Set statement_timeout per connection or per transaction

See original GitHub issue

Using 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:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
coleifercommented, Apr 13, 2019
1reaction
coleifercommented, Apr 13, 2019

This seems to work only when transaction is explicitly defined (e.g. with db.atomic()). It doesn’t trigger with autocommit=True.

Yes, that’s correct.

In the logs, I cannot see any BEGIN, COMMIT or SAVEPOINT messages so I’m not sure what db.atomic() actually triggers

The transactions are managed behind-the-scenes by psycopg2. You can see in the peewee code that it calls rollback() or commit() 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 an atomic() context manager. Otherwise each statement is effectively in its own transaction.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found