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): revert superseded materializations that were never called (#95024)

Jovan Sakovicec9579577e04
diff --git a/products/endpoints/backend/tasks/tasks.py b/products/endpoints/backend/tasks/tasks.py
index 51143ba968b3..52ea79b22abe 100644
--- a/products/endpoints/backend/tasks/tasks.py
+++ b/products/endpoints/backend/tasks/tasks.py
@@ -1,7 +1,7 @@
from datetime import timedelta
from typing import Any
-from django.db.models import Exists, OuterRef, Q
+from django.db.models import Exists, F, OuterRef, Q
from django.utils import timezone
from celery import shared_task
@@ -62,7 +62,8 @@ def deactivate_stale_materializations() -> None:
1. The version has an active materialization (saved_query.is_materialized = True)
2. The materialization has run in the past 24h (a completed job, or saved_query.last_run_at)
3. The materialization was enabled at least 30 days ago (saved_query.created_at)
- 4. The version was last executed over 30 days ago (via API key)
+ 4. The version was last executed over 30 days ago (via API key), or it is a superseded
+ version that was never executed
For matching versions, the materialization is reverted to save resources.
"""
@@ -77,8 +78,14 @@ def deactivate_stale_materializations() -> None:
)
ran_recently = Q(saved_query__last_run_at__gte=twenty_four_hours_ago) | Q(Exists(recent_job))
- version_stale = Q(last_executed_at__lt=stale_threshold) | Q(
- last_executed_at__isnull=True, endpoint__last_executed_at__lt=stale_threshold
+ # Both stamps are written together on every API-key call, so a superseded version with no stamp
+ # of its own has not been called since the stamp existed. The endpoint stamp belongs to the
+ # version that replaced it.
+ superseded = ~Q(version=F("endpoint__current_version"))
+ version_stale = (
+ Q(last_executed_at__lt=stale_threshold)
+ | Q(last_executed_at__isnull=True, endpoint__last_executed_at__lt=stale_threshold)
+ | (Q(last_executed_at__isnull=True) & superseded)
)
stale_versions = EndpointVersion.objects.filter(
diff --git a/products/endpoints/backend/tests/test_deactivate_stale_materializations.py b/products/endpoints/backend/tests/test_deactivate_stale_materializations.py
index 3e91cb6b2be6..442966798e60 100644
--- a/products/endpoints/backend/tests/test_deactivate_stale_materializations.py
+++ b/products/endpoints/backend/tests/test_deactivate_stale_materializations.py
@@ -6,6 +6,8 @@ from unittest import mock
from django.utils import timezone
+from parameterized import parameterized
+
from products.data_modeling.backend.facade.models import DataModelingJob, DataWarehouseSavedQuery
from products.endpoints.backend.models import Endpoint, EndpointVersion
from products.endpoints.backend.tasks.tasks import (
@@ -249,15 +251,17 @@ class TestDeactivateStaleMaterializationsTask(BaseTest):
mock_deactivate.assert_called_once()
assert mock_deactivate.call_args[0][0].id == version.id
+ @parameterized.expand([("executed_long_ago", timedelta(days=45)), ("never_executed", None)])
@mock.patch("products.endpoints.backend.tasks.tasks._deactivate_version_materialization")
- def test_deactivates_superseded_version_of_an_active_endpoint(self, mock_deactivate):
+ def test_deactivates_superseded_version_of_an_active_endpoint(self, _name, old_version_age, mock_deactivate):
now = timezone.now()
endpoint, old_version = self._create_materialized_endpoint(
name="active_endpoint",
last_executed_at=now - timedelta(days=5),
materialization_created_at=now - timedelta(days=45),
)
- EndpointVersion.objects.filter(id=old_version.id).update(last_executed_at=now - timedelta(days=45))
+ old_stamp = now - old_version_age if old_version_age else None
+ EndpointVersion.objects.filter(id=old_version.id).update(last_executed_at=old_stamp)
current_saved_query = DataWarehouseSavedQuery.objects.create(
team=self.team,