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(data-warehouse): disable psycopg prepared statements in queue db (#115095)

Tom Owers1f1b8c34a091
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/postgres_queue/test_consumer.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/postgres_queue/test_consumer.py
index 09ddc558aa01..1b8297f52ec7 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/postgres_queue/test_consumer.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/postgres_queue/test_consumer.py
@@ -1714,6 +1714,10 @@ class TestStatementTimeoutBackstop:
"SET statement_timeout = 210000",
"SET jit = off",
]
+ # The queue DB sits behind a transaction pooler: psycopg's default prepare_threshold
+ # would server-prepare a statement after 5 identical executions, and a pooler can bind
+ # it on a backend that never parsed it ("unable to bind ... cannot get parse message").
+ assert fresh.prepare_threshold is None
class TestPollBackoff:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/postgres_queue/test_jobs_db.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/postgres_queue/test_jobs_db.py
index e29c9bea4c7a..889991e96ab9 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/postgres_queue/test_jobs_db.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/postgres_queue/test_jobs_db.py
@@ -695,7 +695,13 @@ class TestVerifyGroupLeaseSync:
)
connect.assert_called_once()
- assert _sync_connection_pool(_db_url, 5).idle_count == 1
+ pool = _sync_connection_pool(_db_url, 5)
+ assert pool.idle_count == 1
+ # The queue DB sits behind a transaction pooler: psycopg's default prepare_threshold
+ # would server-prepare a statement after 5 identical executions on this long-lived
+ # pooled connection, and a later Bind can land on a backend that never parsed it
+ # ("unable to bind ... cannot get parse message").
+ assert pool._idle[0].prepare_threshold is None
@pytest.mark.asyncio
async def test_a_dropped_pooled_connection_is_replaced_not_reported_as_a_lost_lease(self, conn, _db_url):
diff --git a/products/warehouse_sources_queue/backend/core/batch_consumer.py b/products/warehouse_sources_queue/backend/core/batch_consumer.py
index 48cdccb83dd4..374f2efb5ef5 100644
--- a/products/warehouse_sources_queue/backend/core/batch_consumer.py
+++ b/products/warehouse_sources_queue/backend/core/batch_consumer.py
@@ -544,6 +544,14 @@ class BatchConsumer:
autocommit=True,
connect_timeout=self._config.connect_timeout_seconds,
)
+ # The queue DB sits behind a transaction pooler, which can hand consecutive
+ # statements on the same client connection to different backends. psycopg's default
+ # prepare_threshold server-prepares a statement after 5 identical executions, and a
+ # later Bind against that name can land on a backend that never saw the Parse,
+ # raising "unable to bind ... cannot get parse message". Disabling it keeps every
+ # statement unnamed, which the pooler always replays correctly. Same setting the
+ # Redshift batch export uses for its own pooler/compatibility reasons.
+ conn.prepare_threshold = None
# Session-scoped SET, not a libpq startup option: PgBouncer rejects
# statement_timeout inside the `options` startup parameter.
timeout_ms = self._statement_timeout_ms(statement_timeout_seconds)
diff --git a/products/warehouse_sources_queue/backend/core/jobs_db.py b/products/warehouse_sources_queue/backend/core/jobs_db.py
index 17931e645b21..b2ebae424685 100644
--- a/products/warehouse_sources_queue/backend/core/jobs_db.py
+++ b/products/warehouse_sources_queue/backend/core/jobs_db.py
@@ -1203,9 +1203,16 @@ class _SyncConnectionPool:
with self._lock:
conn = self._idle.pop() if self._idle else None
if conn is None:
- return psycopg.connect(
+ conn = psycopg.connect(
self._database_url, autocommit=True, connect_timeout=self._connect_timeout_seconds
)
+ # Pooled connections run the same query text many times, and the queue DB
+ # sits behind a transaction pooler that can hand consecutive statements to
+ # different backends. psycopg's default prepare_threshold would server-prepare
+ # after 5 identical executions, and a later Bind can land on a backend that
+ # never saw the Parse ("unable to bind ... cannot get parse message").
+ conn.prepare_threshold = None
+ return conn
if not conn.closed and not conn.broken:
return conn
self._close_quietly(conn)