-
Notifications
You must be signed in to change notification settings - Fork 418
[Feature Request] Allow to manage reset query for connection #780
New issue
Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “No Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? No Sign in to your account
Comments
There's already a way to override connection behavior: make a subclass and pass it in as |
Thanks for reply! But is there something, that stopping implementation of this feature in default one? As I see, almost in every project you would need to implement custom, almost one line class, if you want to optimize performance. In case of many short queries asyncpg will almost double qps to postgresql. I think many connection poolers has interface for tuning connection reset algorithm, in example pg_bouncer server_reset_query param. I suppose, that most projects do not use all the caps. We can keep default behavior, but add caps arg to init, that will override autodetection, or allow directly set reset query. |
Server capabilities aren't meant to be an API and overriding the detection is a bad idea, because new releases might add more caps for newer Postgres versions or alternative implementations and you overload will become outdated, which might lead to unexpected behavior. Basically, this is a dangerous way to achieve better performance.
Setting the reset query directly is a more reasonable approach. Feel free to send a PR. |
I stumbled upon this thread, because we noticed a lot of Is there any doc for these server capabilities ? And why there are these unused ones ? |
Just wondering if it's possible to add this reset query to the last executed query before connection is released instead of making a separate request that ends up in extra round trip to DB? |
@LMalikov i think - it can not be done in that way. At least when asyncpg executing query it can't understand that it is the last one query in a connection context. |
@mdespriee i can't see any documentation about this. But you can understand how that works from here: https://github.com/MagicStack/asyncpg/blob/v0.27.0/asyncpg/connection.py#L93 - how caps determined. https://github.com/MagicStack/asyncpg/blob/v0.27.0/asyncpg/connection.py#L1525 - how thay used to generate reset query that executed before connection release. |
The pgbouncer admin console complains with asyncpg clients:
is related, as it seems? |
@sandr8 |
That's part of the notice message from pgbouncer of course... which is quoting the command sent by pgbouncer. What I would like to understand, when I find the time, is what the deal is with the «capabilities» cited above and if, as it sounds, the server is supposed somehow to let the client know what the client can do, and the client should act accordingly. |
I took a look at the code. I think the current approach is destined to become more complicated. For example, CockroachDB is soon going to support PL/pgSQL. At that point the code will have to branch on the CockroachDB version. Why not just dynamically |
The default connection reset behaviour might be an overkill for some cases e.g. when respective server capabilities are not used or resources are cleaned up explicitly before returning the connection to the pool. This provides an opt-in way to override this default behaviour. Also see MagicStack#780
A coroutine can be passed to the new `reset` argument of `create_pool` to control what happens to the connection when it is returned back to the pool by `release()`. By default `Connection.reset()` is called. Additionally, `Connection.get_reset_query` is renamed from `Connection._get_reset_query` to enable an alternative way of customizing the reset process via subclassing. Closes: #780 Closes: #1146
A coroutine can be passed to the new `reset` argument of `create_pool` to control what happens to the connection when it is returned back to the pool by `release()`. By default `Connection.reset()` is called. Additionally, `Connection.get_reset_query` is renamed from `Connection._get_reset_query` to enable an alternative way of customizing the reset process via subclassing. Closes: #780 Closes: #1146
Every connection on release calls reset, which results in evaluation of this statements (for postgres caps):
So if you use postgres your caps predefined. But not every project uses this capabilities, so this calls looks like overhead.
Maybe add interface to specify server caps explicitly, this will reduce reset statements to only useful ones or even noop at all.
Something like pass server caps in init of connection or/and connection pool.
The text was updated successfully, but these errors were encountered: