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-modeling): stop running nodes whose query was deleted (#80610)

Jovan Sakovic40d6b14f992a
diff --git a/posthog/temporal/data_modeling/activities/get_dag_structure.py b/posthog/temporal/data_modeling/activities/get_dag_structure.py
index 69e0ca7e5738..ccf7902c37b0 100644
--- a/posthog/temporal/data_modeling/activities/get_dag_structure.py
+++ b/posthog/temporal/data_modeling/activities/get_dag_structure.py
@@ -64,7 +64,11 @@ class DAG:
def _get_dag_structure_async(team_id: int, dag_id: str) -> DAG:
"""Retrieve all nodes and edges for a DAG from the database."""
nodes = Node.objects.filter(team_id=team_id, dag_id=dag_id)
- executable_nodes = nodes.filter(type__in=[NodeType.VIEW, NodeType.MAT_VIEW, NodeType.ENDPOINT])
+ # a node outlives the query it points at, because deleting one is best-effort while soft-deleting
+ # the query is not. running it can only fail, so it must not reach the materialization activity.
+ executable_nodes = nodes.filter(type__in=[NodeType.VIEW, NodeType.MAT_VIEW, NodeType.ENDPOINT]).exclude(
+ saved_query__deleted=True
+ )
ephemeral_nodes = executable_nodes.filter(type=NodeType.VIEW)
edges = (
Edge.objects.prefetch_related("source", "target")
diff --git a/posthog/temporal/tests/data_modeling/test_execute_dag_workflow.py b/posthog/temporal/tests/data_modeling/test_execute_dag_workflow.py
index 2aa2d48eb2d0..2ac626d01b86 100644
--- a/posthog/temporal/tests/data_modeling/test_execute_dag_workflow.py
+++ b/posthog/temporal/tests/data_modeling/test_execute_dag_workflow.py
@@ -140,6 +140,32 @@ class TestGetDagStructureActivity:
source_node = dag_nodes[0]
assert str(source_node.id) not in dag.executable_nodes
+ async def test_excludes_nodes_whose_saved_query_was_deleted(
+ self, activity_environment, ateam, saved_queries, dag_nodes, adag
+ ):
+ ghost_query, ghost_node = saved_queries[0], dag_nodes[1]
+ ghost_query.deleted = True
+ await database_sync_to_async(ghost_query.save)()
+
+ inputs = GetDAGStructureInputs(team_id=ateam.pk, dag_id=str(adag.id))
+ dag = await activity_environment.run(get_dag_structure_activity, inputs)
+
+ assert str(ghost_node.id) not in dag.executable_nodes
+ assert len(dag.executable_nodes) == 2
+
+ async def test_keeps_nodes_whose_saved_query_has_no_deleted_flag(
+ self, activity_environment, ateam, saved_queries, dag_nodes, adag
+ ):
+ # deleted is nullable and null on most rows, a case an exclude() can silently take with it
+ for query in saved_queries:
+ query.deleted = None
+ await database_sync_to_async(query.save)()
+
+ inputs = GetDAGStructureInputs(team_id=ateam.pk, dag_id=str(adag.id))
+ dag = await activity_environment.run(get_dag_structure_activity, inputs)
+
+ assert len(dag.executable_nodes) == 3
+
@pytest.mark.parametrize("enforced", [True, False])
async def test_reports_suspended_nodes_only_when_enforced(
self, activity_environment, ateam, dag_nodes, adag, enforced