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): retry transient Supavisor pool-checkout failures in-process (#66510)

Tom Owersbd7bc180d81e
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/postgres.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/postgres.py
index 9d3de1de9a00..25a2cbeb733c 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/postgres.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/postgres.py
@@ -194,15 +194,19 @@ _CONNECTION_DROPPED_ERROR_SUBSTRINGS = (
# Supavisor (Supabase's connection pooler) doesn't surface a dropped upstream connection with a
# libpq/PgBouncer signature — it raises its own pooler-internal error as a generic psycopg
-# InternalError_ (SQLSTATE XX000) carrying the "(EDBHANDLEREXITED)" code. The trailing message
-# varies — both "(EDBHANDLEREXITED) DbHandler exited. Check logs for more information" and
+# InternalError_ (SQLSTATE XX000) carrying a Supavisor error code. The trailing message varies —
+# both "(EDBHANDLEREXITED) DbHandler exited. Check logs for more information" and
# "(EDBHANDLEREXITED) connection to database closed. Check logs for more information" have been
-# observed for the same condition — but the EDBHANDLEREXITED code is the stable signal: the
-# pooler's per-session DbHandler process exited because its backend connection died (idle cull,
-# backend restart, failover). That's the same transient class as the libpq drops above and
-# recovers on reconnect, so match the code itself rather than any one message wording. Genuine
+# observed for the same condition — but the code is the stable signal, so match the code itself
+# rather than any one message wording.
+# - EDBHANDLEREXITED: the pooler's per-session DbHandler process exited because its backend
+# connection died (idle cull, backend restart, failover).
+# - ECHECKOUTRETRIES ("failed to check out a connection after multiple retries"): the pooler
+# couldn't hand us a backend connection after retrying internally — its pool was momentarily
+# exhausted or every backend was busy. A slot frees the moment another session returns one.
+# Both are the same transient class as the libpq drops above and recover on reconnect. Genuine
# XX000 internal errors (data corruption, etc.) carry a different code and stay non-recoverable.
-_POOLER_CONNECTION_DROPPED_ERROR_SUBSTRINGS = ("edbhandlerexited",)
+_POOLER_CONNECTION_DROPPED_ERROR_SUBSTRINGS = ("edbhandlerexited", "echeckoutretries")
# Connect-time capacity errors: the source refuses a *new* connection because it has hit a
# connection limit, not because anything is misconfigured. PostgreSQL raises "sorry, too many
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py
index f4c29313c329..4482d22e61a2 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py
@@ -1031,6 +1031,11 @@ class TestIsConnectionDroppedError:
psycopg.errors.InternalError_(
"(EDBHANDLEREXITED) connection to database closed. Check logs for more information"
),
+ # Supavisor also surfaces a transient pool-checkout failure as an XX000 InternalError_
+ # carrying the "(ECHECKOUTRETRIES)" code — it couldn't hand us a backend connection after
+ # retrying internally. Same transient pooler class as EDBHANDLEREXITED; recovers on
+ # reconnect once a session returns a connection to the pool.
+ psycopg.errors.InternalError_("(ECHECKOUTRETRIES) failed to check out a connection after multiple retries"),
# Supavisor reports a transient timeout reaching the upstream backend as a
# ConnectionFailure (08006, an OperationalError) carrying the Erlang-tuple reason
# "{:error, :etimedout}" — a transient drop the in-process recovery must catch.
@@ -1055,7 +1060,8 @@ class TestIsConnectionDroppedError:
ValueError("server conn crashed?"),
Exception("server conn crashed?"),
# A genuine XX000 internal error that isn't the Supavisor pooler drop must stay
- # non-recoverable — the InternalError_ match is scoped to the "(EDBHANDLEREXITED)" code.
+ # non-recoverable — the InternalError_ match is scoped to the known pooler codes
+ # ("(EDBHANDLEREXITED)" / "(ECHECKOUTRETRIES)"), not every XX000.
psycopg.errors.InternalError_("XX000: internal error: something went wrong"),
],
)