: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
fix(hogql): surface posthog.* data-plane tables in information_schema (#71423)
posthog[bot]47bc1c7ec355
diff --git a/posthog/hogql/database/schema/information_schema.py b/posthog/hogql/database/schema/information_schema.pyindex af3f04457056..cc35031938b2 100644--- a/posthog/hogql/database/schema/information_schema.py+++ b/posthog/hogql/database/schema/information_schema.py@@ -175,9 +175,16 @@ def _classify_table(name: str, table: Table, warehouse: set[str], views: set[strdef _visible_table_names(database: "Database") -> list[str]:- # `posthog.*` is an internal namespace that mostly duplicates the top-level tables — skip it- # to keep the catalog clean. Everything else (built-in, system, warehouse, views) is included.- return [n for n in database.tables.resolve_visible_table_names() if not n.startswith("posthog.")]+ # The `posthog.*` namespace holds two kinds of tables: copies of the top-level tables+ # (`posthog.events`, `posthog.persons`, …) and data-plane tables that live *only* there+ # (`posthog.ai_events`, `posthog.trace_spans`, `posthog.metrics`, the web pre-aggregated+ # tables, …). Hide the copies to keep the catalog clean, but keep the unique ones — they're+ # queryable by their `posthog.`-qualified name (a bare `FROM ai_events` errors), so leaving+ # them out makes them undiscoverable through the schema-discovery workflow. Everything else+ # (built-in, system, warehouse, views) is included.+ names = database.tables.resolve_visible_table_names()+ root_names = {n for n in names if "." not in n}+ return [n for n in names if not (n.startswith("posthog.") and n[len("posthog.") :] in root_names)]# Per-column statistics surfaced into information_schema.columns: (null_fraction, min_value, max_value).diff --git a/posthog/hogql/database/schema/test/test_information_schema.py b/posthog/hogql/database/schema/test/test_information_schema.pyindex f39466dce76c..3be627e4d3b7 100644--- a/posthog/hogql/database/schema/test/test_information_schema.py+++ b/posthog/hogql/database/schema/test/test_information_schema.py@@ -200,6 +200,36 @@ class TestInformationSchema(ClickhouseTestMixin, APIBaseTest):# information_schema is self-describingassert rows.get("system.information_schema.columns") == "information_schema"+ def test_posthog_namespaced_data_plane_tables_are_discoverable(self):+ # The `posthog.*` namespace holds data-plane tables (ai_events, trace_spans, metrics, …) that+ # exist ONLY there — a bare `FROM ai_events` errors, and the execute-sql prompt points agents at+ # their `posthog.`-qualified names. If the catalog drops them, they become undiscoverable through+ # the schema-discovery workflow and a healthy project looks like it has no such schema. Copies of+ # root tables (posthog.events) must stay hidden so the catalog isn't cluttered with duplicates.+ names = {+ row[0]+ for row in execute_hogql_query(+ "SELECT table_name FROM system.information_schema.tables", team=self.team+ ).results+ or []+ }+ assert {"posthog.ai_events", "posthog.trace_spans", "posthog.metrics"}.issubset(names)+ assert "events" in names+ assert "posthog.events" not in names++ def test_posthog_namespaced_table_columns_resolve(self):+ # Being listed isn't enough — an agent that discovers `posthog.trace_spans` must then be able to+ # inspect its columns. Guards the split between enumerating a dotted name and resolving it.+ columns = {+ row[0]+ for row in execute_hogql_query(+ "SELECT column_name FROM system.information_schema.columns WHERE table_name = 'posthog.trace_spans'",+ team=self.team,+ ).results+ or []+ }+ assert {"trace_id", "span_id"}.issubset(columns)+def test_access_scoped_system_tables_are_filtered(self):# Access-scoped system tables the caller can't reach must not leak into the catalog,# while unscoped ones remain visible — mirroring the SQL editor's access decision.diff --git a/services/mcp/src/templates/sections/schema-discovery.md b/services/mcp/src/templates/sections/schema-discovery.mdindex 1a4351cc9f23..6cbca2f3eb7c 100644--- a/services/mcp/src/templates/sections/schema-discovery.md+++ b/services/mcp/src/templates/sections/schema-discovery.md@@ -8,7 +8,7 @@- Discover joins / foreign keys: `SELECT source_table, source_column, target_table, target_column FROM system.information_schema.relationships WHERE source_table = 'events'`.- `description` carries the semantic description of a table, view, or column when one has been set (author-, source-, or AI-authored), including data-warehouse tables and views. Filter on it to find things by meaning, e.g. `WHERE description ILIKE '%revenue%'`.- **This covers `system.*` entity tables too** (`system.insights`, `system.dashboards`, `system.cohorts`, …) — query them by their full name, e.g. `WHERE table_name = 'system.insights'`. Their column sets differ per entity, so confirm columns before projecting them.+ **This covers `system.*` entity tables** (`system.insights`, `system.dashboards`, `system.cohorts`, …) **and `posthog.*` data-plane tables** (`posthog.ai_events`, `posthog.trace_spans`, `posthog.metrics`, …) — query them by their full, qualified name, e.g. `WHERE table_name = 'system.insights'` or `WHERE table_name = 'posthog.trace_spans'`. Core tables like `events` use their bare name (there is no `posthog.events` in the catalog). Column sets differ per table, so confirm columns before projecting them.2. **Event taxonomy** — call `read-data-schema` to verify events, properties, and property values. Do not rely on training data or PostHog defaults.3. **Write the SQL** only after steps 1 and 2 confirm the data exists, using the verified table and column names.