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.

perf(tags): limit orphan tag cleanup to the tags a save removed (#120863)

posthog[bot]e750104893a2
diff --git a/posthog/api/tagged_item.py b/posthog/api/tagged_item.py
index 024fdebe8bc1..458d63da0f02 100644
--- a/posthog/api/tagged_item.py
+++ b/posthog/api/tagged_item.py
@@ -1,5 +1,6 @@
import dataclasses
-from collections.abc import Iterable, Sequence
+from collections import defaultdict
+from collections.abc import Collection, Iterable, Sequence
from typing import TYPE_CHECKING, Any, Optional, cast
from uuid import UUID
@@ -27,8 +28,22 @@ def set_tags_on_object(tags: list[str], obj: Any) -> list[TaggedItem]:
"""Set tags on a taggable object, creating/deleting TaggedItems as needed.
This is the core tag-setting logic extracted for reuse across serializers
- and bulk operations.
+ and bulk operations. It leaves orphaned tags in place; use ``replace_tags_on_object``
+ to also remove them.
"""
+ tagged_item_objects, _ = _set_tags_on_object(tags, obj)
+ return tagged_item_objects
+
+
+def replace_tags_on_object(tags: list[str], obj: Any) -> list[TaggedItem]:
+ """Set tags on an object, then delete the removed tags that no other object uses."""
+ tagged_item_objects, removed_tag_ids = _set_tags_on_object(tags, obj)
+ cleanup_orphan_tags(obj.team_id, removed_tag_ids)
+ return tagged_item_objects
+
+
+def _set_tags_on_object(tags: list[str], obj: Any) -> tuple[list[TaggedItem], set[int]]:
+ """Set tags on an object and return its tagged items and the ids of the tags it lost."""
# Concurrent transactions must insert shared tags in the same order to avoid deadlocks.
deduped_tags = sorted({tagify(t) for t in tags})
tagged_item_objects = []
@@ -40,16 +55,24 @@ def set_tags_on_object(tags: list[str], obj: Any) -> list[TaggedItem]:
tagged_item_objects.append(tagged_item_instance)
# Delete tags that are missing (use individual deletes to trigger activity logging)
+ removed_tag_ids: set[int] = set()
tagged_items_to_delete = obj.tagged_items.exclude(tag__name__in=deduped_tags)
for tagged_item in tagged_items_to_delete:
+ removed_tag_ids.add(tagged_item.tag_id)
tagged_item.delete()
- return tagged_item_objects
+ return tagged_item_objects, removed_tag_ids
-def cleanup_orphan_tags(team_id: int) -> None:
- """Remove tags that are no longer referenced by any TaggedItem."""
- Tag.objects.filter(Q(team_id=team_id) & Q(tagged_items__isnull=True)).delete()
+def cleanup_orphan_tags(team_id: int, tag_ids: Collection[int]) -> None:
+ """Delete the given tags if no TaggedItem references them any more.
+
+ Only a removed TaggedItem can orphan a tag, so callers pass the ids of the tags they detached.
+ A scan of every tag of the team is slow on teams with many tags.
+ """
+ if not tag_ids:
+ return
+ Tag.objects.filter(Q(team_id=team_id) & Q(id__in=tag_ids) & Q(tagged_items__isnull=True)).delete()
def normalize_tag_names(tags: Iterable[str]) -> set[str]:
@@ -113,15 +136,15 @@ def apply_bulk_tag_changes(
"""
normalized_tags = normalize_tag_names(tags)
updated: list[dict[str, Any]] = []
- team_ids: set[int] = set()
+ removed_tag_ids_by_team: defaultdict[int, set[int]] = defaultdict(set)
activity_entries: list[LogActivityEntry] = []
for obj in objects:
- team_ids.add(obj.team_id)
current_tags = current_tag_names(obj)
new_tags = resolve_bulk_tags(current_tags, tag_action, normalized_tags)
- set_tags_on_object(list(new_tags), obj)
+ _, removed_tag_ids = _set_tags_on_object(list(new_tags), obj)
+ removed_tag_ids_by_team[obj.team_id] |= removed_tag_ids
updated.append({"id": obj.id, "tags": sorted(new_tags)})
if activity_context is not None and current_tags != new_tags:
@@ -129,8 +152,8 @@ def apply_bulk_tag_changes(
_bulk_tag_activity_entry(obj, sorted(current_tags), sorted(new_tags), activity_context)
)
- for team_id in team_ids:
- cleanup_orphan_tags(team_id)
+ for team_id, removed_tag_ids in removed_tag_ids_by_team.items():
+ cleanup_orphan_tags(team_id, removed_tag_ids)
if activity_entries:
bulk_log_activity(activity_entries)
@@ -175,8 +198,7 @@ class TaggedItemSerializerMixin(serializers.Serializer):
# If the object hasn't been created yet, this method will be called again on the create method.
return
- obj.prefetched_tags = set_tags_on_object(tags, obj)
- cleanup_orphan_tags(obj.team_id)
+ obj.prefetched_tags = replace_tags_on_object(tags, obj)
def to_representation(self, obj):
ret = super().to_representation(obj)
diff --git a/posthog/api/test/test_tagged_item.py b/posthog/api/test/test_tagged_item.py
index dbd61a42de33..f758e83c6871 100644
--- a/posthog/api/test/test_tagged_item.py
+++ b/posthog/api/test/test_tagged_item.py
@@ -372,6 +372,18 @@ class TestBulkUpdateTags(APIBaseTest):
assert not Tag.objects.filter(name="orphan-soon", team_id=self.team.id).exists()
+ @parameterized.expand([("unchanged", ["kept"]), ("added", ["kept", "new"])])
+ def test_save_without_removed_tags_leaves_unrelated_orphans(self, _name, tags):
+ dashboard = self._create_dashboard_with_tags("dash", ["kept"])
+ Tag.objects.create(name="unrelated-orphan", team_id=self.team.id)
+
+ response = self.client.patch(
+ f"/api/projects/{self.team.id}/dashboards/{dashboard.id}", {"tags": tags}, format="json"
+ )
+
+ assert response.status_code == status.HTTP_200_OK
+ assert Tag.objects.filter(name="unrelated-orphan", team_id=self.team.id).exists()
+
def test_tag_shared_by_other_objects_is_not_deleted_on_remove(self):
d1 = self._create_dashboard_with_tags("d1", ["shared-tag"])
d2 = self._create_dashboard_with_tags("d2", ["shared-tag"])
diff --git a/products/ai_observability/backend/resource_tags.py b/products/ai_observability/backend/resource_tags.py
index a4c0a0b303f6..a3e41a55cec9 100644
--- a/products/ai_observability/backend/resource_tags.py
+++ b/products/ai_observability/backend/resource_tags.py
@@ -6,7 +6,7 @@ from typing import TYPE_CHECKING
from django.db import models, transaction
from django.db.models import Exists, Prefetch, QuerySet
-from posthog.api.tagged_item import cleanup_orphan_tags, set_tags_on_object
+from posthog.api.tagged_item import replace_tags_on_object
from posthog.models.tagged_item import TaggedItem
if TYPE_CHECKING:
@@ -50,5 +50,4 @@ class ResourceTags:
with transaction.atomic():
# Serialize replacement on the resource so concurrent saves cannot merge two replacement sets.
type(resource)._base_manager.select_for_update().get(pk=resource.pk, team_id=resource.team_id)
- resource.__dict__["prefetched_tags"] = set_tags_on_object(tags, resource)
- cleanup_orphan_tags(resource.team_id)
+ resource.__dict__["prefetched_tags"] = replace_tags_on_object(tags, resource)
diff --git a/products/conversations/backend/api/ticket_actions.py b/products/conversations/backend/api/ticket_actions.py
index 5cb82729902f..4b1d9606a0cd 100644
--- a/products/conversations/backend/api/ticket_actions.py
+++ b/products/conversations/backend/api/ticket_actions.py
@@ -553,6 +553,8 @@ def handle_ticket_patch(request: Request, team: Team, ticket_id: str | uuid.UUID
# the per-instance delete() so the signal fires for them too; a bulk queryset
# delete would skip it. The trigger context attributes every resulting entry
# to the workflow that made the change.
+ # Only a removed TaggedItem can orphan a tag, so the cleanup checks just those tags.
+ removed_tag_ids: set[int] = set()
with ActivityTriggerContext(workflow_trigger):
if tags_mode == "remove":
for tagged_item in (
@@ -560,8 +562,10 @@ def handle_ticket_patch(request: Request, team: Team, ticket_id: str | uuid.UUID
.select_related("tag__team")
.prefetch_related("uuid_object")
):
+ removed_tag_ids.add(tagged_item.tag_id)
tagged_item.delete()
- Tag.objects.filter(team_id=team.id, tagged_items__isnull=True).delete()
+ if removed_tag_ids:
+ Tag.objects.filter(team_id=team.id, id__in=removed_tag_ids, tagged_items__isnull=True).delete()
elif tags_mode == "set":
for tag_name in normalized_tags:
tag_instance, _ = Tag.objects.get_or_create(name=tag_name, team_id=team.id)
@@ -571,8 +575,10 @@ def handle_ticket_patch(request: Request, team: Team, ticket_id: str | uuid.UUID
.select_related("tag__team")
.prefetch_related("uuid_object")
):
+ removed_tag_ids.add(tagged_item.tag_id)
tagged_item.delete()
- Tag.objects.filter(team_id=team.id, tagged_items__isnull=True).delete()
+ if removed_tag_ids:
+ Tag.objects.filter(team_id=team.id, id__in=removed_tag_ids, tagged_items__isnull=True).delete()
else:
for tag_name in normalized_tags:
tag_instance, _ = Tag.objects.get_or_create(name=tag_name, team_id=team.id)
diff --git a/products/customer_analytics/backend/facade/api.py b/products/customer_analytics/backend/facade/api.py
index d91a6aefeddf..cbd59a30d7f0 100644
--- a/products/customer_analytics/backend/facade/api.py
+++ b/products/customer_analytics/backend/facade/api.py
@@ -1014,7 +1014,10 @@ def _set_tags(
for tagged_item in account.tagged_items.exclude(tag__name__in=deduped_tags).select_related("tag"):
removed_tags.append(tagged_item.tag)
tagged_item.delete()
- Tag.objects.filter(Q(team_id=account.team_id) & Q(tagged_items__isnull=True)).delete()
+ if removed_tags:
+ Tag.objects.filter(
+ Q(team_id=account.team_id) & Q(id__in=[tag.id for tag in removed_tags]) & Q(tagged_items__isnull=True)
+ ).delete()
account.prefetched_tags = tagged_item_objects # type: ignore[attr-defined]
_schedule_account_tags_added(account, added_tags, actor, workflow_id=workflow_id)
_schedule_account_tags_removed(account, removed_tags, actor, workflow_id=workflow_id)
diff --git a/products/endpoints/backend/logic/crud.py b/products/endpoints/backend/logic/crud.py
index d2191d3c2990..e8102c1b8d78 100644
--- a/products/endpoints/backend/logic/crud.py
+++ b/products/endpoints/backend/logic/crud.py
@@ -17,7 +17,7 @@ from rest_framework.request import Request
from posthog.schema import EndpointRequest, HogQLQuery
-from posthog.api.tagged_item import cleanup_orphan_tags, set_tags_on_object
+from posthog.api.tagged_item import replace_tags_on_object
from posthog.clickhouse.query_tagging import Product
from posthog.event_usage import report_user_action
from posthog.exceptions_capture import capture_exception
@@ -42,10 +42,9 @@ def apply_tags(endpoint: Endpoint, tags: list[str] | None) -> None:
"""Replace the endpoint's tags. No-op when tags is None (field omitted)."""
if tags is None:
return
- # `prefetched_tags` is a dynamic attribute populated by the TaggedItem prefetch / set_tags_on_object;
+ # `prefetched_tags` is a dynamic attribute populated by the TaggedItem prefetch / replace_tags_on_object;
# it's not declared on the Endpoint model.
- endpoint.prefetched_tags = set_tags_on_object(tags, endpoint) # type: ignore[attr-defined]
- cleanup_orphan_tags(endpoint.team_id)
+ endpoint.prefetched_tags = replace_tags_on_object(tags, endpoint) # type: ignore[attr-defined]
@dataclasses.dataclass(frozen=True)
diff --git a/products/messaging/backend/services/message_templates.py b/products/messaging/backend/services/message_templates.py
index ce0fc04b7331..0480da5d329e 100644
--- a/products/messaging/backend/services/message_templates.py
+++ b/products/messaging/backend/services/message_templates.py
@@ -8,7 +8,7 @@ from django.db.models.fields.json import KT
from django.db.models.functions import Cast
from django.utils import timezone
-from posthog.api.tagged_item import cleanup_orphan_tags, current_tag_names, set_tags_on_object
+from posthog.api.tagged_item import current_tag_names, replace_tags_on_object, set_tags_on_object
from posthog.models import TaggedItem
from posthog.models.tag import tagify
@@ -125,8 +125,7 @@ def update_template(team_id: int, template_id: UUID | str, fields: dict[str, Any
_apply_fields(template, fields)
template.save()
if tags_changed:
- set_tags_on_object(tags, template)
- cleanup_orphan_tags(team_id)
+ replace_tags_on_object(tags, template)
return template
diff --git a/products/replay_vision/backend/tests/test_api.py b/products/replay_vision/backend/tests/test_api.py
index 939a5c8a6d6e..dc7c9ac2d725 100644
--- a/products/replay_vision/backend/tests/test_api.py
+++ b/products/replay_vision/backend/tests/test_api.py
@@ -1326,14 +1326,14 @@ class TestReplayScannerTags(_VisionAPITestCase):
self.assertEqual(report.call_args.args[2]["edited_fields"], ["tags"])
def test_create_rolls_back_scanner_when_tag_write_fails(self) -> None:
- with patch("posthog.api.tagged_item.set_tags_on_object", side_effect=RuntimeError("boom")):
+ with patch("posthog.api.tagged_item.replace_tags_on_object", side_effect=RuntimeError("boom")):
resp = self.client.post(self.scanners_url, data=self._scanner_payload("atomic", tags=["a"]), format="json")
self.assertEqual(resp.status_code, 500)
self.assertFalse(ReplayScanner.objects.filter(team=self.team, name="atomic").exists())
def test_update_rolls_back_columns_when_tag_write_fails(self) -> None:
scanner = self._create_scanner(description="before")
- with patch("posthog.api.tagged_item.set_tags_on_object", side_effect=RuntimeError("boom")):
+ with patch("posthog.api.tagged_item.replace_tags_on_object", side_effect=RuntimeError("boom")):
resp = self.client.patch(
f"{self.scanners_url}{scanner.id}/", data={"description": "after", "tags": ["a"]}, format="json"
)
diff --git a/products/tasks/backend/facade/api.py b/products/tasks/backend/facade/api.py
index d7b90559d2f8..79b6a9c8475e 100644
--- a/products/tasks/backend/facade/api.py
+++ b/products/tasks/backend/facade/api.py
@@ -50,7 +50,7 @@ from django.utils.http import content_disposition_header
import posthoganalytics
from rest_framework.exceptions import ValidationError
-from posthog.api.tagged_item import cleanup_orphan_tags, set_tags_on_object
+from posthog.api.tagged_item import replace_tags_on_object
from posthog.clickhouse.query_tagging import tag_queries
from posthog.dataclasses import frozen
from posthog.event_usage import groups
@@ -3807,8 +3807,7 @@ def set_task_run_summary(
with transaction.atomic():
run.state = TaskRun.update_state_atomic(run.id, updates=updates)
if tags is not None and not run.task.is_scout_experiment:
- set_tags_on_object(tags, run.task)
- cleanup_orphan_tags(run.team_id)
+ replace_tags_on_object(tags, run.task)
run.refresh_from_db()
run.publish_stream_state_event()
return _task_run_detail_to_dto(
diff --git a/products/workflows/backend/services/hog_flow_writes.py b/products/workflows/backend/services/hog_flow_writes.py
index 59f9edffecbb..ea0f31a83317 100644
--- a/products/workflows/backend/services/hog_flow_writes.py
+++ b/products/workflows/backend/services/hog_flow_writes.py
@@ -7,7 +7,7 @@ from django.db import transaction
from django.utils import timezone
from django.utils.dateparse import parse_datetime
-from posthog.api.tagged_item import cleanup_orphan_tags, current_tag_names, set_tags_on_object
+from posthog.api.tagged_item import current_tag_names, replace_tags_on_object, set_tags_on_object
from posthog.models.tag import tagify
from products.workflows.backend.facade.contracts import (
@@ -443,8 +443,7 @@ def update_workflow(
_append_revisions(team_id, user_id, instance, before_update)
if tags_changed:
- set_tags_on_object(tags, instance)
- cleanup_orphan_tags(team_id)
+ replace_tags_on_object(tags, instance)
paused = 0
if not route_to_draft: