Repository navigation
Reconnect PostgreSQL after the server drops the connection - #236
Merged
Merged
Conversation
A failover, an idle-session reap or pg_terminate_backend closes the connection under a running app. The adapter kept the dead psycopg2 connection, so every later query raised "connection already closed" until the process was restarted. The adapter now keeps its connect arguments and, when psycopg2 has marked the connection closed, opens a new one. The statement that hit the dropped link is retried once, but only when no explicit transaction was open and no uncommitted work was pending. Otherwise the caller sees the error and the next statement runs on the new connection. This mirrors the Firebird adapter's dead-connection handling. Session state set with SET after connecting does not carry over to the new connection; connect options do. Signed-off-by: Michael <michael@codeinfinity.co.za>
Collaborator
Author
|
I have read the Tina4 Contributor Licence Agreement and I agree to it. |
This was referenced Oct 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
After the PostgreSQL server drops a connection (failover, idle-session reap,
pg_terminate_backend), the adapter keeps the dead psycopg2 connection and every later query fails withconnection already closeduntil the process is restarted. This was seen in production on 3.13.52 after a database failover.Change (
tina4_python/database/postgres.py)connect()stores its arguments.Not carried over: session state set with
SETafter connecting (connect options are).Tests:
tests/test_postgres_reconnect_after_drop.pyruns against a live PostgreSQL, with the drop done by a realpg_terminate_backend. 5 of its 6 tests fail onv3and all pass with this change. The existing PostgreSQL tests give the same results before and after.