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(aio): stop the users tab query from running out of memory (#90821)

posthog[bot]93b4b7fd4371
diff --git a/frontend/src/scenes/insights/EmptyStates/EmptyStates.tsx b/frontend/src/scenes/insights/EmptyStates/EmptyStates.tsx
index 6bad9e77bf05..beb398f7ffbb 100644
--- a/frontend/src/scenes/insights/EmptyStates/EmptyStates.tsx
+++ b/frontend/src/scenes/insights/EmptyStates/EmptyStates.tsx
@@ -710,10 +710,18 @@ export function isRawServerErrorTitle(title: string, status?: number | null): bo
return status != null && status >= 500
}
-type InsightErrorKind = 'rate_limit' | 'invalid_query' | 'permission' | 'transient' | 'server' | 'unknown'
+type InsightErrorKind =
+ | 'rate_limit'
+ | 'memory_limit'
+ | 'invalid_query'
+ | 'permission'
+ | 'transient'
+ | 'server'
+ | 'unknown'
const ERROR_HOGGIES: Record<InsightErrorKind, React.ComponentType<{ className?: string }>> = {
rate_limit: HedgehogTrafficController,
+ memory_limit: HedgehogMagnifyingGlass,
invalid_query: HedgehogMagnifyingGlass,
permission: HedgehogStampDenied,
transient: HedgehogConstruction2,
@@ -730,6 +738,12 @@ function getInsightErrorKind(status?: number | null): InsightErrorKind {
if (status === 429) {
return 'rate_limit'
}
+ // 513 covers every ClickHouse memory failure: the query's own limit, cluster-wide pressure, and
+ // the failure breaker holding a query that already failed this way. The status cannot tell them
+ // apart, so the remediation comes from the backend detail instead.
+ if (status === 513) {
+ return 'memory_limit'
+ }
if (status === 400 || status === 422) {
return 'invalid_query'
}
@@ -750,6 +764,9 @@ function getInsightErrorTitle(
fallback: string | JSX.Element | null | undefined,
titleStatus?: number | null
): string | JSX.Element {
+ if (kind === 'memory_limit') {
+ return "This query couldn't finish"
+ }
if (kind === 'invalid_query') {
return "We couldn't run this query"
}
@@ -768,10 +785,18 @@ function getInsightErrorTitle(
return fallback ?? 'There was a problem completing this query'
}
-function getInsightErrorRemediation(kind: InsightErrorKind, retryAfter?: string | null): string | null {
+function getInsightErrorRemediation(
+ kind: InsightErrorKind,
+ retryAfter?: string | null,
+ backendDetail?: string | null
+): string | null {
switch (kind) {
case 'rate_limit':
return `Try again ${retryAfter ?? 'later'}.`
+ case 'memory_limit':
+ // Only the backend copy knows whether to shrink this query, wait out cluster load, or
+ // how long the breaker holds the query for.
+ return backendDetail ?? 'Try a shorter date range or narrower filters, then run it again.'
case 'invalid_query':
return 'Open the query debugger and correct the query.'
case 'permission':
@@ -822,7 +847,9 @@ export function InsightErrorState({
const displayTitle = getInsightErrorTitle(errorKind, safeTitle, titleStatus)
const isExport = placement === DashboardPlacement.Export
const showBugReport = !isExport && (errorKind === 'transient' || errorKind === 'server' || errorKind === 'unknown')
- const remediation = getInsightErrorRemediation(errorKind, retryAfter)
+ // A 513 body is curated backend copy, unless a staff account got the raw ClickHouse trace back.
+ const backendDetail = typeof title === 'string' && !isRawServerErrorTitle(title) ? title : null
+ const remediation = getInsightErrorRemediation(errorKind, retryAfter, backendDetail)
const { preflight } = useValues(preflightLogic)
const { openSupportForm } = useActions(supportLogic)
@@ -875,7 +902,7 @@ export function InsightErrorState({
{!supportOnly && (
<div className="mt-4">
- {remediation && <p>{remediation}</p>}
+ {remediation && <p className="max-w-120">{renderDetailWithLinks(remediation)}</p>}
{!excludeDetail && showBugReport && <p>{bugReportLink}</p>}
</div>
)}
diff --git a/frontend/src/scenes/insights/EmptyStates/InsightErrorStates.test.tsx b/frontend/src/scenes/insights/EmptyStates/InsightErrorStates.test.tsx
index 539faa651d28..b274c2bf940f 100644
--- a/frontend/src/scenes/insights/EmptyStates/InsightErrorStates.test.tsx
+++ b/frontend/src/scenes/insights/EmptyStates/InsightErrorStates.test.tsx
@@ -9,6 +9,11 @@ import { DashboardPlacement } from '~/types'
import { InsightErrorState, InsightValidationError, isRawServerErrorTitle } from './EmptyStates'
+// Status 513 also covers cluster pressure, whose backend copy says to wait rather than to shrink
+// the query. Verbatim from ClickHouseClusterMemoryLimitExceeded.
+const CLUSTER_MEMORY_DETAIL =
+ "We're under heavy load right now and couldn't finish this query. Please try again in a few minutes."
+
describe('insight error states', () => {
let captureSpy: jest.SpyInstance
@@ -129,6 +134,12 @@ describe('insight error states', () => {
it.each([
{ status: 429, expectedCopy: 'Try again in 2 minutes.', retry: true },
+ {
+ status: 513,
+ title: CLUSTER_MEMORY_DETAIL,
+ expectedCopy: CLUSTER_MEMORY_DETAIL,
+ retry: true,
+ },
{ status: 400, expectedCopy: 'Open the query debugger and correct the query.', retry: false },
{
status: 403,
@@ -140,12 +151,12 @@ describe('insight error states', () => {
{ status: 503, expectedCopy: 'Try again in a moment.', retry: true, queryDebugger: false, bugReport: true },
])(
'shows the correct action for HTTP $status errors',
- ({ status, expectedCopy, retry, queryDebugger = !retry, bugReport = false }) => {
+ ({ status, expectedCopy, retry, title = 'The query failed.', queryDebugger = !retry, bugReport = false }) => {
preflightLogic.actions.loadPreflightSuccess({ cloud: true } as any)
const { container } = render(
<InsightErrorState
- title="The query failed."
+ title={title}
titleStatus={status}
retryAfter={status === 429 ? 'in 2 minutes' : undefined}
query={{ kind: 'InsightVizNode' }}
@@ -160,6 +171,15 @@ describe('insight error states', () => {
}
)
+ it('keeps raw ClickHouse traces out of memory failures', () => {
+ const rawTrace = 'Code: 241. DB::Exception: Memory limit (for query) exceeded. Stack trace:\n0.'
+ render(<InsightErrorState title={rawTrace} titleStatus={513} onRetry={() => {}} />)
+
+ expect(screen.getByText("This query couldn't finish")).toBeTruthy()
+ expect(screen.getByText('Try a shorter date range or narrower filters, then run it again.')).toBeTruthy()
+ expect(screen.queryByText(/DB::Exception/)).toBeNull()
+ })
+
it('uses user-facing copy for invalid query errors', () => {
render(<InsightErrorState title="This query is invalid" titleStatus={400} query={{ kind: 'InsightVizNode' }} />)
diff --git a/products/ai_observability/frontend/aiObservabilityColumnRenderers.tsx b/products/ai_observability/frontend/aiObservabilityColumnRenderers.tsx
index 37cbf6ff79a2..57e05c05ec50 100644
--- a/products/ai_observability/frontend/aiObservabilityColumnRenderers.tsx
+++ b/products/ai_observability/frontend/aiObservabilityColumnRenderers.tsx
@@ -173,7 +173,10 @@ function PersonColumnCellWithRedirect({ person }: { person: PersonData | null |
)
}
-export function LazyPersonColumnCell({ distinctId }: { distinctId: string }): JSX.Element {
+// Resolves a person from its distinct id through the shared batched loader, so a query that lists
+// distinct ids does not have to carry person properties. Falls back to the distinct id alone while
+// the batch is in flight and when the person cannot be found.
+function useLazyPerson(distinctId: string): PersonData {
const { personsCache, currentTeamId } = useValues(llmPersonsLazyLoaderLogic)
const { ensurePersonLoaded } = useActions(llmPersonsLazyLoaderLogic)
@@ -185,30 +188,25 @@ export function LazyPersonColumnCell({ distinctId }: { distinctId: string }): JS
}
}, [currentTeamId, cached, distinctId, ensurePersonLoaded])
- const personData: PersonData = cached
- ? { distinct_id: cached.distinct_id, properties: cached.properties }
- : { distinct_id: distinctId }
+ return cached ? { distinct_id: cached.distinct_id, properties: cached.properties } : { distinct_id: distinctId }
+}
+
+export function LazyPersonColumnCell({ distinctId }: { distinctId: string }): JSX.Element {
+ const personData = useLazyPerson(distinctId)
return <PersonColumnCell person={personData} />
}
-// Avatar only (no name) for inline use beside a title; a click still opens the
-// full person popover. Shares the lazy person loader with LazyPersonColumnCell.
-export function LazyPersonAvatar({ distinctId }: { distinctId: string }): JSX.Element {
- const { personsCache, currentTeamId } = useValues(llmPersonsLazyLoaderLogic)
- const { ensurePersonLoaded } = useActions(llmPersonsLazyLoaderLogic)
-
- const cached = personsCache[distinctId]
+function LazyPersonColumnCellWithRedirect({ distinctId }: { distinctId: string }): JSX.Element {
+ const personData = useLazyPerson(distinctId)
- useEffect(() => {
- if (currentTeamId && cached === undefined) {
- ensurePersonLoaded(distinctId)
- }
- }, [currentTeamId, cached, distinctId, ensurePersonLoaded])
+ return <PersonColumnCellWithRedirect person={personData} />
+}
- const personData: PersonData = cached
- ? { distinct_id: cached.distinct_id, properties: cached.properties }
- : { distinct_id: distinctId }
+// Avatar only (no name) for inline use beside a title; a click still opens the
+// full person popover.
+export function LazyPersonAvatar({ distinctId }: { distinctId: string }): JSX.Element {
+ const personData = useLazyPerson(distinctId)
return (
<PersonDisplay person={personData}>
@@ -597,18 +595,8 @@ export const aiObservabilityColumnRenderers: Record<string, QueryContextColumn>
__llm_person: {
title: 'Person',
render: ({ value }) => {
- // User data from HogQL query comes as a tuple [distinct_id, created_at, properties_json]
- if (Array.isArray(value) && value.length >= 3) {
- const [distinctId, , propertiesJson] = value
- let properties: Record<string, unknown> = {}
-
- try {
- properties = typeof propertiesJson === 'string' ? JSON.parse(propertiesJson) : {}
- } catch {
- // Ignore parsing errors
- }
-
- return <PersonColumnCellWithRedirect person={{ distinct_id: distinctId, properties }} />
+ if (typeof value === 'string' && value) {
+ return <LazyPersonColumnCellWithRedirect distinctId={value} />
}
return <PersonColumnCellWithRedirect person={null} />
diff --git a/products/ai_observability/frontend/tabs/aiObservabilityUsersLogic.ts b/products/ai_observability/frontend/tabs/aiObservabilityUsersLogic.ts
index 09ebc6f9f01f..8dc27d6aaec4 100644
--- a/products/ai_observability/frontend/tabs/aiObservabilityUsersLogic.ts
+++ b/products/ai_observability/frontend/tabs/aiObservabilityUsersLogic.ts
@@ -100,7 +100,7 @@ export const aiObservabilityUsersLogic = kea<aiObservabilityUsersLogicType>([
kind: NodeKind.HogQLQuery,
query: `
SELECT
- argMax(user_tuple, timestamp) as __llm_person,
+ distinct_id as __llm_person,
countDistinctIf(ai_trace_id, notEmpty(ai_trace_id)) as traces,
count() as generations,
countIf(notEmpty(ai_error) OR ai_is_error = 'true') as errors,
@@ -114,12 +114,7 @@ export const aiObservabilityUsersLogic = kea<aiObservabilityUsersLogicType>([
properties.$ai_trace_id as ai_trace_id,
properties.$ai_total_cost_usd as ai_total_cost_usd,
properties.$ai_error as ai_error,
- properties.$ai_is_error as ai_is_error,
- tuple(
- distinct_id,
- person.created_at,
- person.properties
- ) as user_tuple
+ properties.$ai_is_error as ai_is_error
FROM events
WHERE event = '$ai_generation' AND {filters}
)