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(dags): retry the duckgres idle-session reap in the events backfill (#95304)

James Greenhillba0bdec266e5
diff --git a/posthog/dags/events_backfill_to_duckling.py b/posthog/dags/events_backfill_to_duckling.py
index 70a926f51f48..308bf98e985b 100644
--- a/posthog/dags/events_backfill_to_duckling.py
+++ b/posthog/dags/events_backfill_to_duckling.py
@@ -423,6 +423,14 @@ _CONNECTION_DROPPED_SQLSTATES = {
"57P01", # admin_shutdown
"57P02", # crash_shutdown
"57P03", # cannot_connect_now
+ # idle_session_timeout: duckgres reaped an idle client session (the backfill
+ # holds its connection open across the ClickHouse->S3 export, which can outrun
+ # the server's duckgres.idle_timeout). Older duckgres just closed the socket,
+ # so this arrived as a transport error matching _CONNECTION_DROPPED_MARKERS;
+ # it now sends a proper FATAL first, which is a cleaner signal but a different
+ # shape. Safest possible replay: the session was idle, so no statement was
+ # in flight and a retry cannot double-apply.
+ "57P05",
}
# Transport/connection-loss phrases ONLY. Deliberately NOT "flight execute":
@@ -448,6 +456,7 @@ _CONNECTION_DROPPED_MARKERS = (
"error reading from server",
"server closed the connection",
"terminating connection due to administrator command",
+ "terminating connection due to idle timeout",
"transport:",
)
diff --git a/posthog/dags/test_events_backfill_to_duckling.py b/posthog/dags/test_events_backfill_to_duckling.py
index e179373ab08b..13ac9192c2e0 100644
--- a/posthog/dags/test_events_backfill_to_duckling.py
+++ b/posthog/dags/test_events_backfill_to_duckling.py
@@ -1362,6 +1362,43 @@ class TestConnectionDropped:
msg = "flight execute: rpc error: code = Internal desc = transport: error while reading: EOF"
assert _connection_dropped(psycopg.InternalError(msg)) is True
+ @parameterized.expand(
+ [
+ # duckgres reaps a client session that idled past duckgres.idle_timeout
+ # (the backfill holds its connection across the ClickHouse->S3 export).
+ # It sends a FATAL 57P05 before closing; older builds just dropped the
+ # socket, which landed in the transport-marker branch instead. The two
+ # halves of that classification are guarded independently below, so
+ # neither can regress while the other keeps these cases green.
+ #
+ # The real observed shape: sqlstate AND message both present.
+ (
+ "idle_session_timeout",
+ psycopg.errors.IdleSessionTimeout(
+ "terminating connection due to idle timeout (duckgres.idle_timeout is 5m0s)"
+ ),
+ ),
+ # Message alone, no sqlstate — psycopg surfacing the text without a
+ # parsed ErrorResponse. Guards the _CONNECTION_DROPPED_MARKERS entry.
+ (
+ "idle_timeout_message_without_sqlstate",
+ psycopg.OperationalError("terminating connection due to idle timeout (duckgres.idle_timeout is 5m0s)"),
+ ),
+ # Sqlstate alone. PostgreSQL's own idle_session_timeout wording is
+ # hyphenated ("idle-session timeout") and so does NOT match the
+ # duckgres-shaped marker — which is exactly why this guards the
+ # _CONNECTION_DROPPED_SQLSTATES entry on its own.
+ (
+ "idle_session_timeout_sqlstate_without_marker",
+ psycopg.errors.IdleSessionTimeout("terminating connection due to idle-session timeout"),
+ ),
+ ]
+ )
+ def test_idle_session_reap_is_dropped(self, _label, exc):
+ # Safest possible replay: the session was idle, so no statement was in
+ # flight and a retry cannot double-apply.
+ assert _connection_dropped(exc) is True
+
@parameterized.expand(
[
("disk_full", psycopg.errors.DiskFull()),