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-deletion): pre-warm hogvm import for dagster predicate compile (#64510)

Paweł Szczurb6bb30090759
diff --git a/posthog/dags/data_deletion_requests.py b/posthog/dags/data_deletion_requests.py
index af62b63ed445..576afa5d8d77 100644
--- a/posthog/dags/data_deletion_requests.py
+++ b/posthog/dags/data_deletion_requests.py
@@ -9,6 +9,18 @@ import dagster
import pydantic
from clickhouse_driver import Client
+# Pre-warm the HogQL → HogVM bytecode import chain at code-location load time. Compiling a
+# predicate (process_property_removal_per_shard → compile_hogql_predicate) builds the HogQL
+# database, whose virtual-field placeholder replacement lazily does
+# ``from common.hogvm.python.execute import …`` (posthog/hogql/placeholders.py). That import
+# first runs during op execution, when the Dagster run worker can no longer resolve the
+# top-level ``common`` namespace package off sys.path — yielding "No module named 'common'".
+# Importing it here, while the worker is still loading op modules with the repo root resolvable,
+# caches the chain in sys.modules so the op-time lazy import is a cache hit. Regular posthog.*
+# packages already get cached this way; common.hogvm is the only fresh import on the predicate
+# path, so it is the one that breaks without this.
+import posthog.hogql.compiler.bytecode # noqa: F401
+
from posthog.clickhouse.adhoc_events_deletion import ADHOC_EVENTS_DELETION_TABLE
from posthog.clickhouse.cluster import AlterTableMutationRunner, ClickhouseCluster, LightweightDeleteMutationRunner
from posthog.dags.common import JobOwners
diff --git a/posthog/models/data_deletion_request.py b/posthog/models/data_deletion_request.py
index 47321ff63829..553334a98458 100644
--- a/posthog/models/data_deletion_request.py
+++ b/posthog/models/data_deletion_request.py
@@ -94,6 +94,11 @@ def compile_hogql_predicate(obj) -> tuple[str, dict]:
)
try:
sql = translate_hogql(predicate, context, dialect="clickhouse")
+ except ImportError:
+ # A failed import means the runtime environment is broken (e.g. a Dagster worker that
+ # can't resolve ``common.hogvm`` during compilation), not that the predicate is invalid.
+ # Let it propagate as-is rather than masquerading as a predicate validation error.
+ raise
except Exception as exc:
raise ValidationError({"hogql_predicate": f"Could not compile HogQL: {exc}"}) from exc