Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(postgres): recover from "the connection is lost" drop mid-sync (#64257)

Tom Owers38ba7a176b69
diff --git a/posthog/temporal/data_imports/sources/postgres/postgres.py b/posthog/temporal/data_imports/sources/postgres/postgres.py
index f6e79e0bb9a3..74f2b421ecc2 100644
--- a/posthog/temporal/data_imports/sources/postgres/postgres.py
+++ b/posthog/temporal/data_imports/sources/postgres/postgres.py
@@ -137,6 +137,10 @@ _CONNECTION_DROPPED_ERROR_SUBSTRINGS = (
"consuming input failed",
"no connection to the server",
"terminating connection due to",
+ # psycopg's own message when libpq finds the socket already gone (PGconn.socket
+ # raises this from inside a wait, e.g. the commit at the end of get_connection).
+ # Same transient dead-socket class as the libpq drops above — recover by reconnecting.
+ "the connection is lost",
)
# Supavisor (Supabase's connection pooler) doesn't surface a dropped upstream connection with a
diff --git a/posthog/temporal/data_imports/sources/postgres/test_postgres.py b/posthog/temporal/data_imports/sources/postgres/test_postgres.py
index c47b487883cd..09826685e923 100644
--- a/posthog/temporal/data_imports/sources/postgres/test_postgres.py
+++ b/posthog/temporal/data_imports/sources/postgres/test_postgres.py
@@ -606,6 +606,11 @@ class TestIsConnectionDroppedError:
psycopg.OperationalError("connection to server was closed unexpectedly"),
psycopg.OperationalError("consuming input failed: EOF detected"),
psycopg.OperationalError("terminating connection due to administrator command"),
+ # psycopg's message when libpq finds the socket already gone (raised from
+ # PGconn.socket inside the commit at the end of get_connection). A transient
+ # dead-socket drop the in-process recovery must catch — without this the
+ # reconnect retry gives up and the offset-chunking fallback never triggers.
+ psycopg.OperationalError("the connection is lost"),
psycopg.errors.ProtocolViolation("SERVER CONN CRASHED?"),
# SQLSTATE 25P03: the source's idle_in_transaction_session_timeout culled our
# backend mid-stream. psycopg maps this to InternalError, not OperationalError,