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.

chore(skills): flag single-team raw SQL as needing HogQL (#62195)

Robbie0ee14a1a7f53
diff --git a/.agents/skills/optimizing-clickhouse-and-hogql-queries/SKILL.md b/.agents/skills/optimizing-clickhouse-and-hogql-queries/SKILL.md
index 48eaa794ad42..428e4b114fb8 100644
--- a/.agents/skills/optimizing-clickhouse-and-hogql-queries/SKILL.md
+++ b/.agents/skills/optimizing-clickhouse-and-hogql-queries/SKILL.md
@@ -26,7 +26,14 @@ If the file you were pointed at is a coordinator, orchestrator, Celery task, Tem
**If the slow query turns out to be raw ClickHouse SQL embedded in production code** (Python f-strings, string SQL passed to `sync_execute`, `client.execute`, `client.read_query`, etc., not the output of the HogQL printer), flag this to the user up front, then continue with the optimization. HogQL queries get materialized-column substitution, property-group dispatch, lazy joins, team-id guards, and a pile of other optimizations automatically through the printer; raw SQL has to reimplement each of those or live without them. The structural fix is usually to express the query in HogQL and let the printer handle these consistently, but that's a larger change; walk through the rest of this skill normally so the user has both options (local fix now, HogQL move later, or both).
-For `INSERT` statements specifically, HogQL has no `INSERT` statement (intentional design choice), so the envelope has to be hand-built. The recommended pattern is to construct the `SELECT` in HogQL, print it, and concatenate it into `INSERT INTO <table> <printed_select>`. The read half still gets materialization, lazy joins, team-id guards, and everything else the printer does; only the `INSERT` wrapper is a hand-built string. ClickHouse migrations and one-shot operational scripts are reasonable exceptions where raw SQL is fine end-to-end.
+**Single-team vs multi-team is the deciding factor for whether raw SQL is even excusable.** `execute_hogql_query` is team-scoped — it always runs against exactly one team and injects the `team_id` guard for you. So:
+
+- **A raw query scoped to one team should almost always be HogQL.** If the SQL has (or should have) a `team_id = X` filter and reads a single team's data, there is no reason it's hand-written — it's leaving materialized-column substitution, lazy joins, and the team-id guard on the table. Treat raw single-team `sync_execute` as a smell in its own right and recommend the HogQL move, not just a local tweak. The clearest tell is a query that hand-rolls a materialized-column lookup (e.g. calling `get_materialized_column_for_property(...)` with a `JSONExtract` fallback) — that is the printer's job, reimplemented by hand because the query never goes through it.
+- **A query that legitimately spans multiple teams is exempt.** Cross-team / global jobs — periodic enrichment, billing rollups, "find every team where X" scans with no `team_id` filter or a `team_id IN (...)` over many teams — can't go through `execute_hogql_query`, since it scopes to a single team. These are a reasonable place for raw `sync_execute`. Don't push them toward HogQL; just optimize the raw SQL in place (materialized columns, sort-key prefix, etc.). When such a query does its own materialized-column lookup, that's expected, not a smell.
+
+So when you find raw SQL, first ask "is this one team or many?" One team → recommend HogQL. Many teams → keep it raw and optimize the SQL directly.
+
+**`INSERT` is not an escape hatch from HogQL — the single-team rule still applies to the read half.** HogQL has no `INSERT` statement (intentional design choice), so only the envelope has to be hand-built. The recommended pattern is to construct the `SELECT` in HogQL, print it, and concatenate it into `INSERT INTO <table> <printed_select>`. The read half still gets materialization, lazy joins, team-id guards, and everything else the printer does; only the `INSERT` wrapper is a hand-built string. So a single-team `INSERT ... SELECT` whose `SELECT` is a hand-written string (raw column lists, `JSONExtract` over `properties`, a manual `team_id` filter) is the same smell as any other single-team raw query: flag it, and recommend moving the `SELECT` half to a HogQL-printed query while keeping the `INSERT INTO <table>` wrapper raw. The fact that the surrounding statement is an `INSERT` does not make the read exempt. The only genuinely raw-SQL-fine cases are: `INSERT ... VALUES` of explicit rows assembled in Python (no `SELECT` to print), multi-team `INSERT ... SELECT` (same exemption as any multi-team query), ClickHouse migrations, and one-shot operational scripts.
## Background: read these once