: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
feat(hogql): Add new system tables for batch exports (#81866)
Tomás Farías Santanafdc98bb0cf99
diff --git a/posthog/hogql/database/schema/system.py b/posthog/hogql/database/schema/system.pyindex cbc20bcbfc7b..23b315280292 100644--- a/posthog/hogql/database/schema/system.py+++ b/posthog/hogql/database/schema/system.py@@ -161,6 +161,107 @@ batch_exports: PostgresTable = PostgresTable(},)+batch_export_on_demands: PostgresTable = PostgresTable(+ name="batch_export_on_demands",+ postgres_table_name="posthog_batchexportondemand",+ access_scope="batch_export",+ description="Batch exports triggered on demand rather than on a schedule; one row per on-demand export.",+ fields={+ "id": StringDatabaseField(name="id", description="On-demand batch export UUID."),+ "team_id": IntegerDatabaseField(name="team_id"),+ "destination_id": StringDatabaseField(+ name="destination_id", description="Identifier of the destination config the export writes to."+ ),+ "source_id": StringDatabaseField(+ name="source_id",+ nullable=True,+ description="Source of the data to export, if any; when set, takes precedence over model.",+ ),+ "model": StringDatabaseField(+ name="model",+ nullable=True,+ description="Data model exported, e.g. 'events', 'persons', 'sessions' or 'hogql'.",+ ),+ "filters": StringJSONDatabaseField(+ name="filters", nullable=True, description="Filters applied to the exported data."+ ),+ "_deleted": BooleanDatabaseField(name="deleted", hidden=True),+ "deleted": ExpressionField(name="deleted", expr=ast.Call(name="toInt", args=[ast.Field(chain=["_deleted"])])),+ "created_at": DateTimeDatabaseField(+ name="created_at", description="When the on-demand batch export was created."+ ),+ "last_updated_at": DateTimeDatabaseField(+ name="last_updated_at", description="When the on-demand batch export was last updated."+ ),+ },+)+++class _BatchExportRunsTable(PostgresTable, DANGEROUS_NoTeamIdCheckTable):+ predicates: list[Expr] = [+ parse_expr(+ "batch_export_id IN (SELECT id FROM system.batch_exports)"+ " OR batch_export_on_demand_id IN (SELECT id FROM system.batch_export_on_demands)"+ )+ ]+++batch_export_runs: _BatchExportRunsTable = _BatchExportRunsTable(+ name="batch_export_runs",+ postgres_table_name="posthog_batchexportrun",+ access_scope="batch_export",+ description="Batch export run history; one row per run.",+ fields={+ "id": StringDatabaseField(name="id", description="Run UUID."),+ "batch_export_id": StringDatabaseField(+ name="batch_export_id",+ nullable=True,+ description="Batch export this run belongs to; joins to batch_exports.id. May be NULL if the run corresponds to an on-demand batch export.",+ ),+ "batch_export_on_demand_id": StringDatabaseField(+ name="batch_export_on_demand_id",+ nullable=True,+ description="On-demand batch export this run belongs to; joins to batch_export_on_demands.id. May be NULL if the run corresponds to a regularly scheduled batch export.",+ ),+ "backfill_id": StringDatabaseField(+ name="backfill_id",+ nullable=True,+ description="Backfill this run belongs to, if any; joins to batch_export_backfills.id.",+ ),+ "data_interval_start": DateTimeDatabaseField(+ name="data_interval_start",+ nullable=True,+ description="Start of the time range covered by the run (NULL means no lower bound).",+ ),+ "data_interval_end": DateTimeDatabaseField(+ name="data_interval_end",+ nullable=False,+ description="End of the time range covered by the run",+ ),+ "status": StringDatabaseField(+ name="status", description="Run status, e.g. Running, Completed, Failed, Cancelled."+ ),+ "created_at": DateTimeDatabaseField(name="created_at", description="When the run was created."),+ "finished_at": DateTimeDatabaseField(+ name="finished_at", nullable=True, description="When the run finished; NULL while still running."+ ),+ "last_updated_at": DateTimeDatabaseField(+ name="last_updated_at", description="When the run row was last updated."+ ),+ "records_completed": IntegerDatabaseField(+ name="records_completed",+ nullable=True,+ description="Total number of records exported (NULL while still running or if the run failed).",+ ),+ "bytes_exported": IntegerDatabaseField(+ name="bytes_exported",+ nullable=True,+ description="Total number of bytes exported (NULL while still running or if the run failed).",+ ),+ },+)++alerts: PostgresTable = PostgresTable(name="alerts",postgres_table_name="posthog_alertconfiguration",@@ -2635,6 +2736,8 @@ class SystemTables(TableNode):"alerts": TableNode(name="alerts", table=alerts),"annotations": TableNode(name="annotations", table=annotations),"batch_export_backfills": TableNode(name="batch_export_backfills", table=batch_export_backfills),+ "batch_export_on_demands": TableNode(name="batch_export_on_demands", table=batch_export_on_demands),+ "batch_export_runs": TableNode(name="batch_export_runs", table=batch_export_runs),"batch_exports": TableNode(name="batch_exports", table=batch_exports),"business_knowledge_chunks": TableNode(name="business_knowledge_chunks", table=business_knowledge_chunks),"business_knowledge_documents": TableNode(diff --git a/posthog/hogql/database/schema/test/test_system_tables.py b/posthog/hogql/database/schema/test/test_system_tables.pyindex b124cf416134..ae9b6b7d1b69 100644--- a/posthog/hogql/database/schema/test/test_system_tables.py+++ b/posthog/hogql/database/schema/test/test_system_tables.py@@ -109,6 +109,9 @@ TEAM_ID_FILTER_PATTERNS = {"_account_tagged_items": "system__accounts.team_id",# The custom property tables declare their real team_id column, so the standard direct# guard applies (and is pushed into the federated read).+ # Runs have no team_id column; isolation is enforced via a predicate scoping through the+ # run's parent (scheduled or on-demand batch export)+ "batch_export_runs": "system__batch_exports.team_id",# Same shape, scoped through system.support_tickets instead"_ticket_tagged_items": "system__support_tickets.team_id","_ticket_assignments": "system__support_tickets.team_id",@@ -196,6 +199,33 @@ def _create_batch_export_backfill(team: Team, label: str):return BatchExportBackfill.objects.create(team=team, batch_export=batch_export, status="Running")+def _create_batch_export_run(team: Team, label: str):+ from products.batch_exports.backend.models.batch_export import BatchExport, BatchExportDestination, BatchExportRun++ destination = BatchExportDestination.objects.create(type="S3", config={})+ batch_export = BatchExport.objects.create(+ team=team, name=f"export_for_run_{label}", destination=destination, interval="hour"+ )+ return BatchExportRun.objects.create(batch_export=batch_export, status="Running", data_interval_end=timezone.now())+++def _create_batch_export_on_demand(team: Team, label: str):+ from products.batch_exports.backend.models.batch_export import BatchExportDestination, BatchExportOnDemand++ destination = BatchExportDestination.objects.create(type="S3", config={})+ with team_scope(team.pk):+ return BatchExportOnDemand.objects.create(team=team, destination=destination)+++def _create_batch_export_run_on_demand(team: Team, label: str):+ from products.batch_exports.backend.models.batch_export import BatchExportRun++ on_demand = _create_batch_export_on_demand(team, label)+ return BatchExportRun.objects.create(+ batch_export_on_demand=on_demand, status="Running", data_interval_end=timezone.now()+ )++def _create_alert(team: Team, label: str) -> AlertConfiguration:insight = Insight.objects.create(team=team, name=f"insight_for_alert_{label}")return AlertConfiguration.objects.create(team=team, insight=insight, name=f"alert_{label}")@@ -769,6 +799,10 @@ SYSTEM_TABLE_FACTORIES = [("alerts", _create_alert),("annotations", _create_annotation),("batch_export_backfills", _create_batch_export_backfill),+ ("batch_export_on_demands", _create_batch_export_on_demand),+ ("batch_export_runs", _create_batch_export_run),+ # A run parented by an on-demand export exercises the other branch of the runs table's scoping predicate+ ("batch_export_runs", _create_batch_export_run_on_demand),("batch_exports", _create_batch_export),("business_knowledge_chunks", _create_business_knowledge_chunk),("business_knowledge_documents", _create_business_knowledge_document),diff --git a/posthog/hogql/database/test/__snapshots__/test_database.ambr b/posthog/hogql/database/test/__snapshots__/test_database.ambrindex 8b06e40ce08a..45d497dd9df7 100644--- a/posthog/hogql/database/test/__snapshots__/test_database.ambr+++ b/posthog/hogql/database/test/__snapshots__/test_database.ambr@@ -3514,6 +3514,224 @@"row_count": null,"type": "system"},+ "system.batch_export_on_demands": {+ "certification": null,+ "fields": {+ "id": {+ "chain": null,+ "fields": null,+ "hogql_value": "id",+ "id": null,+ "name": "id",+ "schema_valid": true,+ "table": null,+ "type": "string"+ },+ "destination_id": {+ "chain": null,+ "fields": null,+ "hogql_value": "destination_id",+ "id": null,+ "name": "destination_id",+ "schema_valid": true,+ "table": null,+ "type": "string"+ },+ "source_id": {+ "chain": null,+ "fields": null,+ "hogql_value": "source_id",+ "id": null,+ "name": "source_id",+ "schema_valid": true,+ "table": null,+ "type": "string"+ },+ "model": {+ "chain": null,+ "fields": null,+ "hogql_value": "model",+ "id": null,+ "name": "model",+ "schema_valid": true,+ "table": null,+ "type": "string"+ },+ "filters": {+ "chain": null,+ "fields": null,+ "hogql_value": "filters",+ "id": null,+ "name": "filters",+ "schema_valid": true,+ "table": null,+ "type": "json"+ },+ "deleted": {+ "chain": null,+ "fields": null,+ "hogql_value": "deleted",+ "id": null,+ "name": "deleted",+ "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"+ },+ "last_updated_at": {+ "chain": null,+ "fields": null,+ "hogql_value": "last_updated_at",+ "id": null,+ "name": "last_updated_at",+ "schema_valid": true,+ "table": null,+ "type": "datetime"+ }+ },+ "id": "system.batch_export_on_demands",+ "name": "system.batch_export_on_demands",+ "row_count": null,+ "type": "system"+ },+ "system.batch_export_runs": {+ "certification": null,+ "fields": {+ "id": {+ "chain": null,+ "fields": null,+ "hogql_value": "id",+ "id": null,+ "name": "id",+ "schema_valid": true,+ "table": null,+ "type": "string"+ },+ "batch_export_id": {+ "chain": null,+ "fields": null,+ "hogql_value": "batch_export_id",+ "id": null,+ "name": "batch_export_id",+ "schema_valid": true,+ "table": null,+ "type": "string"+ },+ "batch_export_on_demand_id": {+ "chain": null,+ "fields": null,+ "hogql_value": "batch_export_on_demand_id",+ "id": null,+ "name": "batch_export_on_demand_id",+ "schema_valid": true,+ "table": null,+ "type": "string"+ },+ "backfill_id": {+ "chain": null,+ "fields": null,+ "hogql_value": "backfill_id",+ "id": null,+ "name": "backfill_id",+ "schema_valid": true,+ "table": null,+ "type": "string"+ },+ "data_interval_start": {+ "chain": null,+ "fields": null,+ "hogql_value": "data_interval_start",+ "id": null,+ "name": "data_interval_start",+ "schema_valid": true,+ "table": null,+ "type": "datetime"+ },+ "data_interval_end": {+ "chain": null,+ "fields": null,+ "hogql_value": "data_interval_end",+ "id": null,+ "name": "data_interval_end",+ "schema_valid": true,+ "table": null,+ "type": "datetime"+ },+ "status": {+ "chain": null,+ "fields": null,+ "hogql_value": "status",+ "id": null,+ "name": "status",+ "schema_valid": true,+ "table": null,+ "type": "string"+ },+ "created_at": {+ "chain": null,+ "fields": null,+ "hogql_value": "created_at",+ "id": null,+ "name": "created_at",+ "schema_valid": true,+ "table": null,+ "type": "datetime"+ },+ "finished_at": {+ "chain": null,+ "fields": null,+ "hogql_value": "finished_at",+ "id": null,+ "name": "finished_at",+ "schema_valid": true,+ "table": null,+ "type": "datetime"+ },+ "last_updated_at": {+ "chain": null,+ "fields": null,+ "hogql_value": "last_updated_at",+ "id": null,+ "name": "last_updated_at",+ "schema_valid": true,+ "table": null,+ "type": "datetime"+ },+ "records_completed": {+ "chain": null,+ "fields": null,+ "hogql_value": "records_completed",+ "id": null,+ "name": "records_completed",+ "schema_valid": true,+ "table": null,+ "type": "integer"+ },+ "bytes_exported": {+ "chain": null,+ "fields": null,+ "hogql_value": "bytes_exported",+ "id": null,+ "name": "bytes_exported",+ "schema_valid": true,+ "table": null,+ "type": "integer"+ }+ },+ "id": "system.batch_export_runs",+ "name": "system.batch_export_runs",+ "row_count": null,+ "type": "system"+ },"system.batch_exports": {"certification": null,"fields": {@@ -13406,6 +13624,224 @@"row_count": null,"type": "system"},+ "system.batch_export_on_demands": {+ "certification": null,+ "fields": {+ "id": {+ "chain": null,+ "fields": null,+ "hogql_value": "id",+ "id": null,+ "name": "id",+ "schema_valid": true,+ "table": null,+ "type": "string"+ },+ "destination_id": {+ "chain": null,+ "fields": null,+ "hogql_value": "destination_id",+ "id": null,+ "name": "destination_id",+ "schema_valid": true,+ "table": null,+ "type": "string"+ },+ "source_id": {+ "chain": null,+ "fields": null,+ "hogql_value": "source_id",+ "id": null,+ "name": "source_id",+ "schema_valid": true,+ "table": null,+ "type": "string"+ },+ "model": {+ "chain": null,+ "fields": null,+ "hogql_value": "model",+ "id": null,+ "name": "model",+ "schema_valid": true,+ "table": null,+ "type": "string"+ },+ "filters": {+ "chain": null,+ "fields": null,+ "hogql_value": "filters",+ "id": null,+ "name": "filters",+ "schema_valid": true,+ "table": null,+ "type": "json"+ },+ "deleted": {+ "chain": null,+ "fields": null,+ "hogql_value": "deleted",+ "id": null,+ "name": "deleted",+ "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"+ },+ "last_updated_at": {+ "chain": null,+ "fields": null,+ "hogql_value": "last_updated_at",+ "id": null,+ "name": "last_updated_at",+ "schema_valid": true,+ "table": null,+ "type": "datetime"+ }+ },+ "id": "system.batch_export_on_demands",+ "name": "system.batch_export_on_demands",+ "row_count": null,+ "type": "system"+ },+ "system.batch_export_runs": {+ "certification": null,+ "fields": {+ "id": {+ "chain": null,+ "fields": null,+ "hogql_value": "id",+ "id": null,+ "name": "id",+ "schema_valid": true,+ "table": null,+ "type": "string"+ },+ "batch_export_id": {+ "chain": null,+ "fields": null,+ "hogql_value": "batch_export_id",+ "id": null,+ "name": "batch_export_id",+ "schema_valid": true,+ "table": null,+ "type": "string"+ },+ "batch_export_on_demand_id": {+ "chain": null,+ "fields": null,+ "hogql_value": "batch_export_on_demand_id",+ "id": null,+ "name": "batch_export_on_demand_id",+ "schema_valid": true,+ "table": null,+ "type": "string"+ },+ "backfill_id": {+ "chain": null,+ "fields": null,+ "hogql_value": "backfill_id",+ "id": null,+ "name": "backfill_id",+ "schema_valid": true,+ "table": null,+ "type": "string"+ },+ "data_interval_start": {+ "chain": null,+ "fields": null,+ "hogql_value": "data_interval_start",+ "id": null,+ "name": "data_interval_start",+ "schema_valid": true,+ "table": null,+ "type": "datetime"+ },+ "data_interval_end": {+ "chain": null,+ "fields": null,+ "hogql_value": "data_interval_end",+ "id": null,+ "name": "data_interval_end",+ "schema_valid": true,+ "table": null,+ "type": "datetime"+ },+ "status": {+ "chain": null,+ "fields": null,+ "hogql_value": "status",+ "id": null,+ "name": "status",+ "schema_valid": true,+ "table": null,+ "type": "string"+ },+ "created_at": {+ "chain": null,+ "fields": null,+ "hogql_value": "created_at",+ "id": null,+ "name": "created_at",+ "schema_valid": true,+ "table": null,+ "type": "datetime"+ },+ "finished_at": {+ "chain": null,+ "fields": null,+ "hogql_value": "finished_at",+ "id": null,+ "name": "finished_at",+ "schema_valid": true,+ "table": null,+ "type": "datetime"+ },+ "last_updated_at": {+ "chain": null,+ "fields": null,+ "hogql_value": "last_updated_at",+ "id": null,+ "name": "last_updated_at",+ "schema_valid": true,+ "table": null,+ "type": "datetime"+ },+ "records_completed": {+ "chain": null,+ "fields": null,+ "hogql_value": "records_completed",+ "id": null,+ "name": "records_completed",+ "schema_valid": true,+ "table": null,+ "type": "integer"+ },+ "bytes_exported": {+ "chain": null,+ "fields": null,+ "hogql_value": "bytes_exported",+ "id": null,+ "name": "bytes_exported",+ "schema_valid": true,+ "table": null,+ "type": "integer"+ }+ },+ "id": "system.batch_export_runs",+ "name": "system.batch_export_runs",+ "row_count": null,+ "type": "system"+ },"system.batch_exports": {"certification": null,"fields": {