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(mcp): clarify sql validation and memory recovery (#114088)

Vasco de Krijger8b9c45f88d5f
diff --git a/products/posthog_ai/skills/querying-posthog-data/references/guidelines.md b/products/posthog_ai/skills/querying-posthog-data/references/guidelines.md
index 154ff9206814..6ba7cb951ff6 100644
--- a/products/posthog_ai/skills/querying-posthog-data/references/guidelines.md
+++ b/products/posthog_ai/skills/querying-posthog-data/references/guidelines.md
@@ -147,7 +147,7 @@ FROM system.information_schema.columns
WHERE table_name = 'events'
```
-This works for `system.*` entity tables too — query them by full name, e.g. `WHERE table_name = 'system.insights'`. Their column sets differ per entity, so confirm columns before projecting them.
+This works for `system.*` entity tables too — query them by full name, e.g. `WHERE table_name = 'system.insights'`. Their column sets differ per entity and from REST/tool response fields, so confirm columns before projecting them. If a required field is absent, use a tool that exposes it or explain the limitation; do not remove the filter or substitute a different field just to make the query succeed.
**Discover how a table joins to others:**
@@ -221,13 +221,30 @@ This prevents wasted API calls and gives users immediate feedback when the data
##### Progressive exploration
-For unfamiliar or potentially large datasets, probe cheaply before running the expensive aggregation. Widen only if the cheap step looks reasonable:
+For unfamiliar or potentially large datasets, use a short timestamp window to check the data and intended query shape before widening it:
1. **Count first** — `SELECT count() FROM events WHERE timestamp >= now() - INTERVAL 1 DAY AND event = 'foo'`. Confirms the data exists and gives a sense of volume.
2. **Small sample** — inspect a handful of rows (`LIMIT 10`) to verify property shapes and values match expectations.
-3. **Full query** — run the real aggregation with a time range and `LIMIT`, having confirmed it won't scan needlessly or return empty.
+3. **Aggregation**: test the intended aggregation over that short window before widening it. Keep `LIMIT` to bound the output.
-This is faster than discovering an empty result or a mis-shaped property after the full aggregation, and it costs less.
+Counts and samples check volume and property shapes; they do not prove a join or aggregation will fit in memory.
+`LIMIT` bounds returned rows. Joins and aggregations can still process the full input before applying it.
+
+##### Recovering from memory failures
+
+Follow the error's advice: wait and retry once when it reports temporary server capacity.
+The `memory_limit` category alone cannot distinguish server pressure from a per-query limit.
+
+If the error says the query itself ran out of memory, shorten the time range in `WHERE` and test one adjusted query before running more windows.
+Avoid parallel retries of the same failing query shape. Even a query filtered by time or person can be too large.
+If the smaller window still fails, reduce join inputs or grouping cardinality only where that preserves the requested calculation.
+
+When splitting a requested range, use non-overlapping windows such as `timestamp >= start AND timestamp < end` with the same filters.
+Split only when each window can be computed independently without changing the calculation.
+Combine complete counts and sums by adding them, and compute averages from totals and counts.
+Do not sum distinct counts or combine per-window quantiles, funnels, or retention results as if they covered the full range.
+Rows cut off by `LIMIT` or truncation are incomplete.
+Track which windows succeeded and disclose missing windows or reduced scope; do not present partial coverage as the full answer.
##### Skipping index
@@ -364,6 +381,7 @@ Find the reference for [Sparkline, SemVer, Session replays, Actions, Translation
##### Other rules
+- Use explicit `AS` and descriptive column aliases: `min(timestamp) AS first_seen_at` instead of `min(timestamp) first`. Some short names, such as `fn`, are reserved even after `AS`; use a descriptive name or backtick-quote the alias and its references.
- WHERE clause must come after all JOINs
- No semicolons at end of queries
- `toStartOfWeek(timestamp, 1)` for Monday start (numeric, not string)