Writing clickhouse queries

From PostHog/posthog · View interactive version

Writing clickhouse queries is an AI agent skill from the GitHub repository PostHog/posthog. Guide for writing performant ClickHouse queries in PostHog product code. Use when writing HogQL query runners, adding or reviewing a ClickHouse call site (`sync_execute`, `execute_hogql_query`, a `QueryRunner`) and choosing its `Workload` (ONLINE, OFFLINE, LOGS, ENDPOINTS), designing a ClickHouse ta

How to use this skill

Skill file

Writing ClickHouse queries for new products

If you're optimizing an existing query rather than writing a new one, this is the wrong skill. Use CODE0 instead. That skill covers layer triage, smell scanning (FROM ... FINAL, JSONExtract over properties, missing skip indexes, self-joins, CTE blow-up), measurement on the Test Cluster, and applying the fix at the right layer.

Read CODE0 for the authoritative guide on writing new queries.

Then pull in whichever related docs the task touches:

When to use

For investigating an existing slow query, debugging a system.query_log row, or reviewing a proposed HogQL printer change for performance, use CODE1.

Not the right skill for: customer-facing ad-hoc HogQL via Max / posthog:execute-sql, use query-examples for that. For migration mechanics (node roles, engines, replication), use clickhouse-migrations.

Choose a workload on purpose

Every call site passes an explicit workload=, and the PR description says which one and why.

ONLINE is only for results a person waits for on screen.

Work that no person waits for (scheduled Celery tasks, most Temporal activities, Dagster assets, exports, backfills) is OFFLINE.

The workload is a tag of intent; the pod's CLICKHOUSE_HOST decides which replicas run the query.

Read CODE0 for the decision table, the overrides in sync_execute, and how to check where queries land.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.