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.

feat(customer-analytics): expose accounts as a hogql system table (#59680)

Arthur Moreira de Deusc1cedcd62faa
diff --git a/posthog/hogql/database/schema/system.py b/posthog/hogql/database/schema/system.py
index b863688b32d1..e4f479172ce9 100644
--- a/posthog/hogql/database/schema/system.py
+++ b/posthog/hogql/database/schema/system.py
@@ -115,6 +115,42 @@ cohort_calculation_history: PostgresTable = PostgresTable(
},
)
+accounts: PostgresTable = PostgresTable(
+ name="accounts",
+ postgres_table_name="customer_analytics_account",
+ access_scope="customer_analytics",
+ fields={
+ "id": UUIDDatabaseField(name="id"),
+ "team_id": IntegerDatabaseField(name="team_id"),
+ "external_id": StringDatabaseField(name="external_id", nullable=True),
+ "name": StringDatabaseField(name="name"),
+ "properties": StringJSONDatabaseField(name="properties"),
+ "stripe_customer_id": ExpressionField(
+ name="stripe_customer_id",
+ expr=parse_expr("JSONExtractString(properties, 'stripe_customer_id')"),
+ ),
+ "hubspot_deal_id": ExpressionField(
+ name="hubspot_deal_id",
+ expr=parse_expr("JSONExtractString(properties, 'hubspot_deal_id')"),
+ ),
+ "billing_id": ExpressionField(
+ name="billing_id",
+ expr=parse_expr("JSONExtractString(properties, 'billing_id')"),
+ ),
+ "sfdc_id": ExpressionField(
+ name="sfdc_id",
+ expr=parse_expr("JSONExtractString(properties, 'sfdc_id')"),
+ ),
+ "zendesk_id": ExpressionField(
+ name="zendesk_id",
+ expr=parse_expr("JSONExtractString(properties, 'zendesk_id')"),
+ ),
+ "created_by_id": IntegerDatabaseField(name="created_by_id", nullable=True),
+ "created_at": DateTimeDatabaseField(name="created_at"),
+ "updated_at": DateTimeDatabaseField(name="updated_at", nullable=True),
+ },
+)
+
cohorts: PostgresTable = PostgresTable(
name="cohorts",
postgres_table_name="posthog_cohort",
@@ -1085,6 +1121,7 @@ sandbox_environments: PostgresTable = PostgresTable(
class SystemTables(TableNode):
name: str = "system"
children: dict[str, TableNode] = {
+ "accounts": TableNode(name="accounts", table=accounts),
"activity_logs": TableNode(name="activity_logs", table=activity_logs),
"actions": TableNode(name="actions", table=actions),
"alerts": TableNode(name="alerts", table=alerts),
diff --git a/posthog/hogql/database/schema/test/test_system_tables.py b/posthog/hogql/database/schema/test/test_system_tables.py
index 23dad4ebb5c0..ff2ce9d5431d 100644
--- a/posthog/hogql/database/schema/test/test_system_tables.py
+++ b/posthog/hogql/database/schema/test/test_system_tables.py
@@ -32,6 +32,7 @@ from posthog.models.project import Project
from products.actions.backend.models.action import Action
from products.alerts.backend.models.alert import AlertConfiguration
from products.conversations.backend.models import Ticket
+from products.customer_analytics.backend.models.account import Account
from products.dashboards.backend.models.dashboard import Dashboard
from products.data_modeling.backend.models.data_modeling_job import DataModelingJob
from products.data_modeling.backend.models.datawarehouse_saved_query import DataWarehouseSavedQuery
@@ -127,6 +128,10 @@ def _create_activity_log(team: Team, label: str) -> ActivityLog:
return ActivityLog.objects.create(team_id=team.pk, activity="updated", scope="FeatureFlag", item_id=label)
+def _create_account(team: Team, label: str) -> Account:
+ return Account.objects.unscoped().create(team=team, name=f"account_{label}", external_id=f"ext_{label}")
+
+
def _create_action(team: Team, label: str) -> Action:
return Action.objects.create(team=team, name=f"action_{label}")
@@ -511,6 +516,7 @@ def _create_usage_metric(team: Team, label: str) -> GroupUsageMetric:
SYSTEM_TABLE_FACTORIES = [
+ ("accounts", _create_account),
("activity_logs", _create_activity_log),
("actions", _create_action),
("alerts", _create_alert),
diff --git a/posthog/hogql/database/test/__snapshots__/test_database.ambr b/posthog/hogql/database/test/__snapshots__/test_database.ambr
index a9cd534cffba..ca1da6e5e62f 100644
--- a/posthog/hogql/database/test/__snapshots__/test_database.ambr
+++ b/posthog/hogql/database/test/__snapshots__/test_database.ambr
@@ -2347,6 +2347,124 @@
"row_count": null,
"type": "system"
},
+ "system.accounts": {
+ "fields": {
+ "external_id": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "external_id",
+ "id": null,
+ "name": "external_id",
+ "schema_valid": true,
+ "table": null,
+ "type": "string"
+ },
+ "name": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "name",
+ "id": null,
+ "name": "name",
+ "schema_valid": true,
+ "table": null,
+ "type": "string"
+ },
+ "properties": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "properties",
+ "id": null,
+ "name": "properties",
+ "schema_valid": true,
+ "table": null,
+ "type": "json"
+ },
+ "stripe_customer_id": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "stripe_customer_id",
+ "id": null,
+ "name": "stripe_customer_id",
+ "schema_valid": true,
+ "table": null,
+ "type": "string"
+ },
+ "hubspot_deal_id": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "hubspot_deal_id",
+ "id": null,
+ "name": "hubspot_deal_id",
+ "schema_valid": true,
+ "table": null,
+ "type": "string"
+ },
+ "billing_id": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "billing_id",
+ "id": null,
+ "name": "billing_id",
+ "schema_valid": true,
+ "table": null,
+ "type": "string"
+ },
+ "sfdc_id": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "sfdc_id",
+ "id": null,
+ "name": "sfdc_id",
+ "schema_valid": true,
+ "table": null,
+ "type": "string"
+ },
+ "zendesk_id": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "zendesk_id",
+ "id": null,
+ "name": "zendesk_id",
+ "schema_valid": true,
+ "table": null,
+ "type": "string"
+ },
+ "created_by_id": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "created_by_id",
+ "id": null,
+ "name": "created_by_id",
+ "schema_valid": true,
+ "table": null,
+ "type": "integer"
+ },
+ "created_at": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "created_at",
+ "id": null,
+ "name": "created_at",
+ "schema_valid": true,
+ "table": null,
+ "type": "datetime"
+ },
+ "updated_at": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "updated_at",
+ "id": null,
+ "name": "updated_at",
+ "schema_valid": true,
+ "table": null,
+ "type": "datetime"
+ }
+ },
+ "id": "system.accounts",
+ "name": "system.accounts",
+ "row_count": null,
+ "type": "system"
+ },
"system.activity_logs": {
"fields": {
"id": {
@@ -9620,6 +9738,124 @@
"row_count": null,
"type": "system"
},
+ "system.accounts": {
+ "fields": {
+ "external_id": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "external_id",
+ "id": null,
+ "name": "external_id",
+ "schema_valid": true,
+ "table": null,
+ "type": "string"
+ },
+ "name": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "name",
+ "id": null,
+ "name": "name",
+ "schema_valid": true,
+ "table": null,
+ "type": "string"
+ },
+ "properties": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "properties",
+ "id": null,
+ "name": "properties",
+ "schema_valid": true,
+ "table": null,
+ "type": "json"
+ },
+ "stripe_customer_id": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "stripe_customer_id",
+ "id": null,
+ "name": "stripe_customer_id",
+ "schema_valid": true,
+ "table": null,
+ "type": "string"
+ },
+ "hubspot_deal_id": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "hubspot_deal_id",
+ "id": null,
+ "name": "hubspot_deal_id",
+ "schema_valid": true,
+ "table": null,
+ "type": "string"
+ },
+ "billing_id": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "billing_id",
+ "id": null,
+ "name": "billing_id",
+ "schema_valid": true,
+ "table": null,
+ "type": "string"
+ },
+ "sfdc_id": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "sfdc_id",
+ "id": null,
+ "name": "sfdc_id",
+ "schema_valid": true,
+ "table": null,
+ "type": "string"
+ },
+ "zendesk_id": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "zendesk_id",
+ "id": null,
+ "name": "zendesk_id",
+ "schema_valid": true,
+ "table": null,
+ "type": "string"
+ },
+ "created_by_id": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "created_by_id",
+ "id": null,
+ "name": "created_by_id",
+ "schema_valid": true,
+ "table": null,
+ "type": "integer"
+ },
+ "created_at": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "created_at",
+ "id": null,
+ "name": "created_at",
+ "schema_valid": true,
+ "table": null,
+ "type": "datetime"
+ },
+ "updated_at": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "updated_at",
+ "id": null,
+ "name": "updated_at",
+ "schema_valid": true,
+ "table": null,
+ "type": "datetime"
+ }
+ },
+ "id": "system.accounts",
+ "name": "system.accounts",
+ "row_count": null,
+ "type": "system"
+ },
"system.activity_logs": {
"fields": {
"id": {
diff --git a/products/data_warehouse/backend/test/__snapshots__/test_hogql_fixer_ai.ambr b/products/data_warehouse/backend/test/__snapshots__/test_hogql_fixer_ai.ambr
index d9b2b67319cc..99e13384864d 100644
--- a/products/data_warehouse/backend/test/__snapshots__/test_hogql_fixer_ai.ambr
+++ b/products/data_warehouse/backend/test/__snapshots__/test_hogql_fixer_ai.ambr
@@ -110,7 +110,7 @@
This is a list of all the available tables in the database:
```
- ['events', 'groups', 'persons', 'sessions', 'logs', 'query_log', 'system.activity_logs', 'system.actions', 'system.alerts', 'system.annotations', 'system.batch_export_backfills', 'system.batch_exports', 'system.cohort_calculation_history', 'system.cohorts', 'system.dashboards', 'system.data_modeling_jobs', 'system.data_modeling_views', 'system.data_modeling_endpoint_versions', 'system.data_modeling_endpoints', 'system.data_warehouse_sources', 'system.data_warehouse_tables', 'system.error_tracking_assignment_rules', 'system.error_tracking_issue_assignments', 'system.error_tracking_issue_fingerprints', 'system.error_tracking_issues', 'system.error_tracking_releases', 'system.error_tracking_symbol_sets', 'system.error_tracking_suppression_rules', 'system.early_access_features', 'system.experiments', 'system.exports', 'system.feature_flags', 'system.groups', 'system.group_type_mappings', 'system.hog_flows', 'system.hog_functions', 'system.ingestion_warnings', 'system.integrations', 'system.integration_repository_cache', 'system.insight_variables', 'system.logs_alerts', 'system.logs_views', 'system.insights', 'system.notebooks', 'system.sandbox_environments', 'system.review_queue_items', 'system.review_queues', 'system.score_definitions', 'system.session_recording_playlists', 'system.session_recordings', 'system.source_schemas', 'system.source_sync_jobs', 'system.support_tickets', 'system.surveys', 'system.task_runs', 'system.tasks', 'system.teams', 'system.trace_review_scores', 'system.trace_reviews', 'system.usage_metrics']
+ ['events', 'groups', 'persons', 'sessions', 'logs', 'query_log', 'system.accounts', 'system.activity_logs', 'system.actions', 'system.alerts', 'system.annotations', 'system.batch_export_backfills', 'system.batch_exports', 'system.cohort_calculation_history', 'system.cohorts', 'system.dashboards', 'system.data_modeling_jobs', 'system.data_modeling_views', 'system.data_modeling_endpoint_versions', 'system.data_modeling_endpoints', 'system.data_warehouse_sources', 'system.data_warehouse_tables', 'system.error_tracking_assignment_rules', 'system.error_tracking_issue_assignments', 'system.error_tracking_issue_fingerprints', 'system.error_tracking_issues', 'system.error_tracking_releases', 'system.error_tracking_symbol_sets', 'system.error_tracking_suppression_rules', 'system.early_access_features', 'system.experiments', 'system.exports', 'system.feature_flags', 'system.groups', 'system.group_type_mappings', 'system.hog_flows', 'system.hog_functions', 'system.ingestion_warnings', 'system.integrations', 'system.integration_repository_cache', 'system.insight_variables', 'system.logs_alerts', 'system.logs_views', 'system.insights', 'system.notebooks', 'system.sandbox_environments', 'system.review_queue_items', 'system.review_queues', 'system.score_definitions', 'system.session_recording_playlists', 'system.session_recordings', 'system.source_schemas', 'system.source_sync_jobs', 'system.support_tickets', 'system.surveys', 'system.task_runs', 'system.tasks', 'system.teams', 'system.trace_review_scores', 'system.trace_reviews', 'system.usage_metrics']
```
`person` or `event` metadata unspecified above (emails, names, etc.) is stored in `properties` fields, accessed like: `properties.foo.bar`.