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.

perf(ingestion): read only the feature keys from the team query (#127184)

Meikel Ratz6a8d68cc1433
diff --git a/nodejs/src/common/utils/team-manager.ts b/nodejs/src/common/utils/team-manager.ts
index a4885ca03fff..ce59e3ed1a90 100644
--- a/nodejs/src/common/utils/team-manager.ts
+++ b/nodejs/src/common/utils/team-manager.ts
@@ -7,18 +7,15 @@ import { logger } from './logger'
import { captureTeamEvent } from './posthog'
type RawTeam = Omit<Team, 'available_features'> & {
- available_product_features: unknown
+ available_product_feature_keys: unknown
}
-// One team's row must not throw, because a throw fails the batched lookup for every team in it. The
-// column is a jsonb array, so an entry can be null. An entry without a string key is skipped.
+// One team's row must not throw, because a throw fails the batched lookup for every team in it.
function availableFeatures(raw: unknown): OrganizationAvailableFeature[] {
if (!Array.isArray(raw)) {
return []
}
- return raw.flatMap((feature) =>
- typeof feature?.key === 'string' ? [feature.key as OrganizationAvailableFeature] : []
- )
+ return raw.flatMap((key) => (typeof key === 'string' ? [key as OrganizationAvailableFeature] : []))
}
// Postgres stores team ids in int4 columns. A query that compares one of those columns to a
@@ -192,7 +189,15 @@ export class TeamManager {
extract('epoch' from t.drop_events_older_than) as drop_events_older_than_seconds,
COALESCE(cfg.minimal_flag_called_events, false) AS minimal_flag_called_events,
COALESCE(ocfg.flag_evaluations_mode, 0) AS flag_evaluations_mode,
- o.available_product_features
+ -- The keys are the only part of available_product_features a caller reads. Picking
+ -- them out here returns a text[], which the pg driver reads with its plain array
+ -- parser. Selecting the jsonb[] column makes the driver JSON-parse every feature
+ -- object of every row, and an organization holds about a hundred of them.
+ ARRAY(
+ SELECT feature->>'key'
+ FROM unnest(o.available_product_features) AS feature
+ WHERE feature->>'key' IS NOT NULL
+ ) AS available_product_feature_keys
FROM posthog_team t
JOIN posthog_organization o ON o.id = t.organization_id
LEFT JOIN feature_flags_teamfeatureflagsconfig cfg ON cfg.team_id = t.id
@@ -211,7 +216,7 @@ export class TeamManager {
// Fill in actual teams where they exist
result.rows.forEach((row) => {
- const { available_product_features, ...teamPartial } = row
+ const { available_product_feature_keys, ...teamPartial } = row
const team: Team = {
...teamPartial,
// NOTE: The postgres lib loads the bigint as a string, so we need to cast it to a ProjectId
@@ -221,7 +226,7 @@ export class TeamManager {
teamPartial.drop_events_older_than_seconds !== null
? Number(teamPartial.drop_events_older_than_seconds)
: null,
- available_features: availableFeatures(available_product_features),
+ available_features: availableFeatures(available_product_feature_keys),
}
resultRecord[row.id] = team
resultRecord[row.api_token] = team