: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
chore(cohorts): split the resave_cohorts per-team loop (#92922)
Paul D'Ambraef062b5aad0c
diff --git a/posthog/management/commands/resave_cohorts.py b/posthog/management/commands/resave_cohorts.pyindex 5e8685356c26..b59ddea84a57 100644--- a/posthog/management/commands/resave_cohorts.py+++ b/posthog/management/commands/resave_cohorts.py@@ -10,7 +10,7 @@ from posthog.api.cohort import validate_filters_and_compute_realtime_supportfrom posthog.dataclasses import frozenfrom posthog.models.team.team import Team-from products.cohorts.backend.models.cohort import Cohort, CohortType+from products.cohorts.backend.models.cohort import Cohort, CohortOrEmpty, CohortTypefrom products.cohorts.backend.models.util import get_all_cohort_dependencies, sort_cohorts_topologicallyfrom products.cohorts.backend.realtime_teams import is_realtime_cohort_team, realtime_allowlist_matches_every_team@@ -41,6 +41,16 @@ class CohortResaveStats:prospective_realtime: int+@frozen+class CohortResaveOutcome:+ changed: bool+ validation_failed: bool+ prospective_realtime: bool+++UNTOUCHED_COHORT = CohortResaveOutcome(changed=False, validation_failed=False, prospective_realtime=False)++@frozenclass TeamSelection:team_ids: tuple[int, ...] | None@@ -266,41 +276,19 @@ class Command(BaseCommand):def _process_team_cohorts(self, team: Team, batch_size: int, dry_run: bool) -> CohortResaveStats:"""Process all cohorts for a single team."""- # Initialize stats for this team+ all_cohorts = self._load_team_cohorts(team, batch_size)+ seen_cohorts_cache: dict[int, CohortOrEmpty] = {c.id: c for c in all_cohorts}+ cohort_dependencies = self._build_dependency_map(all_cohorts, seen_cohorts_cache)++ # Sort cohorts topologically - dependencies first, then dependents+ sorted_cohort_ids = sort_cohorts_topologically({c.id for c in all_cohorts}, seen_cohorts_cache)+total = 0changed = 0errors = 0validation_errors = 0prospective_realtime = 0- # Get all cohorts for this team using pagination- base_qs = Cohort.objects.filter(team=team).order_by("id")- all_cohorts = []- last_id = 0-- while True:- batch = list(base_qs.filter(id__gt=last_id)[:batch_size])- if not batch:- break- all_cohorts.extend(batch)- last_id = batch[-1].id-- # Build dependency information for all cohorts- seen_cohorts_cache = {c.id: c for c in all_cohorts}- cohort_dependencies = {} # cohort_id -> set of all cohort ids it depends on-- for cohort in all_cohorts:- if not cohort.filters:- continue- # Get ALL dependencies recursively (A->B->C means A depends on both B and C)- dependencies = get_all_cohort_dependencies(cohort, seen_cohorts_cache=seen_cohorts_cache)- dependency_ids = {dep.id for dep in dependencies}- cohort_dependencies[cohort.id] = dependency_ids-- # Sort cohorts topologically - dependencies first, then dependents- sorted_cohort_ids = sort_cohorts_topologically({c.id for c in all_cohorts}, seen_cohorts_cache)-- # Process cohorts in dependency orderfor cohort_id in sorted_cohort_ids:cohort = seen_cohorts_cache.get(cohort_id)if not cohort:@@ -308,75 +296,15 @@ class Command(BaseCommand):total += 1try:- # Skip cohorts without filters (nothing to recompute)- if not cohort.filters:- continue-- # Compute the new filters with inline bytecode and cohort_type- # Use defensive validation with detailed error reporting- clean_filters, computed_type, validation_error_list = validate_filters_and_compute_realtime_support(- cohort.filters, cohort.team, current_cohort_type=cohort.cohort_type, cohort_count=cohort.count- )-- # If validation failed but we got the original filters back, log the issue and skip- if validation_error_list:+ outcome = self._recompute_cohort(cohort, team, seen_cohorts_cache, cohort_dependencies)+ if outcome.validation_failed:validation_errors += 1- logger.warning(- "cohort_validation_skipped",- cohort_id=cohort.id,- team_id=team.pk,- reason="Invalid filter structure - keeping original filters",- )- continue-- # Check if any directly referenced cohorts have dependencies- if computed_type == "realtime" and cohort.filters:- direct_refs = self._get_direct_cohort_references(cohort.filters)- for ref_id in direct_refs:- ref_cohort = seen_cohorts_cache.get(ref_id)- if ref_cohort:- # Static cohorts cannot be realtime, so any cohort referencing them can't be realtime- if ref_cohort.is_static:- computed_type = None- break- # Cohorts without filters (empty cohorts) can be considered realtime-compatible- # since they match no one (always false)- if not ref_cohort.filters:- continue- # If any directly referenced cohort has dependencies, this cannot be realtime- if ref_id in cohort_dependencies and len(cohort_dependencies[ref_id]) > 0:- computed_type = None- break- # Also check if the referenced cohort is not realtime- if ref_cohort.cohort_type != "realtime":- computed_type = None- break-- computed_condition_type = Cohort.compute_condition_type(clean_filters)-- # Decide if there is any change worth persisting/reporting- will_change = (- clean_filters != cohort.filters- or computed_type != cohort.cohort_type- or computed_condition_type != cohort.condition_type- )-- # ALWAYS update in-memory for dependency checking- cohort.filters = clean_filters- cohort.cohort_type = computed_type- cohort.condition_type = computed_condition_type-- # Track summary stats- if computed_type == "realtime":+ # Counted before the save, so a realtime cohort whose save raises still reports as one.+ if outcome.prospective_realtime:prospective_realtime += 1- if dry_run:- if will_change:- changed += 1- continue-- # Persist changes to database if needed- if will_change:- cohort.save(update_fields=["filters", "cohort_type", "condition_type"])+ if outcome.changed:+ if not dry_run:+ cohort.save(update_fields=["filters", "cohort_type", "condition_type"])changed += 1except Exception as err:errors += 1@@ -396,6 +324,120 @@ class Command(BaseCommand):prospective_realtime=prospective_realtime,)+ def _load_team_cohorts(self, team: Team, batch_size: int) -> list[Cohort]:+ """Read every cohort on the team, one page at a time."""+ # No `select_related("team")`: the caller already holds the team object and passes it into+ # `_recompute_cohort`, so joining it in here would only hydrate the deprecated taxonomy+ # columns (which TOAST out to megabytes) onto every retained cohort row. The forward-FK+ # defer that `TeamManager` applies does not reach a `select_related` JOIN.+ base_qs = Cohort.objects.filter(team=team).order_by("id")+ all_cohorts: list[Cohort] = []+ last_id = 0++ while True:+ batch = list(base_qs.filter(id__gt=last_id)[:batch_size])+ if not batch:+ return all_cohorts+ all_cohorts.extend(batch)+ last_id = batch[-1].id++ def _build_dependency_map(+ self, all_cohorts: list[Cohort], seen_cohorts_cache: dict[int, CohortOrEmpty]+ ) -> dict[int, set[int]]:+ """Map each cohort id to every cohort id it depends on, recursively: A->B->C makes A depend on+ both B and C."""+ cohort_dependencies: dict[int, set[int]] = {}+ for cohort in all_cohorts:+ if not cohort.filters:+ continue+ dependencies = get_all_cohort_dependencies(cohort, seen_cohorts_cache=seen_cohorts_cache)+ cohort_dependencies[cohort.id] = {dep.id for dep in dependencies}+ return cohort_dependencies++ def _recompute_cohort(+ self,+ cohort: Cohort,+ team: Team,+ seen_cohorts_cache: dict[int, CohortOrEmpty],+ cohort_dependencies: dict[int, set[int]],+ ) -> CohortResaveOutcome:+ """Recompute one cohort's filters, cohort_type, and condition_type in memory.++ This only mutates the cohort in memory and returns the outcome; the caller persists the+ change and counts it.+ """+ filters = cohort.filters+ # Nothing to recompute without filters.+ if not filters:+ return UNTOUCHED_COHORT++ # Compute the new filters with inline bytecode and cohort_type+ # Use defensive validation with detailed error reporting+ clean_filters, computed_type, validation_error_list = validate_filters_and_compute_realtime_support(+ filters, team, current_cohort_type=cohort.cohort_type, cohort_count=cohort.count+ )++ # If validation failed but we got the original filters back, log the issue and skip+ if validation_error_list:+ logger.warning(+ "cohort_validation_skipped",+ cohort_id=cohort.id,+ team_id=cohort.team_id,+ reason="Invalid filter structure - keeping original filters",+ )+ return CohortResaveOutcome(changed=False, validation_failed=True, prospective_realtime=False)++ if computed_type == "realtime" and self._references_block_realtime(+ filters, seen_cohorts_cache, cohort_dependencies+ ):+ computed_type = None++ computed_condition_type = Cohort.compute_condition_type(clean_filters)++ # Decide if there is any change worth persisting/reporting+ will_change = (+ clean_filters != filters+ or computed_type != cohort.cohort_type+ or computed_condition_type != cohort.condition_type+ )++ # ALWAYS update in-memory for dependency checking+ cohort.filters = clean_filters+ cohort.cohort_type = computed_type+ cohort.condition_type = computed_condition_type++ return CohortResaveOutcome(+ changed=will_change,+ validation_failed=False,+ prospective_realtime=computed_type == "realtime",+ )++ def _references_block_realtime(+ self,+ filters: dict[str, Any],+ seen_cohorts_cache: dict[int, CohortOrEmpty],+ cohort_dependencies: dict[int, set[int]],+ ) -> bool:+ """Whether a directly referenced cohort stops this one from evaluating in realtime."""+ for ref_id in self._get_direct_cohort_references(filters):+ ref_cohort = seen_cohorts_cache.get(ref_id)+ if not ref_cohort:+ continue+ # Static cohorts cannot be realtime, so any cohort referencing them can't be realtime+ if ref_cohort.is_static:+ return True+ # Cohorts without filters (empty cohorts) can be considered realtime-compatible+ # since they match no one (always false)+ if not ref_cohort.filters:+ continue+ # If any directly referenced cohort has dependencies, this cannot be realtime+ if cohort_dependencies.get(ref_id):+ return True+ # Also check if the referenced cohort is not realtime+ if ref_cohort.cohort_type != "realtime":+ return True+ return False+def _get_direct_cohort_references(self, filters: dict[str, Any]) -> set[int]:"""Get only the direct cohort references from filters (not transitive)."""referenced_ids = set()diff --git a/posthog/management/commands/test/test_resave_cohorts.py b/posthog/management/commands/test/test_resave_cohorts.pyindex 22d902fab8db..ddc85f99d380 100644--- a/posthog/management/commands/test/test_resave_cohorts.py+++ b/posthog/management/commands/test/test_resave_cohorts.py@@ -14,6 +14,7 @@ from django.test import override_settingsfrom parameterized import parameterizedfrom posthog.models.team.team import Team+from posthog.test.db_context_capturing import capture_db_queriesfrom products.cohorts.backend.models.cohort import Cohort@@ -194,6 +195,27 @@ class TestResaveCohortsCommandSingleTeam(BaseTest):"cohorts": False,}+ def test_command_does_not_hydrate_team_onto_each_cohort(self):+ # `_load_team_cohorts` retains every cohort in memory, so hydrating the team row onto each+ # cohort would pull the deprecated taxonomy columns (which TOAST out to megabytes per team)+ # back once per cohort. The command already holds the team object, so the cohort SELECT must+ # not JOIN and hydrate `posthog_team`. A query-count guard would miss a re-added+ # `select_related("team")`, so assert the SQL directly, as `test_list_cohorts_does_not_hydrate_team`+ # does.+ team: Team = self.team+ for i in range(3):+ Cohort.objects.create(team=team, name=f"c{i}", filters=_make_person_only_filters())++ with capture_db_queries() as context:+ call_command("resave_cohorts", team_id=[team.id])++ cohort_selects = [q["sql"] for q in context.captured_queries if 'FROM "posthog_cohort"' in q["sql"]]+ assert cohort_selects, "expected the command to SELECT from posthog_cohort"+ # `event_names` is a deprecated taxonomy column that TOASTs out to megabytes; its presence+ # would mean the team row is hydrated onto each cohort row.+ for sql in cohort_selects:+ assert 'posthog_team"."event_names"' not in sql+class TestResaveCohortsCommandWithDependencies(BaseTest):def test_cohort_dependency_blocks_realtime(self):