Repository navigation
PostgreSQL reconnects after the server drops the connection - #1778
Open
dimitri-yatsenko wants to merge 1 commit into
Open
dimitri-yatsenko wants to merge 1 commit into
dimitri-yatsenko wants to merge 1 commit into
Conversation
psycopg2 reports a lost connection as OperationalError or InterfaceError without a SQLSTATE, which translate_error passed through, so query() never reconnected on PostgreSQL. Map them, and the SQLSTATEs with which the server ends a session (57P02, 57P05, 25P03), to LostConnectionError, and refuse a cursor on a closed connection with LostConnectionError.
dimitri-yatsenko
requested review from
MilagrosMarin,
lum-agilyti and
ttngu207
December 1, 2026 00:25
This branch has not been deployed
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.
Fixes #1776.
On PostgreSQL, a query after the server dropped the connection raised psycopg2's own error and never reconnected, although
database.reconnectisTrueby default. Inside a transaction, the caller got the driver's error instead ofLostConnectionError("Connection was lost during a transaction."). MySQL already reconnects.Changes
In
PostgreSQLAdapter:translate_errormapspsycopg2.OperationalErrorandpsycopg2.InterfaceErrorthat carry no SQLSTATE (pgcodeisNone) toLostConnectionError. psycopg2 reports a lost connection this way: "server closed the connection unexpectedly" on the first query after the drop, "connection already closed" after that. Errors the server sends carry a SQLSTATE and are mapped as before.translate_erroralso maps the SQLSTATEs with which the server ends a session: besides57P01(admin shutdown, already mapped),57P02(crash shutdown),57P05(idle_session_timeout) and25P03(idle_in_transaction_session_timeout). An idle-session timeout is a common cause of dropped connections on managed PostgreSQL.get_cursorraisesLostConnectionErroron a closed connection instead of psycopg2'sInterfaceError.Connection.query()then handles the error as it does on MySQL: it reconnects and re-runs the query, or, inside a transaction, cancels it and raisesLostConnectionError.Tests
tests/integration/test_reconnection_server_drop.py, on MySQL and PostgreSQL. Each test drops the session from a second connection (KILL/pg_terminate_backend) and checks that:reconnect=Truereconnects;reconnect=False, the query raisesLostConnectionError, and so does the next one.tests/unit/test_adapters.pycover the translation (including the session-ending SQLSTATEs) andget_cursor.All six PostgreSQL cases fail on
master; the MySQL cases pass on both.Notes
get_cursorinsidequery()'stry, so a query on a connection already known to be closed also reconnects. Without it, that query raisesLostConnectionErrorwithout reconnecting.ping()andis_connectedon a dropped connection).