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): label out-of-memory query failures correctly (#103921)

posthog[bot]de357bbeb9c3
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index e4ec6590284c..466236cd56d5 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -11563,9 +11563,9 @@ snapshots:
scenes-app-insights-error-empty-states--long-loading--light:
hash: v1.k794b7964.48b72670610c929db07e8089e69542edb05c7d14899af99a19224a2f631ec1dd.b0wSwuaUsbUp_pzSJHzwHfT0kJc0OGSWeIlA8kIeFAA
scenes-app-insights-error-empty-states--memory-limit-exceeded--dark:
- hash: v1.k794b7964.59d9ca86f867ffb27ccbf34bb20682d9b86ddf30bec7ac7522bd36f7fc464ee2.mj0agJN8aJFanRI0BEpzuzKwB0Zl8b2Z2QOC66OvdCA
+ hash: v1.k794b7964.26a0dc7b45ad88edc2589530f5c23b69e655dfae7ee7f183a4917eb477fe7f3f.MPiNmuZ18vH_SaeR9rdkn_PajmU0obu4f-t_lbx2iYc
scenes-app-insights-error-empty-states--memory-limit-exceeded--light:
- hash: v1.k794b7964.a8dadffcf1dce63a7a0bbd85e81b9f72b0b5470ee994a71f77ceb91070052b1f.e7CthrFsnkN4A_ICLzuEi1etMJgPbKkbcPKXxhFcLCU
+ hash: v1.k794b7964.1c7341a98c75aa0ea76c2847579f1079f31c569abcb13623a4566a1470df5cea.tEzkZCsd_HtTAm66SP4fCrWX5xzOoFZ1dNlz9Ybz7Ns
scenes-app-insights-error-empty-states--query-server-error--dark:
hash: v1.k794b7964.8e86d1d4ab6af6159804e2801298842f1d086cbbb796c91fb60de816ab1abf5f.H93T__KkhRkfdLXG-mls00Kg7VOvi1uPJRzKchhICco
scenes-app-insights-error-empty-states--query-server-error--light:
diff --git a/frontend/src/scenes/dashboard/dashboardUtils.test.ts b/frontend/src/scenes/dashboard/dashboardUtils.test.ts
index 79ed3c582bc9..46689593ad8a 100644
--- a/frontend/src/scenes/dashboard/dashboardUtils.test.ts
+++ b/frontend/src/scenes/dashboard/dashboardUtils.test.ts
@@ -22,6 +22,7 @@ import {
dashboardToSaveableTemplate,
searchParamsWithUrlFilters,
getDashboardTileDisplayName,
+ getInsightQueryError,
getInsightWithRetry,
isWidgetTileVisibleOnPlacement,
parseURLFilters,
@@ -478,3 +479,40 @@ describe('shouldSharedDashboardAutoForceForStaleTime', () => {
})
})
})
+
+describe('getInsightQueryError', () => {
+ it.each([
+ ['clickhouse_memory_limit_exceeded', 513],
+ ['invalid_query', 400],
+ ])('maps error code %s to status %s', (errorCode, expectedStatus) => {
+ const error = getInsightQueryError({
+ query_status: {
+ id: 'query-id',
+ error: true,
+ error_message: 'Query ran out of memory',
+ error_code: errorCode,
+ },
+ } as unknown as InsightModel)
+
+ expect(error?.status).toBe(expectedStatus)
+ expect(error?.data?.code).toBe(errorCode)
+ })
+
+ it('reads the memory code out of the error message when the status field is absent', () => {
+ const error = getInsightQueryError({
+ query_status: {
+ id: 'query-id',
+ error: true,
+ error_message:
+ "[ErrorDetail(string='Query ran out of memory', code='clickhouse_memory_limit_exceeded')]",
+ },
+ } as unknown as InsightModel)
+
+ expect(error?.status).toBe(513)
+ expect(error?.data?.code).toBe('clickhouse_memory_limit_exceeded')
+ })
+
+ it('returns null when the query did not error', () => {
+ expect(getInsightQueryError({} as InsightModel)).toBeNull()
+ })
+})
diff --git a/frontend/src/scenes/dashboard/dashboardUtils.ts b/frontend/src/scenes/dashboard/dashboardUtils.ts
index e521205c8431..a64c3d01008a 100644
--- a/frontend/src/scenes/dashboard/dashboardUtils.ts
+++ b/frontend/src/scenes/dashboard/dashboardUtils.ts
@@ -5,7 +5,7 @@ import { lemonToast } from '@posthog/lemon-ui'
import { getDashboardWidgetCatalogEntry } from '@posthog/products-dashboards/frontend/widget_types/catalog'
import api, { ApiMethodOptions, getJSONOrNull } from 'lib/api'
-import { ApiError } from 'lib/api-error'
+import { ApiError, CLICKHOUSE_MEMORY_LIMIT_ERROR_CODE } from 'lib/api-error'
import type { Dayjs } from 'lib/dayjs'
import { currentSessionId } from 'lib/internalMetrics'
import posthog from 'lib/posthog-typed'
@@ -41,9 +41,14 @@ export function getInsightQueryError(insight: InsightModel): ApiError | null {
}
const parsedError = parseErrorMessage(queryStatus.error_message ?? undefined)
- return new ApiError(undefined, 400, undefined, {
+ const code = queryStatus.error_code ?? parsedError.code
+ // A tile failure arrives without the HTTP status the query originally failed with, so the
+ // status is rebuilt from the code. Out of memory must stay 513: as a 400 the tile tells the
+ // user their query definition is broken, which sends them to fix a query that was never wrong.
+ const status = code === CLICKHOUSE_MEMORY_LIMIT_ERROR_CODE ? 513 : 400
+ return new ApiError(undefined, status, undefined, {
detail: parsedError.message,
- code: queryStatus.error_code ?? parsedError.code,
+ code,
queryId: queryStatus.id,
})
}
diff --git a/frontend/src/scenes/insights/EmptyStates/EmptyStates.tsx b/frontend/src/scenes/insights/EmptyStates/EmptyStates.tsx
index 3d64bccc7cfd..10ddde26d8f6 100644
--- a/frontend/src/scenes/insights/EmptyStates/EmptyStates.tsx
+++ b/frontend/src/scenes/insights/EmptyStates/EmptyStates.tsx
@@ -608,10 +608,14 @@ export function InsightValidationError({
}): JSX.Element {
const { openSidePanel } = useActions(sidePanelStateLogic)
const debugWithAI = (): void => openSidePanel(SidePanelTab.Max, MEMORY_LIMIT_AI_PROMPT)
- const isMemoryLimitError = validationErrorCode === CLICKHOUSE_MEMORY_LIMIT_ERROR_CODE
+ // Everything that reaches this panel is a validation status, so the kind stays invalid_query
+ // unless the code says otherwise: a memory failure is not a broken query definition.
+ const errorKind = getInsightErrorKind(400, validationErrorCode)
+ const isMemoryLimitError = errorKind === 'memory_limit'
const displayDetail = getInsightValidationDetail(detail)
const showQueryDebuggerInstruction =
query &&
+ !isMemoryLimitError &&
displayDetail !== 'Check the query for errors, then run it again.' &&
placement !== DashboardPlacement.Export
const shouldExcludeActions = excludeActions || placement === DashboardPlacement.Export
@@ -632,12 +636,10 @@ export function InsightValidationError({
data-attr="insight-empty-state"
className="flex flex-col items-center justify-center gap-2 rounded px-4 py-6 h-full w-full text-center text-balance"
>
- <InsightErrorHoggie kind="invalid_query" />
+ <InsightErrorHoggie kind={errorKind} />
<h2 data-attr="insight-loading-too-long" className="text-xl leading-tight font-bold mb-0 text-danger">
- We couldn't run this query
- {/* Note that this phrasing above signals the issue is not intermittent, */}
- {/* but rather that it's something with the definition of the query itself */}
+ {getInsightErrorTitle(errorKind, null)}
</h2>
<p className="text-sm text-muted max-w-120 mb-2">{renderDetailWithLinks(displayDetail)}</p>
@@ -732,7 +734,11 @@ function InsightErrorHoggie({ kind }: { kind: InsightErrorKind }): JSX.Element {
return <Hoggie className="w-24 h-24 mb-2" />
}
-function getInsightErrorKind(status?: number | null): InsightErrorKind {
+function getInsightErrorKind(status?: number | null, code?: string | null): InsightErrorKind {
+ // The code is the surer signal: a tile failure carries it even when the status was rebuilt.
+ if (code === CLICKHOUSE_MEMORY_LIMIT_ERROR_CODE) {
+ return 'memory_limit'
+ }
if (status === 429) {
return 'rate_limit'
}
diff --git a/frontend/src/scenes/insights/EmptyStates/InsightErrorStates.test.tsx b/frontend/src/scenes/insights/EmptyStates/InsightErrorStates.test.tsx
index b274c2bf940f..bacd3251ffab 100644
--- a/frontend/src/scenes/insights/EmptyStates/InsightErrorStates.test.tsx
+++ b/frontend/src/scenes/insights/EmptyStates/InsightErrorStates.test.tsx
@@ -64,6 +64,22 @@ describe('insight error states', () => {
})
})
+ it('labels a memory failure as unfinished rather than invalid', () => {
+ render(
+ <InsightValidationError
+ detail={CLUSTER_MEMORY_DETAIL}
+ validationErrorCode="clickhouse_memory_limit_exceeded"
+ query={{ kind: 'InsightVizNode' }}
+ />
+ )
+
+ expect(screen.getByText("This query couldn't finish")).toBeTruthy()
+ expect(screen.queryByText("We couldn't run this query")).toBeNull()
+ expect(screen.getByText(CLUSTER_MEMORY_DETAIL)).toBeTruthy()
+ expect(screen.queryByText('Open the query debugger and correct the query.')).toBeNull()
+ expect(screen.getByText('Debug with PostHog AI')).toBeTruthy()
+ })
+
it('replaces generic invalid-query detail with a next step', () => {
render(
<InsightValidationError