Reconnection Strategies for Psycopg in High-Usage Scenarios After Connection Failures #742
-
Hello everyone, Our team is currently utilizing psycopg2, and we're considering a transition to psycopg3. We've encountered issues with our database during periods of high API usage, particularly with an increased number of read operations. The error message we receive is as follows: "Could not establish connection from the local to the remote side of the tunnel: open new channel ssh error: SSH session not active connection to server at 'localhost', port failed: server closed the connection unexpectedly. This probably means the server terminated abnormally before or while processing the request." Despite employing SimplePoolConnection, we've found that all our connections become stale following this error. I'm reaching out to inquire about potential reconnection strategies that could help us maintain a fresh connection in the event that the current one goes stale. Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
Check the pool documentation for this topic. You can use |
Beta Was this translation helpful? Give feedback.
-
Also would it be possible to show me how you implement the check parameter
It's written in the documentation page I linked, at <
https://www.psycopg.org/psycopg3/docs/advanced/pool.html#connection-quality>.
An implementation is already available:
A simple implementation is available as the static method
ConnectionPool.check_connection
<https://www.psycopg.org/psycopg3/docs/api/pool.html#psycopg_pool.ConnectionPool.check_connection>,
which can be used as:
with ConnectionPool(
..., check=ConnectionPool.check_connection, ...
) as pool:
...
… |
Beta Was this translation helpful? Give feedback.
Check the pool documentation for this topic. You can use
check()
to check all the connections of the database and/or thecheck
parameter in the pool constructor if you are ok to pay the price of an extra query per connection.