: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
perf(hogql): two-phase groups dedup to fix select-from-groups OOM (#66286)
Robbiebdecb8e1c241
diff --git a/posthog/hogql/database/schema/groups.py b/posthog/hogql/database/schema/groups.pyindex ed3515f516cb..60e15c7c9a1d 100644--- a/posthog/hogql/database/schema/groups.py+++ b/posthog/hogql/database/schema/groups.py@@ -1,3 +1,6 @@+from typing import cast++from posthog.hogql import astfrom posthog.hogql.ast import SelectQueryfrom posthog.hogql.context import HogQLContextfrom posthog.hogql.database.argmax import argmax_select@@ -16,6 +19,8 @@ from posthog.hogql.database.models import ()from posthog.hogql.database.schema.groups_revenue_analytics import GroupsRevenueAnalyticsTablefrom posthog.hogql.errors import ResolutionError+from posthog.hogql.parser import parse_select+from posthog.hogql.visitor import TraversingVisitorGROUPS_TABLE_FIELDS: dict[str, FieldOrTable] = {"index": IntegerDatabaseField(@@ -50,13 +55,91 @@ GROUPS_TABLE_FIELDS: dict[str, FieldOrTable] = {}-def select_from_groups_table(requested_fields: dict[str, list[str | int]]):- return argmax_select(+def select_from_groups_table(requested_fields: dict[str, list[str | int]], key_limit: int | None = None):+ select = argmax_select(table_name="raw_groups",select_fields=requested_fields,group_fields=["index", "key"],argmax_field="updated_at",)+ if key_limit is not None:+ # Two-phase dedup: pick the N group keys cheaply, then argMax the heavy properties for only those keys.+ keys = parse_select("SELECT index, key FROM raw_groups GROUP BY index, key")+ keys.limit = ast.Constant(value=key_limit)+ # GLOBAL IN, not IN: groups is Distributed(rand()); a plain IN dedups per-shard over a subset of versions.+ select.where = ast.CompareOperation(+ op=ast.CompareOperationOp.GlobalIn,+ left=ast.Tuple(exprs=[ast.Field(chain=["index"]), ast.Field(chain=["key"])]),+ right=keys,+ )+ return select+++def _resolved_table(table_type) -> object | None:+ # Unwrap alias wrappers (e.g. `FROM groups AS g`) to the underlying table.+ while isinstance(table_type, (ast.TableAliasType, ast.ColumnAliasedTableType)):+ table_type = table_type.table_type+ return getattr(table_type, "table", None)+++def _is_nonneg_int_constant(expr) -> bool:+ return isinstance(expr, ast.Constant) and isinstance(expr.value, int) and expr.value >= 0+++class _WindowFunctionFinder(TraversingVisitor):+ # Window functions evade the order_by and has_aggregation guards but must see every group, so bail on them too.+ found: bool = False++ def visit(self, node):+ if not self.found:+ super().visit(node)++ def visit_select_query(self, node: ast.SelectQuery):+ pass # a window inside a scalar subquery doesn't change this query's rows++ def visit_window_function(self, node: ast.WindowFunction):+ self.found = True+++def _has_window_function(expr: ast.Expr) -> bool:+ finder = _WindowFunctionFinder()+ finder.visit(expr)+ return finder.found+++def _bare_limit_key_count(node: SelectQuery) -> int | None:+ # Deferred: posthog.hogql.property imports database schema modules, so a top-level import here is circular.+ from posthog.hogql.property import has_aggregation # noqa: PLC0415++ # Only safe for a bare `from groups limit N`; bail to the full dedup on anything that changes the surviving rows.+ if (+ node.select_from is None+ or node.select_from.next_join is not None+ or not _is_nonneg_int_constant(node.limit)+ or (node.offset is not None and not _is_nonneg_int_constant(node.offset))+ or node.where+ or node.prewhere+ or node.having+ or node.qualify+ or node.group_by+ or node.distinct+ or node.order_by+ or node.array_join_op+ or node.limit_by+ or node.limit_with_ties+ or node.limit_percent+ or any(has_aggregation(expr) for expr in node.select)+ or node.window_exprs+ or any(_has_window_function(expr) for expr in node.select)+ or not isinstance(_resolved_table(node.select_from.type), GroupsTable)+ ):+ return None++ # Guard above proved these are int Constants; cast so mypy sees `.value`.+ node_limit = cast(ast.Constant, node.limit)+ node_offset = cast(ast.Constant, node.offset)+ # +1 (mirrors persons): lets the outer LIMIT detect whether more rows exist.+ return node_limit.value + (node_offset.value if node.offset else 0) + 1def join_with_group_n_table(@@ -64,8 +147,6 @@ def join_with_group_n_table(context: HogQLContext,node: SelectQuery,):- from posthog.hogql import ast-# Which $group_N events column to join on; carried as plain data instead of being# captured in a closure so the LazyJoin (and the Database holding it) stays serializable.group_index = join_to_add.lazy_join.resolver_params.get("group_index")@@ -119,7 +200,7 @@ class GroupsTable(LazyTable):fields: dict[str, FieldOrTable] = GROUPS_TABLE_FIELDSdef lazy_select(self, table_to_add: LazyTableToAdd, context, node):- return select_from_groups_table(table_to_add.fields_accessed)+ return select_from_groups_table(table_to_add.fields_accessed, key_limit=_bare_limit_key_count(node))def to_printed_clickhouse(self, context):return "groups"diff --git a/posthog/hogql/database/schema/test/test_groups.py b/posthog/hogql/database/schema/test/test_groups.pynew file mode 100644index 000000000000..2d6ef42ecc23--- /dev/null+++ b/posthog/hogql/database/schema/test/test_groups.py@@ -0,0 +1,157 @@+from datetime import datetime++from posthog.test.base import APIBaseTest, ClickhouseTestMixin++from parameterized import parameterized++from posthog.hogql.parser import parse_select+from posthog.hogql.query import execute_hogql_query++from posthog.models import Team+from posthog.models.group.util import create_group, raw_create_group_ch+++class TestGroupsLimitPushdown(ClickhouseTestMixin, APIBaseTest):+ def _team(self) -> Team:+ # Fresh team per test: ClickHouse isn't rolled back, and the inner team_id filter isolates this team's groups.+ return Team.objects.create(organization=self.organization)++ def _run(self, team: Team, query: str) -> tuple[str, list]:+ response = execute_hogql_query(parse_select(query), team)+ assert response.clickhouse is not None+ return response.clickhouse, response.results++ def test_bare_limit_two_phase_returns_correct_deduplicated_rows(self):+ team = self._team()+ # Exactly 10 groups so LIMIT 10 returns all of them (the inner key subquery is unordered).+ for i in range(1, 10):+ create_group(team_id=team.pk, group_type_index=0, group_key=f"g{i:02d}", properties={"name": f"name-{i}"})+ # g00 gets two CH rows (stale + latest) so the outer argMax must still return LATEST under the key limit.+ raw_create_group_ch(team.pk, 0, "g00", {"name": "STALE"}, datetime(2020, 1, 1), timestamp=datetime(2020, 1, 1))+ raw_create_group_ch(team.pk, 0, "g00", {"name": "LATEST"}, datetime(2024, 1, 1), timestamp=datetime(2024, 1, 1))++ sql, results = self._run(team, "SELECT key, properties.name FROM groups LIMIT 10")++ assert len(results) == 10+ # ...the returned keys are distinct (no dedup leak duplicating a group)+ assert len({key for key, _ in results}) == 10+ # ...and g00's value is the latest, not the stale one+ assert dict(results)["g00"] == "LATEST"+ # Assert the two-phase actually fired (results alone can't prove the argMax was limited).+ assert "globalIn(tuple(" in sql+ assert "LIMIT 11" in sql++ def test_aggregate_without_group_by_counts_all_groups(self):+ team = self._team()+ for i in range(12):+ create_group(team_id=team.pk, group_type_index=0, group_key=f"g{i:02d}", properties={})++ # count() collapses to one row, so the limit must not bound the dedup: a buggy pushdown would count 6, not 12.+ sql, results = self._run(team, "SELECT count() FROM groups LIMIT 5")++ assert results[0][0] == 12+ assert "globalIn(tuple(" not in sql++ def test_null_limit_does_not_crash_the_compiler(self):+ team = self._team()+ create_group(team_id=team.pk, group_type_index=0, group_key="g00", properties={})++ # LIMIT NULL is Constant(value=None); the guard must bail rather than crash on `None + 1` (CH rejects it later).+ try:+ self._run(team, "SELECT key FROM groups LIMIT NULL")+ except TypeError:+ raise AssertionError("LIMIT NULL crashed the HogQL compiler with a TypeError")+ except Exception:+ pass++ def test_order_by_limit_returns_global_top_and_is_not_limited(self):+ team = self._team()+ # Global max rank lives on g11; a wrongly-pushed limit could drop it before the ordering runs.+ for i in range(11):+ create_group(team_id=team.pk, group_type_index=0, group_key=f"g{i:02d}", properties={"rank": str(i)})+ create_group(team_id=team.pk, group_type_index=0, group_key="g11", properties={"rank": "999"})++ sql, results = self._run(team, "SELECT key FROM groups ORDER BY toInt(properties.rank) DESC LIMIT 5")++ # real-ClickHouse correctness: the global top-by-rank is returned+ assert len(results) == 5+ assert results[0][0] == "g11"+ # Deterministic check that the key limit was not applied.+ assert "LIMIT 6" not in sql++ @parameterized.expand(+ [+ ("where", "SELECT key FROM groups WHERE properties.tag = 'needle' LIMIT 5"),+ # HAVING is a post-dedup filter just like WHERE; pushing the limit before it would under-return.+ ("having", "SELECT key FROM groups HAVING properties.tag = 'needle' LIMIT 5"),+ ]+ )+ def test_post_dedup_filter_is_not_pushed(self, _name, query):+ team = self._team()+ # Only g11 matches; a pushed limit would keep the smallest keys and lose it after filtering.+ for i in range(11):+ create_group(team_id=team.pk, group_type_index=0, group_key=f"g{i:02d}", properties={"tag": "haystack"})+ create_group(team_id=team.pk, group_type_index=0, group_key="g11", properties={"tag": "needle"})++ sql, results = self._run(team, query)++ assert len(results) == 1+ assert results[0][0] == "g11"+ assert "LIMIT 6" not in sql++ def test_array_join_is_not_pushed(self):+ team = self._team()+ for i in range(12):+ create_group(team_id=team.pk, group_type_index=0, group_key=f"g{i:02d}", properties={})++ # ARRAY JOIN can drop source rows (empty array) after the dedup, so the limit must not be pushed into it.+ sql, _ = self._run(team, "SELECT key FROM groups ARRAY JOIN [1, 2] AS x LIMIT 5")++ assert "LIMIT 6" not in sql++ def test_distinct_limit_is_not_pushed(self):+ team = self._team()+ for i in range(8):+ create_group(team_id=team.pk, group_type_index=0, group_key=f"g{i:02d}", properties={})++ # DISTINCT can collapse limited rows below the requested count, so the limit must not be pushed.+ sql, results = self._run(team, "SELECT DISTINCT key FROM groups LIMIT 5")++ assert len(results) == 5+ assert "LIMIT 6" not in sql++ def test_window_function_is_not_pushed(self):+ team = self._team()+ for i in range(12):+ create_group(team_id=team.pk, group_type_index=0, group_key=f"g{i:02d}", properties={})++ # A window function must see every group; a pushed key limit would compute count() OVER () over the limited+ # set (window funcs aren't caught by the order_by or aggregate guards -- the window's ORDER BY is internal).+ sql, results = self._run(team, "SELECT key, count() OVER () AS total FROM groups LIMIT 5")++ assert results[0][1] == 12+ assert "globalIn(tuple(" not in sql+ assert "LIMIT 6" not in sql++ def test_aliased_groups_select_still_pushes_limit(self):+ team = self._team()+ for i in range(12):+ create_group(team_id=team.pk, group_type_index=0, group_key=f"g{i:02d}", properties={})++ # Aliasing the table must not lose the optimisation; the OOM happens for `groups AS g` too.+ sql, results = self._run(team, "SELECT g.key FROM groups AS g LIMIT 10")++ assert len(results) == 10+ assert "LIMIT 11" in sql++ def test_lazy_join_field_with_limit_returns_correct_groups(self):+ team = self._team()+ for i in range(12):+ create_group(team_id=team.pk, group_type_index=0, group_key=f"g{i:02d}", properties={})++ # revenue_analytics is a row-preserving LEFT JOIN attached after the pushdown, so pushing the limit is safe.+ sql, results = self._run(team, "SELECT key, revenue_analytics.revenue FROM groups LIMIT 10")++ assert len(results) == 10+ assert len({key for key, _ in results}) == 10+ assert "LIMIT 11" in sql