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(endpoints): serve ineligible materializations inline (#110713)

Jovan Sakovic2cf4f5a0eae2
diff --git a/products/endpoints/backend/logic/materialized_serving.py b/products/endpoints/backend/logic/materialized_serving.py
index bc2b1bc2b3b6..a86ffe0e1b1b 100644
--- a/products/endpoints/backend/logic/materialized_serving.py
+++ b/products/endpoints/backend/logic/materialized_serving.py
@@ -104,6 +104,9 @@ def serving_state_for_version(
ready: bool,
materialized_at: datetime | None,
) -> MaterializedServingState:
+ # A version materialized before an eligibility rule tightened keeps its table, but the table
+ # can no longer answer the query correctly, so it serves inline until the query changes.
+ ready = ready and version.can_materialize()[0]
return MaterializedServingState(
ready=ready,
materialized_at=materialized_at,
diff --git a/products/endpoints/backend/tests/test_endpoint_materialization.py b/products/endpoints/backend/tests/test_endpoint_materialization.py
index 6ad48160b015..0672a398a10f 100644
--- a/products/endpoints/backend/tests/test_endpoint_materialization.py
+++ b/products/endpoints/backend/tests/test_endpoint_materialization.py
@@ -913,8 +913,31 @@ class TestEndpointMaterialization(ClickhouseTestMixin, APIBaseTest):
mock_inline.assert_called_once()
mock_materialized.assert_not_called()
- def test_fresh_materialized_data_uses_materialized_table(self):
+ @parameterized.expand(
+ [
+ ("eligible_query_serves_materialized", None, "materialized"),
+ # Rules can tighten after a version is materialized; the table must stop being served then.
+ # countDistinct over a range variable is one such query: a bucketed table cannot re-aggregate it.
+ (
+ "ineligible_query_serves_inline",
+ {
+ "kind": "HogQLQuery",
+ "query": "SELECT countDistinct(person_id) FROM events "
+ "WHERE timestamp >= {variables.start_ts} AND timestamp < {variables.end_ts}",
+ "variables": {
+ "var-1": {"variableId": "var-1", "code_name": "start_ts", "value": "2024-01-01"},
+ "var-2": {"variableId": "var-2", "code_name": "end_ts", "value": "2024-02-01"},
+ },
+ },
+ "inline",
+ ),
+ ]
+ )
+ def test_fresh_materialized_data_uses_materialized_table(
+ self, _name: str, ineligible_query: dict[str, Any] | None, expected_path: str
+ ) -> None:
"""Test that fresh materialized data uses the materialized table for faster execution."""
+ query = ineligible_query if ineligible_query is not None else self.sample_hogql_query
# Create a materialized endpoint with fresh data
now = timezone.now()
saved_query = DataWarehouseSavedQuery.objects.create(
@@ -937,7 +960,7 @@ class TestEndpointMaterialization(ClickhouseTestMixin, APIBaseTest):
endpoint = create_endpoint_with_version(
name="fresh_data_endpoint",
team=self.team,
- query=self.sample_hogql_query,
+ query=query,
created_by=self.user,
is_active=True,
)
@@ -962,9 +985,8 @@ class TestEndpointMaterialization(ClickhouseTestMixin, APIBaseTest):
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
- # Should use materialized table because data is fresh
- mock_materialized.assert_called_once()
- mock_inline.assert_not_called()
+ self.assertEqual(mock_materialized.call_count, int(expected_path == "materialized"))
+ self.assertEqual(mock_inline.call_count, int(expected_path == "inline"))
@parameterized.expand(
[