: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
chore(skills): always convert JSONExtract to property access in query-perf skill (#62428)
Robbiec031bb1378dc
diff --git a/.agents/skills/optimizing-clickhouse-and-hogql-queries/SKILL.md b/.agents/skills/optimizing-clickhouse-and-hogql-queries/SKILL.mdindex ac04396d14ce..71ae3e26c76f 100644--- a/.agents/skills/optimizing-clickhouse-and-hogql-queries/SKILL.md+++ b/.agents/skills/optimizing-clickhouse-and-hogql-queries/SKILL.md@@ -130,9 +130,21 @@ Worth a mention specific to PostHog: per [`CLAUDE.md`](../../../CLAUDE.md), new### JSON operations on properties-Any `JSONExtractString(properties, ...)`, `JSONExtractFloat(properties, ...)`, `JSONHas(properties, ...)`, or similar against the raw `properties` / `person_properties` / `group_properties` column is a huge smell. It means ClickHouse has to parse the JSON blob at query time for every row it reads.+Any `JSONExtractString(properties, ...)`, `JSONExtractFloat(properties, ...)`, `JSONHas(properties, ...)`, or similar against the raw `properties` / `person_properties` / `group_properties` column is a huge smell. It means ClickHouse has to parse the JSON blob at query time for every row it reads. This holds for both event and person property blobs: reading either as raw JSON can be up to ~100x slower than reading a directly materialized (`mat_*` / `dmat_*`) column, and ~10x slower than a property group read.-We have three materialization strategies. Skim:+**For any query that goes through the HogQL printer, the fix is mechanical and unconditional: replace every hand-written `JSONExtract*(properties, 'X')` with HogQL property access `properties.X` (wrap in `toFloat(...)` / `toInt(...)` when you need a non-string type). Convert _all_ of them — do not stop to work out which properties are materialized.** The printer path is most HogQL: backend `parse_select` / `execute_hogql_query` / `*QueryRunner` queries, and frontend `api.queryHogQL` / `` hogql`...` `` strings (they POST to `/query`, which runs the same printer). When the printer visits `properties.X` it does the materialization lookup against the live ClickHouse and emits the best available form — a directly materialized column, a property group read, a DMAT slot, or a `JSONExtract` fallback when nothing is materialized. So `properties.X` is **never worse** than the hand-written `JSONExtract`: worst case the printer emits the same `JSONExtract`; best case you get the materialized fast path, both now and automatically in the future when the column later gets materialized.++**Do not try to determine which properties are materialized and convert only those.** Reading migration files, the materialized-columns registry, or a `DESCRIBE` to decide which `JSONExtract`s are "safe" to convert is the printer's job reimplemented by hand, and it reaches the wrong answer:++- Materialization is frequently not created by a migration at all, so scanning migrations misses most of it.+- Property groups cover properties that have no dedicated materialized column.+- The materialized set differs per environment and changes over time, so any answer you compute is a snapshot that goes stale.++Converting only the subset you could confirm leaves the query inconsistent (some properties as `properties.X`, sibling properties left as `JSONExtract`, sometimes forced into a `hogql.raw()` conditional to keep one of each) for zero benefit, and silently skips every property you couldn't find evidence for. Convert all of them and let the printer decide at print time.++**The one exception is raw SQL that never goes through the printer** — multi-team `sync_execute` queries, ClickHouse migrations, and temporal activities that build query strings by hand (see Step 0). There is no printer to do the lookup (and `properties.X` HogQL syntax isn't available), so those queries _do_ have to reference the materialized column directly. That hand-rolled lookup is correct there, and only there.++For the raw-SQL exception, and as background, we have three materialization strategies. You do **not** need to consult these to convert a printer-path query:- Directly materialized columns: [`posthog/clickhouse/materialized_columns.py`](../../../posthog/clickhouse/materialized_columns.py)- Property groups: [`posthog/clickhouse/property_groups.py`](../../../posthog/clickhouse/property_groups.py)@@ -140,9 +152,9 @@ We have three materialization strategies. Skim:We are also experimenting with the new ClickHouse JSON data type. Check recent migrations under [`posthog/clickhouse/migrations/`](../../../posthog/clickhouse/migrations/) for the current state.-If a property is not materialized in the local fixtures, snapshot tests will fall back to `JSONExtract*`. In test code, wrap the block in the `materialized()` context manager from [`posthog/test/base.py`](../../../posthog/test/base.py) (search for `def materialized`) to materialize a property for the duration of the test. It supports `create_minmax_index`, `create_bloom_filter_index`, and the lower-case variants when you also want to assert the skip index is used.+In test code, if you need a property materialized for the duration of a test (e.g. to assert the printer emits a column read or that a skip index is used), wrap the block in the `materialized()` context manager from [`posthog/test/base.py`](../../../posthog/test/base.py) (search for `def materialized`). It supports `create_minmax_index`, `create_bloom_filter_index`, and the lower-case variants.-**Important: `JSONExtract` in a test-extracted query is a noisy signal.** The HogQL printer's materialization lookup runs against whatever ClickHouse you're talking to. The test ClickHouse usually has a minimal materialized set (events `$browser`, `$os`, a few others), so the printer falls back to `JSONExtract(properties, ...)` and the `.ambr` snapshot bakes that in. Production has dozens of materialized properties per team and the printer emits direct column reads. Before chasing a JSON smell you saw in a snapshot, confirm it's actually `JSONExtract`-ing in production: pull the same query type from `system.query_log` (via `/query-clickhouse-via-metabase`) and see what the printer actually emitted. If prod is already on `pmat_X` and the snapshot just shows `JSONExtract`, the smell is a test-environment artifact, not a real performance problem.+A `JSONExtract` you see in a `.ambr` snapshot or other printed SQL — where the _source_ query already uses `properties.X` — is just the printer's fallback because the test fixture lacks the materialized column prod has. There is nothing to change in the source, and it is not evidence of a production problem: prod may well emit a materialized column read for the same query. Don't "fix" the snapshot, and don't treat it as a perf bug.### Primary key and skip indexesdiff --git a/.agents/skills/optimizing-clickhouse-and-hogql-queries/references/investigation-playbook.md b/.agents/skills/optimizing-clickhouse-and-hogql-queries/references/investigation-playbook.mdindex 0888950ad5da..8618b4d8c7c7 100644--- a/.agents/skills/optimizing-clickhouse-and-hogql-queries/references/investigation-playbook.md+++ b/.agents/skills/optimizing-clickhouse-and-hogql-queries/references/investigation-playbook.md@@ -63,10 +63,17 @@ diagnosis, and stay open to a cause that is not listed here.1. **Unmaterialized property access (JSONExtract).** The number-one cause of extreme byte reads.`JSONExtract*(events.properties, …)` or `JSONExtract*(person_properties, …)` forces a read of the- full JSON blob per matching row. Confirm by searching the query text for `JSONExtract` / `JSONHas`;- then check whether the property has a `mat_<property>` column. Fix: materialize it (see the- JSON-operations smell in [`SKILL.md`](../SKILL.md) and the migration layer in its Step 5) or drop the- property filter. Person properties are the worst because the blobs are large.+ full JSON blob per matching row. Confirm by searching the query text for `JSONExtract` / `JSONHas`.+ The fix depends on where the `JSONExtract` came from. If it's **hand-written in a printer-path query**+ (HogQL source that literally says `JSONExtractString(properties, 'X')`), the fix is to replace it with+ property access `properties.X` for every such property and let the printer materialize — the column may+ already be materialized in prod, so no migration is needed (see the JSON-operations smell in+ [`SKILL.md`](../SKILL.md)). If the **source already uses `properties.X`** (or it's raw SQL that bypasses+ the printer) and prod still emits `JSONExtract`, the property genuinely isn't materialized — then the+ fix is to materialize it (the migration layer in `SKILL.md` Step 5) or drop the property filter. This+ applies to both event and person property blobs: reading either as raw JSON can be up to ~100x slower+ than reading a directly materialized (`mat_*` / `dmat_*`) column, and ~10x slower than a property group+ read.2. **Session joins.** Joining `raw_sessions` / `sharded_sessions` (for `$session_duration` etc.) adds afull sessions scan. Look for `raw_sessions` in the text.3. **High-cardinality breakdowns.** A `breakdown_value` on something like a URL or an ID explodes the