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: cohort id insert performance (#58753)

José Sequeira865178f4f046
diff --git a/posthog/models/cohort/cohort.py b/posthog/models/cohort/cohort.py
index 39886dcb5af0..c95f9a686e02 100644
--- a/posthog/models/cohort/cohort.py
+++ b/posthog/models/cohort/cohort.py
@@ -758,7 +758,7 @@ class Cohort(FileSystemSyncMixin, RootTeamMixin, models.Model):
# avoiding the O(cohort_size) memory cost of loading all
# existing member IDs into Python. Both tables live on the
# persons DB so the join works on the db_write cursor.
- sql, params = persons_query.distinct("pk").only("pk").query.sql_with_params()
+ sql, params = persons_query.only("pk").query.sql_with_params()
query = f"""
INSERT INTO "{cohort_people_table}" ("person_id", "cohort_id", "version")
SELECT p."id", {self.pk}, {self.version or "NULL"}