Skip to content

PostgreSQL reconnects after the server drops the connection - #1778

Open
dimitri-yatsenko wants to merge 1 commit into
masterfrom
fix/1776-postgres-reconnect
Open

dimitri-yatsenko wants to merge 1 commit into
masterfrom
fix/1776-postgres-reconnect

Conversation

@dimitri-yatsenko

Copy link
Copy Markdown
Member

Fixes #1776.

On PostgreSQL, a query after the server dropped the connection raised psycopg2's own error and never reconnected, although database.reconnect is True by default. Inside a transaction, the caller got the driver's error instead of LostConnectionError("Connection was lost during a transaction."). MySQL already reconnects.

Changes

In PostgreSQLAdapter:

  • translate_error maps psycopg2.OperationalError and psycopg2.InterfaceError that carry no SQLSTATE (pgcode is None) to LostConnectionError. 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_error also maps the SQLSTATEs with which the server ends a session: besides 57P01 (admin shutdown, already mapped), 57P02 (crash shutdown), 57P05 (idle_session_timeout) and 25P03 (idle_in_transaction_session_timeout). An idle-session timeout is a common cause of dropped connections on managed PostgreSQL.
  • get_cursor raises LostConnectionError on a closed connection instead of psycopg2's InterfaceError.

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 raises LostConnectionError.

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:
    • a query with reconnect=True reconnects;
    • inside a transaction, the query raises "Connection was lost during a transaction" and the transaction is not continued;
    • with reconnect=False, the query raises LostConnectionError, and so does the next one.
  • Unit tests in tests/unit/test_adapters.py cover the translation (including the session-ending SQLSTATEs) and get_cursor.

All six PostgreSQL cases fail on master; the MySQL cases pass on both.

Notes

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 dimitri-yatsenko added this to the v2.3.7 milestone Dec 1, 2026
@dimitri-yatsenko dimitri-yatsenko added the bug Indicates an unexpected problem or unintended behavior label Dec 1, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Indicates an unexpected problem or unintended behavior

1 participant