: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(hogql): add direct Postgres phase timings (#82137)
Bill Guowei Yange04a2e5bd065
diff --git a/posthog/hogql/direct_sql/postgres_adapter.py b/posthog/hogql/direct_sql/postgres_adapter.pyindex 10e593b60991..6b5516dbce9f 100644--- a/posthog/hogql/direct_sql/postgres_adapter.py+++ b/posthog/hogql/direct_sql/postgres_adapter.py@@ -1,3 +1,4 @@+from contextlib import ExitStackfrom datetime import date, datetimefrom typing import TYPE_CHECKING, TypedDict, cast@@ -12,6 +13,7 @@ from posthog.hogql.direct_sql.capability import is_direct_capablefrom posthog.hogql.direct_sql.raw_sql import ensure_single_direct_statementfrom posthog.hogql.errors import ExposedHogQLErrorfrom posthog.hogql.escape_sql import escape_postgres_identifier+from posthog.hogql.timings import HogQLTimingsif TYPE_CHECKING:from posthog.models.team import Team@@ -207,27 +209,34 @@ class PostgresAdapter:dialect: HogQLDialect | None = "postgres"def validate_source_config(- self, source: "ExternalDataSource", team: "Team"+ self, source: "ExternalDataSource", team: "Team", timings: HogQLTimings | None = None) -> tuple["PostgresSource", "PostgresSourceConfig"]:- from products.warehouse_sources.backend.facade.source_management import PostgresSource, SourceRegistry- from products.warehouse_sources.backend.facade.types import ExternalDataSourceType-- # Capability, not access_method: a synced source with the direct-query toggle on is valid too.- if not (is_direct_capable(source) and source.direct_engine == self.engine):- raise ExposedHogQLError("Invalid direct Postgres connection.")-- postgres_source = cast(PostgresSource, SourceRegistry.get_source(ExternalDataSourceType.POSTGRES))- config = postgres_source.parse_config(source.job_inputs or {})-- is_ssh_valid, ssh_valid_errors = postgres_source.ssh_tunnel_is_valid(config, team.pk)- if not is_ssh_valid:- raise ExposedHogQLError(ssh_valid_errors or "Invalid SSH tunnel configuration.")-- valid_host, host_errors = postgres_source.is_database_host_valid(- config.host, team.pk, using_ssh_tunnel=config.ssh_tunnel.enabled if config.ssh_tunnel else False- )- if not valid_host:- raise ExposedHogQLError(host_errors or "Invalid Postgres host.")+ timings = timings or HogQLTimings()+ with timings.measure("postgres_source_config"):+ with timings.measure("postgres_source_capability"):+ # Capability, not access_method: a synced source with the direct-query toggle on is valid too.+ if not (is_direct_capable(source) and source.direct_engine == self.engine):+ raise ExposedHogQLError("Invalid direct Postgres connection.")++ with timings.measure("postgres_source_registry", emit_span=True):+ from products.warehouse_sources.backend.facade.source_management import PostgresSource, SourceRegistry+ from products.warehouse_sources.backend.facade.types import ExternalDataSourceType++ postgres_source = cast(PostgresSource, SourceRegistry.get_source(ExternalDataSourceType.POSTGRES))+ with timings.measure("postgres_source_parse_config"):+ config = postgres_source.parse_config(source.job_inputs or {})++ with timings.measure("postgres_ssh_validation"):+ is_ssh_valid, ssh_valid_errors = postgres_source.ssh_tunnel_is_valid(config, team.pk)+ if not is_ssh_valid:+ raise ExposedHogQLError(ssh_valid_errors or "Invalid SSH tunnel configuration.")++ with timings.measure("postgres_host_validation"):+ valid_host, host_errors = postgres_source.is_database_host_valid(+ config.host, team.pk, using_ssh_tunnel=config.ssh_tunnel.enabled if config.ssh_tunnel else False+ )+ if not valid_host:+ raise ExposedHogQLError(host_errors or "Invalid Postgres host.")return postgres_source, config@@ -235,12 +244,17 @@ class PostgresAdapter:return ensure_single_direct_statement(sql)def execute(self, request: DirectQueryRequest) -> DirectQueryResult:- from products.warehouse_sources.backend.facade.source_management import _get_sslmode, source_requires_ssl-source = request.source- postgres_source, source_config = self.validate_source_config(source, request.team)- source_schema = source_config.schema- require_ssl = source_requires_ssl(source, source_config)+ with request.timings.measure("postgres_source_validation"):+ with request.timings.measure("postgres_source_helpers_import"):+ from products.warehouse_sources.backend.facade.source_management import (+ _get_sslmode,+ source_requires_ssl,+ )++ postgres_source, source_config = self.validate_source_config(source, request.team, request.timings)+ source_schema = source_config.schema+ require_ssl = source_requires_ssl(source, source_config)settings = request.settingsstatement_timeout_ms = (max(settings.max_execution_time or DIRECT_POSTGRES_DEFAULT_STATEMENT_TIMEOUT_SECONDS, 1) * 1000@@ -253,7 +267,9 @@ class PostgresAdapter:try:with request.timings.measure("postgres_execute"):- with postgres_source.with_ssh_tunnel(source_config) as (host, port):+ with ExitStack() as tunnel_stack:+ with request.timings.measure("postgres_tunnel_open", emit_span=True):+ host, port = tunnel_stack.enter_context(postgres_source.with_ssh_tunnel(source_config))connection_kwargs: PostgresConnectionKwargs = {"host": host,"port": port,@@ -275,34 +291,40 @@ class PostgresAdapter:# but do not use certificate-based auth.connection_kwargs["sslmode"] = "require"- with psycopg.connect(**connection_kwargs) as connection:+ with request.timings.measure("postgres_connect", emit_span=True):+ connection_context = psycopg.connect(**connection_kwargs)+ with connection_context as connection:runtime_connection_metadata = source.connection_metadataif should_hydrate_runtime_direct_postgres_connection_metadata(source_schema,runtime_connection_metadata,):- runtime_connection_metadata = get_runtime_direct_postgres_connection_metadata(- connection,- runtime_connection_metadata,- )+ with request.timings.measure("postgres_connection_metadata"):+ runtime_connection_metadata = get_runtime_direct_postgres_connection_metadata(+ connection,+ runtime_connection_metadata,+ )session_setup_sql = direct_postgres_session_setup_sql(source_schema,runtime_connection_metadata,host,)if session_setup_sql:- connection.execute(session_setup_sql)+ with request.timings.measure("postgres_session_setup"):+ connection.execute(session_setup_sql)connection.adapters.register_loader("date", LenientDirectPostgresDateLoader)with connection.cursor() as cursor:- 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- )+ with request.timings.measure("postgres_query_execute"):+ 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+ )# 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 []+ with request.timings.measure("postgres_query_fetch"):+ results = cursor.fetchall() if description else []except (psycopg.Error, BaseSSHTunnelForwarderError, 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.pyindex 876f4cb216f2..e8c27a97d659 100644--- a/posthog/hogql/test/test_direct_postgres_query.py+++ b/posthog/hogql/test/test_direct_postgres_query.py@@ -1236,6 +1236,9 @@ class TestDirectPostgresQuery(APIBaseTest):self.assertEqual(response.results, [(1,)])mocked_connection.execute.assert_called_once_with("SELECT current_database(), version()")mocked_cursor.execute.assert_called_once_with("SELECT 1 AS value", None)+ timing_keys = {timing.k for timing in response.timings or []}+ self.assertTrue(any(key.endswith("/postgres_connection_metadata") for key in timing_keys))+ self.assertFalse(any(key.endswith("/postgres_session_setup") for key in timing_keys))@patch("posthog.hogql.direct_sql.postgres_adapter.psycopg.connect")def test_send_raw_query_handles_statements_without_result_set(self, mock_connect):@@ -1617,7 +1620,24 @@ class TestDirectPostgresQuery(APIBaseTest):response = executor.execute()- self.assertTrue(any(timing.k.endswith("/postgres_execute") for timing in response.timings or []))+ timing_keys = {timing.k for timing in response.timings or []}+ for timing_suffix in (+ "/postgres_source_validation",+ "/postgres_source_helpers_import",+ "/postgres_source_config",+ "/postgres_source_capability",+ "/postgres_source_registry",+ "/postgres_source_parse_config",+ "/postgres_ssh_validation",+ "/postgres_host_validation",+ "/postgres_execute",+ "/postgres_tunnel_open",+ "/postgres_connect",+ "/postgres_session_setup",+ "/postgres_query_execute",+ "/postgres_query_fetch",+ ):+ self.assertTrue(any(key.endswith(timing_suffix) for key in timing_keys))@patch("posthog.hogql.direct_sql.postgres_adapter.psycopg.connect")def test_execute_direct_postgres_query_reraises_unexpected_errors(self, mock_connect):