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): do not error on direct queries that return no result set (#65227)

George Munyorof820c35fd9c8
diff --git a/posthog/hogql/direct_sql/postgres_adapter.py b/posthog/hogql/direct_sql/postgres_adapter.py
index 5cd8f514371b..6bbee7590e33 100644
--- a/posthog/hogql/direct_sql/postgres_adapter.py
+++ b/posthog/hogql/direct_sql/postgres_adapter.py
@@ -285,8 +285,12 @@ class PostgresAdapter:
cursor.execute( # nosemgrep: python.django.security.injection.sql.sql-injection-using-db-cursor-execute.sql-injection-db-cursor-execute
request.sql, request.values or None
)
- results = cursor.fetchall()
+ # Statements that don't produce a result set (e.g. ATTACH, SET, other
+ # DDL/utility commands) leave cursor.description as None; calling
+ # fetchall() on them raises ProgrammingError. Treat them as a
+ # successful, empty result instead of surfacing a spurious error.
description = cursor.description or []
+ results = cursor.fetchall() if description else []
except (psycopg.Error, ExposedHogQLError) as error:
span.set_attribute("error_type", error.__class__.__name__)
if request.debug:
diff --git a/posthog/hogql/test/test_direct_postgres_query.py b/posthog/hogql/test/test_direct_postgres_query.py
index 5eebf1eb1dd0..96e9e8f99791 100644
--- a/posthog/hogql/test/test_direct_postgres_query.py
+++ b/posthog/hogql/test/test_direct_postgres_query.py
@@ -1100,6 +1100,51 @@ class TestDirectPostgresQuery(APIBaseTest):
mocked_connection.execute.assert_called_once_with("SELECT current_database(), version()")
mocked_cursor.execute.assert_called_once_with("SELECT 1 AS value", None)
+ @patch("posthog.hogql.direct_sql.postgres_adapter.psycopg.connect")
+ def test_send_raw_query_handles_statements_without_result_set(self, mock_connect):
+ source = ExternalDataSource.objects.create(
+ team=self.team,
+ source_id="source_id",
+ connection_id="connection_id",
+ status=ExternalDataSource.Status.COMPLETED,
+ source_type="Postgres",
+ access_method=ExternalDataSource.AccessMethod.DIRECT,
+ prefix="ph3",
+ job_inputs={
+ "host": "localhost",
+ "port": 5432,
+ "database": "postgres",
+ "user": "postgres",
+ "password": "postgres",
+ "schema": "ph3",
+ },
+ )
+
+ # ATTACH, SET and other utility/DDL statements return no result set: cursor.description
+ # is None and calling fetchall() on them raises ProgrammingError. They must be reported
+ # as a successful, empty result instead of a spurious error.
+ mocked_cursor = MagicMock()
+ mocked_cursor.description = None
+ mocked_cursor.fetchall.side_effect = psycopg.ProgrammingError("the last operation didn't produce a result")
+ mocked_connection = MagicMock()
+ mocked_connection.cursor.return_value.__enter__.return_value = mocked_cursor
+ mock_connect.return_value.__enter__.return_value = mocked_connection
+
+ executor = HogQLQueryExecutor(
+ query="SET TIME ZONE 'UTC'",
+ team=self.team,
+ connection_id=str(source.id),
+ send_raw_query=True,
+ )
+
+ response = executor.execute()
+
+ self.assertIsNone(response.error)
+ self.assertEqual(response.results, [])
+ self.assertEqual(response.columns, [])
+ mocked_cursor.fetchall.assert_not_called()
+ mocked_cursor.execute.assert_called_once_with("SET TIME ZONE 'UTC'", None)
+
@parameterized.expand(
[
("explicit_read_write", "COMMIT; BEGIN READ WRITE; UPDATE t SET a = 1; COMMIT;"),