: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
fix(customer-analytics): Read all matched rows in custom property sync (#67970)
Arthur Moreira de Deus4fd66dfaee1c
diff --git a/products/customer_analytics/backend/logic/custom_property_sync.py b/products/customer_analytics/backend/logic/custom_property_sync.pyindex 49f8df2a1839..c658ee76c298 100644--- a/products/customer_analytics/backend/logic/custom_property_sync.py+++ b/products/customer_analytics/backend/logic/custom_property_sync.py@@ -13,7 +13,10 @@ from uuid import UUIDfrom django.db import transactionfrom django.utils import timezone+import structlog+from posthog.hogql import ast+from posthog.hogql.constants import MAX_SELECT_RETURNED_ROWSfrom posthog.hogql.query import execute_hogql_queryfrom posthog.clickhouse.query_tagging import Feature, Product, tags_context@@ -27,7 +30,10 @@ from products.customer_analytics.backend.logic.custom_property_values import ()from products.customer_analytics.backend.models import Account, CustomPropertySource+logger = structlog.get_logger(__name__)+_WRITE_CONFLICT_RETRIES = 3+_SYNC_KEYS_PER_QUERY = 1000# Mirrors data_modeling's CONSECUTIVE_TIMEOUTS_TO_PAUSE: auto-disable a source that keeps failing.MAX_CONSECUTIVE_SYNC_FAILURES = 5@@ -39,6 +45,8 @@ class SyncResult:view_found: boolwritten: int = 0unmatched_keys: int = 0+ accounts_total: int = 0+ rows_fetched: int = 0source_errors: dict[str, str] = dataclasses.field(default_factory=dict)@@ -71,34 +79,40 @@ def sync_custom_property_values(*, team_id: int, saved_query_id: str | UUID) ->ordered = sorted(selected_columns)column_index = {column: position for position, column in enumerate(ordered)}- rows = _read_view(Team.objects.get(id=team_id), saved_query.name, ordered)- accounts = _accounts_by_external_id(team_id, rows, column_index, usable)+ account_ids_by_external_id = _get_account_ids_by_external_id(team_id)+ external_ids = sorted(account_ids_by_external_id)+ team = Team.objects.get(id=team_id)+ rows_by_key_column = {+ key_column: _read_view(team, saved_query.name, ordered, key_column, external_ids)+ for key_column in sorted({source.key_column for source in usable})+ }+ result.accounts_total = len(account_ids_by_external_id)+ result.rows_fetched = sum(len(rows) for rows in rows_by_key_column.values())unmatched: set[Any] = set()for source in usable:key_index, value_index = column_index[source.key_column], column_index[source.source_column]- for row in rows:+ for row in rows_by_key_column[source.key_column]:key = row[key_index]if key is None:continue- account = accounts.get(str(key))- if account is None:+ account_id = account_ids_by_external_id.get(str(key))+ if account_id is None:unmatched.add(key)continueif row[value_index] is None:continue- if _write(team_id=team_id, account_id=account.id, source=source, value=row[value_index], result=result):+ if _write(team_id=team_id, account_id=account_id, source=source, value=row[value_index], result=result):result.written += 1result.unmatched_keys = len(unmatched)return result-def _accounts_by_external_id(- team_id: int, rows: list, column_index: dict[str, int], sources: list[CustomPropertySource]-) -> dict[str | None, Account]:- keys = {row[column_index[source.key_column]] for source in sources for row in rows}- external_ids = [str(key) for key in keys if key is not None]- return {a.external_id: a for a in Account.objects.for_team(team_id).filter(external_id__in=external_ids)}+def _get_account_ids_by_external_id(team_id: int) -> dict[str, UUID]:+ accounts = Account.objects.for_team(team_id).exclude(external_id=None).exclude(external_id="")+ return {+ external_id: account_id for external_id, account_id in accounts.values_list("external_id", "id") if external_id+ }def _write(*, team_id: int, account_id: Any, source: CustomPropertySource, value: Any, result: SyncResult) -> bool:@@ -123,15 +137,37 @@ def _write(*, team_id: int, account_id: Any, source: CustomPropertySource, valuereturn False-def _read_view(team: Team, view_name: str, columns: list[str]) -> list:- query = ast.SelectQuery(- select=[ast.Field(chain=[column]) for column in columns],- select_from=ast.JoinExpr(table=ast.Field(chain=[view_name])),- )+def _read_view(team: Team, view_name: str, columns: list[str], key_column: str, external_ids: list[str]) -> list:+ """Reads only view rows whose key matches a known account external_id, batching the key filter to+ keep each query small. An unfiltered read would be silently capped by the HogQL default limit —+ views are often orders of magnitude larger than the account set they enrich. Filtering on a single+ key column keeps batches disjoint: a row matches at most one batch, so no duplicates accumulate."""+ rows: list = []with tags_context(product=Product.CUSTOMER_ANALYTICS, feature=Feature.ACCOUNTS, team_id=team.pk):- # Runs as a userless system sync, so bypass user-scoped warehouse-view access control- # (it fails closed without a user); tenant isolation still holds via team.- return execute_hogql_query(query, team=team, bypass_warehouse_access_control=True).results or []+ for start in range(0, len(external_ids), _SYNC_KEYS_PER_QUERY):+ batch = external_ids[start : start + _SYNC_KEYS_PER_QUERY]+ query = ast.SelectQuery(+ select=[ast.Field(chain=[column]) for column in columns],+ select_from=ast.JoinExpr(table=ast.Field(chain=[view_name])),+ where=ast.CompareOperation(+ op=ast.CompareOperationOp.In,+ left=ast.Call(name="toString", args=[ast.Field(chain=[key_column])]),+ right=ast.Array(exprs=[ast.Constant(value=external_id) for external_id in batch]),+ ),+ limit=ast.Constant(value=MAX_SELECT_RETURNED_ROWS),+ )+ # Runs as a userless system sync, so bypass user-scoped warehouse-view access control+ # (it fails closed without a user); tenant isolation still holds via team.+ batch_rows = execute_hogql_query(query, team=team, bypass_warehouse_access_control=True).results or []+ if len(batch_rows) >= MAX_SELECT_RETURNED_ROWS:+ logger.warning(+ "custom_property_sync.view_read_truncated",+ team_id=team.pk,+ view_name=view_name,+ batch_keys=len(batch),+ )+ rows.extend(batch_rows)+ return rowsdef record_sync_outcome(@@ -189,4 +225,15 @@ def run_custom_property_sync(*, team_id: int, saved_query_id: str | UUID) -> Synview_found=result.view_found,source_errors=result.source_errors,)+ logger.info(+ "custom_property_sync.completed",+ team_id=team_id,+ saved_query_id=str(saved_query_id),+ view_found=result.view_found,+ written=result.written,+ unmatched_keys=result.unmatched_keys,+ accounts_total=result.accounts_total,+ rows_fetched=result.rows_fetched,+ source_errors=len(result.source_errors),+ )return resultdiff --git a/products/customer_analytics/backend/logic/custom_property_sync_test.py b/products/customer_analytics/backend/logic/custom_property_sync_test.pyindex e3c932d5099b..93ab9b3db613 100644--- a/products/customer_analytics/backend/logic/custom_property_sync_test.py+++ b/products/customer_analytics/backend/logic/custom_property_sync_test.py@@ -137,6 +137,14 @@ class CustomPropertySyncTest(TeamScopedTestMixin, BaseTest):assert source.last_sync_error == "boom"mock_capture.assert_called_once()+ def test_read_view_batches_key_filter_and_merges_rows(self):+ batch_size = "products.customer_analytics.backend.logic.custom_property_sync._SYNC_KEYS_PER_QUERY"+ responses = [_Response([(100.0, "acme")]), _Response([(200.0, "globex")])]+ with patch(_EXECUTE, side_effect=responses), patch(batch_size, 1):+ rows = _read_view(self.team, "billing_view", ["mrr", "org_id"], "org_id", ["acme", "globex"])++ assert rows == [(100.0, "acme"), (200.0, "globex")]+@patch("posthoganalytics.feature_enabled", new=Mock(return_value=True))class ReadViewAccessControlTest(ClickhouseTestMixin, TeamScopedTestMixin, BaseTest):@@ -150,11 +158,32 @@ class ReadViewAccessControlTest(ClickhouseTestMixin, TeamScopedTestMixin, BaseTecolumns={"org_id": "String", "health_score": "Int64"},)- rows = _read_view(self.team, view.name, ["health_score", "org_id"])+ rows = _read_view(self.team, view.name, ["health_score", "org_id"], "org_id", ["acme"])assert rows == [(100, "acme")]+class ReadViewLimitTest(ClickhouseTestMixin, TeamScopedTestMixin, BaseTest):+ def test_reads_all_matching_rows_beyond_default_hogql_limit(self):+ # An unfiltered, unlimited read gets capped at 100 rows by the HogQL default limit,+ # silently dropping most of a large view. 120 matches > 100 proves the cap is gone;+ # < 150 proves rows without a matching external_id are filtered out server-side.+ view = DataWarehouseSavedQuery.objects.create(+ team=self.team,+ name="org_scores",+ query={+ "kind": "HogQLQuery",+ "query": "SELECT toString(number) AS org_id, number AS score FROM numbers(150)",+ },+ columns={"org_id": "String", "score": "Int64"},+ )+ external_ids = [str(n) for n in range(120)]++ rows = _read_view(self.team, view.name, ["org_id", "score"], "org_id", external_ids)++ assert len(rows) == 120++class RecordSyncOutcomeTest(TeamScopedTestMixin, BaseTest):def setUp(self):super().setUp()