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(warehouse-sources): keep person-property diff linear on repeated keys (#113410)

Daniel RCd82edc9977f4
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/person_property_sync.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/person_property_sync.py
index bf95cdaa2433..ee7f10416d84 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/person_property_sync.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/person_property_sync.py
@@ -166,20 +166,15 @@ def select_changed(
) -> tuple[list[tuple[str, dict]], dict[str, str]]:
"""Keep only bundles whose hash differs from the snapshot. Returns (changed, hashes-for-changed).
Last write wins when a distinct_id appears twice in one run."""
- changed: list[tuple[str, dict]] = []
+ changed: dict[str, dict] = {}
new_hashes: dict[str, str] = {}
- seen: set[str] = set()
for distinct_id, bundle in bundles:
current = bundle_hash(bundle)
if prior_hashes.get(distinct_id) == current and distinct_id not in new_hashes:
continue
- if distinct_id in seen:
- # replace the earlier bundle for this run with the later one
- changed = [(d, b) for d, b in changed if d != distinct_id]
- changed.append((distinct_id, bundle))
+ changed[distinct_id] = bundle
new_hashes[distinct_id] = current
- seen.add(distinct_id)
- return changed, new_hashes
+ return list(changed.items()), new_hashes
# --- S3 / Kafka / personhog boundaries (mocked in tests) ---------------------------------
@@ -459,7 +454,9 @@ async def _process_source_bundles(
await database_sync_to_async(_reconcile_property_definitions, thread_sensitive=False)(
team_id, project_id, binding, source, list((source.column_property_map or {}).values())
)
- changed, new_hashes = select_changed(bundles, prior)
+ # Hashing every bundle is CPU work. Run it in a thread so that the event loop, and the
+ # heartbeater on it, keep running for a large table.
+ changed, new_hashes = await asyncio.to_thread(select_changed, bundles, prior)
ps.changed = len(changed)
if not changed:
return ps
@@ -554,7 +551,7 @@ async def run_person_property_sync(*, team_id: int, binding: WarehouseBinding, j
)
for source in sources:
- bundles = build_bundles(rows, source.key_column, source.column_property_map or {})
+ bundles = await asyncio.to_thread(build_bundles, rows, source.key_column, source.column_property_map or {})
ps = await _process_source_bundles(
team_id=team_id,
project_id=team.project_id,
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/person_property_sync_test.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/person_property_sync_test.py
index 85bd9ed9f2de..2a3b4fa1c6aa 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/person_property_sync_test.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/person_property_sync_test.py
@@ -103,6 +103,33 @@ class TestSelectChanged:
assert changed == [("a", {"plan_tier": "new"})]
assert new_hashes["a"] == pps.bundle_hash({"plan_tier": "new"})
+ def test_repeated_distinct_ids_do_not_rescan_the_changed_set(self):
+ comparisons = 0
+
+ class CountingId(str):
+ __hash__ = str.__hash__
+
+ def __eq__(self, other: object) -> bool:
+ nonlocal comparisons
+ comparisons += 1
+ return str.__eq__(self, other)
+
+ def __ne__(self, other: object) -> bool:
+ nonlocal comparisons
+ comparisons += 1
+ return str.__ne__(self, other)
+
+ distinct = 500
+ first_pass: list[tuple[str, dict]] = [(CountingId(f"id-{i}"), {"plan_tier": "old"}) for i in range(distinct)]
+ second_pass: list[tuple[str, dict]] = [(CountingId(f"id-{i}"), {"plan_tier": "new"}) for i in range(distinct)]
+
+ changed, new_hashes = pps.select_changed(first_pass + second_pass, {})
+
+ assert dict(changed) == {f"id-{i}": {"plan_tier": "new"} for i in range(distinct)}
+ assert len(changed) == distinct
+ assert new_hashes["id-0"] == pps.bundle_hash({"plan_tier": "new"})
+ assert comparisons < 20 * distinct
+
class TestRunOrchestration:
"""Orchestration control flow with all I/O boundaries (S3, personhog, Kafka, DB) mocked."""