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(insights): identify persons in retention actor-query CSV exports (#60319)

Steven Shults9ed0ca7864e8
diff --git a/frontend/src/queries/nodes/DataTable/DataTableExport.tsx b/frontend/src/queries/nodes/DataTable/DataTableExport.tsx
index d08f20429048..8682e355cf8b 100644
--- a/frontend/src/queries/nodes/DataTable/DataTableExport.tsx
+++ b/frontend/src/queries/nodes/DataTable/DataTableExport.tsx
@@ -86,7 +86,14 @@ export async function startDownload(
}
if (columns.includes('person')) {
- columns = columns.map((c: string) => (c === 'person' ? 'person.distinct_ids.0' : c))
+ // Expand the `person` column to `person.id` (canonical UUID, always populated),
+ // `person.distinct_ids.0` (may be blank when the person can't be hydrated), and
+ // `person.is_unresolved` (true for merged/deleted persons whose Postgres row is
+ // missing). The id keeps every row identifiable; is_unresolved must be listed
+ // explicitly so the backend's user_columns filter doesn't drop it.
+ columns = columns.flatMap((c: string) =>
+ c === 'person' ? ['person.id', 'person.distinct_ids.0', 'person.is_unresolved'] : [c]
+ )
}
columns = columns.filter((n: string) => !columnDisallowList.includes(n))
diff --git a/frontend/src/scenes/retention/RetentionModal.tsx b/frontend/src/scenes/retention/RetentionModal.tsx
index c2399d068412..2de9516c6d79 100644
--- a/frontend/src/scenes/retention/RetentionModal.tsx
+++ b/frontend/src/scenes/retention/RetentionModal.tsx
@@ -211,11 +211,12 @@ export function RetentionModal(): JSX.Element | null {
personAppearances.person.properties
)}
</LemonButton>
- ) : 'distinct_ids' in personAppearances.person ? (
+ ) : 'distinct_ids' in personAppearances.person &&
+ personAppearances.person.distinct_ids?.[0] ? (
<LemonButton
size="small"
to={urls.personByDistinctId(
- personAppearances.person.distinct_ids?.[0]
+ personAppearances.person.distinct_ids[0]
)}
data-attr="retention-person-link"
>
diff --git a/frontend/src/types.ts b/frontend/src/types.ts
index 046a772ecef4..6fc5d95fa048 100644
--- a/frontend/src/types.ts
+++ b/frontend/src/types.ts
@@ -1585,8 +1585,15 @@ export interface PersonActorType extends CommonActorType {
type: 'person'
id: string
name?: string
- distinct_ids: string[]
- is_identified: boolean
+ // distinct_ids and is_identified are required for resolved persons but absent on
+ // stubs returned when the ClickHouse actor_id couldn't be hydrated from Postgres
+ // (see is_unresolved). Callers must guard for the stub case.
+ distinct_ids?: string[]
+ is_identified?: boolean
+ // Set when the person's actor_id from ClickHouse couldn't be hydrated from Postgres
+ // (typically because the person was merged into another or deleted). When true,
+ // distinct_ids/is_identified/properties/created_at may all be missing.
+ is_unresolved?: boolean
}
export interface GroupActorType extends CommonActorType {
diff --git a/posthog/hogql_queries/actors_query_runner.py b/posthog/hogql_queries/actors_query_runner.py
index 4b7c364becac..6cbaecd9f1fa 100644
--- a/posthog/hogql_queries/actors_query_runner.py
+++ b/posthog/hogql_queries/actors_query_runner.py
@@ -146,8 +146,16 @@ class ActorsQueryRunner(AnalyticsQueryRunner[ActorsQueryResponse]):
actor_data: dict[str, Any] = {"id": actor_id}
if self.group_type_index is not None:
actor_data["group_type_index"] = self.group_type_index
+ else:
+ # Person stubs occur when the actor_id from ClickHouse can't be
+ # hydrated from Postgres — typically because the person was merged
+ # into another or deleted. Flag the row so CSV consumers can tell
+ # these apart from fully hydrated persons.
+ actor_data["is_unresolved"] = True
if events_distinct_id_lookup is not None:
- actor_data["distinct_ids"] = events_distinct_id_lookup.get(actor_id)
+ # Default to [] to keep the stub shape consistent — downstream CSV
+ # flattening calls len() on this and would crash on None.
+ actor_data["distinct_ids"] = events_distinct_id_lookup.get(actor_id) or []
new_row[actor_column_index] = actor_data
if recordings_column_index is not None and recordings_lookup is not None:
new_row[recordings_column_index] = (
diff --git a/posthog/tasks/exports/test/test_csv_exporter.py b/posthog/tasks/exports/test/test_csv_exporter.py
index 04ff913b6568..08de7a0dea63 100644
--- a/posthog/tasks/exports/test/test_csv_exporter.py
+++ b/posthog/tasks/exports/test/test_csv_exporter.py
@@ -972,9 +972,9 @@ class TestCSVExporter(APIBaseTest):
self.assertEqual(
lines,
[
- "actor.id,actor.is_identified,actor.created_at,actor.last_seen_at,actor.distinct_ids.0,event_count,event_distinct_ids.0",
- "4beb316f-23aa-2584-66d3-4a1b8ab458f2,False,2022-06-01 12:00:00+00:00,,user_1,2,user_1",
- "d0780d6b-ccd0-44fa-a227-47efe4f3f30d,,,,user_2,1,user_2",
+ "actor.id,actor.is_identified,actor.created_at,actor.last_seen_at,actor.distinct_ids.0,actor.is_unresolved,event_count,event_distinct_ids.0",
+ "4beb316f-23aa-2584-66d3-4a1b8ab458f2,False,2022-06-01 12:00:00+00:00,,user_1,,2,user_1",
+ "d0780d6b-ccd0-44fa-a227-47efe4f3f30d,,,,user_2,True,1,user_2",
],
)