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(batch-exports): add BatchExportSource model for hogql-powered exports (#72020)

Ross6665ba951984
diff --git a/.semgrep/rules/security/idor-team-scoped-models.yaml b/.semgrep/rules/security/idor-team-scoped-models.yaml
index b6b48e5d3389..b3768d8ca814 100644
--- a/.semgrep/rules/security/idor-team-scoped-models.yaml
+++ b/.semgrep/rules/security/idor-team-scoped-models.yaml
@@ -80,6 +80,7 @@ rules:
|BatchExportBackfill
|BatchExportFileDownload
|BatchExportOnDemand
+ |BatchExportSource
|BatchImport
|BriefConfig
|ButtonTile
@@ -401,6 +402,7 @@ rules:
|BatchExportBackfill
|BatchExportFileDownload
|BatchExportOnDemand
+ |BatchExportSource
|BatchImport
|BriefConfig
|ButtonTile
diff --git a/products/batch_exports/backend/migrations/0005_add_batch_export_source.py b/products/batch_exports/backend/migrations/0005_add_batch_export_source.py
new file mode 100644
index 000000000000..02e5ac203b72
--- /dev/null
+++ b/products/batch_exports/backend/migrations/0005_add_batch_export_source.py
@@ -0,0 +1,120 @@
+# Generated by Django 5.2.14 on 2026-07-17 15:44
+
+import django.db.models.deletion
+from django.db import migrations, models
+
+import posthog.uuidt
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("batch_exports", "0004_migrate_managed_migrations_models"),
+ ("posthog", "1257_datadeletionrequest_approved_automatically_and_more"),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name="batchexport",
+ name="model",
+ field=models.CharField(
+ blank=True,
+ choices=[
+ ("events", "Events"),
+ ("persons", "Persons"),
+ ("sessions", "Sessions"),
+ ("hogql", "Hogql"),
+ ],
+ default="events",
+ help_text="Which model this BatchExport is exporting.",
+ max_length=64,
+ null=True,
+ ),
+ ),
+ migrations.AlterField(
+ model_name="batchexportondemand",
+ name="model",
+ field=models.CharField(
+ blank=True,
+ choices=[
+ ("events", "Events"),
+ ("persons", "Persons"),
+ ("sessions", "Sessions"),
+ ("hogql", "Hogql"),
+ ],
+ default="events",
+ help_text="Which model this batch export is exporting.",
+ max_length=64,
+ null=True,
+ ),
+ ),
+ migrations.CreateModel(
+ name="BatchExportSource",
+ fields=[
+ (
+ "id",
+ models.UUIDField(
+ default=posthog.uuidt.UUIDT,
+ editable=False,
+ primary_key=True,
+ serialize=False,
+ ),
+ ),
+ (
+ "hogql_query",
+ models.TextField(
+ blank=True,
+ help_text="The HogQL query whose results are exported.",
+ null=True,
+ ),
+ ),
+ (
+ "created_at",
+ models.DateTimeField(
+ auto_now_add=True,
+ help_text="The timestamp at which this BatchExportSource was created.",
+ ),
+ ),
+ (
+ "last_updated_at",
+ models.DateTimeField(
+ auto_now=True,
+ help_text="The timestamp at which this BatchExportSource was last updated.",
+ ),
+ ),
+ (
+ "team",
+ models.ForeignKey(
+ db_constraint=False,
+ help_text="The team this belongs to.",
+ on_delete=django.db.models.deletion.CASCADE,
+ to="posthog.team",
+ ),
+ ),
+ ],
+ options={
+ "db_table": "posthog_batchexportsource",
+ },
+ ),
+ migrations.AddField(
+ model_name="batchexport",
+ name="source",
+ field=models.ForeignKey(
+ blank=True,
+ help_text="The source of the data to export. When set, takes precedence over `model`.",
+ null=True,
+ on_delete=django.db.models.deletion.SET_NULL,
+ to="batch_exports.batchexportsource",
+ ),
+ ),
+ migrations.AddField(
+ model_name="batchexportondemand",
+ name="source",
+ field=models.ForeignKey(
+ blank=True,
+ help_text="The source of the data to export. When set, takes precedence over `model`.",
+ null=True,
+ on_delete=django.db.models.deletion.SET_NULL,
+ to="batch_exports.batchexportsource",
+ ),
+ ),
+ ]
diff --git a/products/batch_exports/backend/migrations/max_migration.txt b/products/batch_exports/backend/migrations/max_migration.txt
index 9e0ef6f5da0c..7a1fcb964e55 100644
--- a/products/batch_exports/backend/migrations/max_migration.txt
+++ b/products/batch_exports/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0004_migrate_managed_migrations_models
+0005_add_batch_export_source
diff --git a/products/batch_exports/backend/models/__init__.py b/products/batch_exports/backend/models/__init__.py
index c6b1f105f738..ff147beb80e8 100644
--- a/products/batch_exports/backend/models/__init__.py
+++ b/products/batch_exports/backend/models/__init__.py
@@ -5,6 +5,7 @@ from .batch_export import (
BatchExportFileDownload,
BatchExportOnDemand,
BatchExportRun,
+ BatchExportSource,
DayOfWeek,
)
@@ -15,5 +16,6 @@ __all__ = [
"BatchExportFileDownload",
"BatchExportOnDemand",
"BatchExportRun",
+ "BatchExportSource",
"DayOfWeek",
]
diff --git a/products/batch_exports/backend/models/batch_export.py b/products/batch_exports/backend/models/batch_export.py
index 016215aa67bc..c9ea59739232 100644
--- a/products/batch_exports/backend/models/batch_export.py
+++ b/products/batch_exports/backend/models/batch_export.py
@@ -128,6 +128,43 @@ class BatchExportDestination(UUIDTModel):
)
+class BatchExportSource(TeamScopedRootMixin, UUIDTModel):
+ """A model for the source of the data that a PostHog BatchExport will export.
+
+ This model answers the question: what data are we exporting? For now it holds a
+ HogQL query whose results are exported, but it's designed to grow into the single
+ place that captures how a batch export selects its data (data warehouse view
+ references, export mode, data interval field, primary and version keys) in future.
+ """
+
+ class Meta:
+ db_table = "posthog_batchexportsource"
+
+ # `db_constraint=False`: a real FK constraint to the hot `posthog_team` table would
+ # take a lock on it while being created. Team scoping is enforced at the app level
+ # via `TeamScopedRootMixin`. See products/README.md "Adding or moving backend models
+ # and migrations".
+ team = models.ForeignKey(
+ "posthog.Team",
+ on_delete=models.CASCADE,
+ db_constraint=False,
+ help_text="The team this belongs to.",
+ )
+ hogql_query = models.TextField(
+ null=True,
+ blank=True,
+ help_text="The HogQL query whose results are exported.",
+ )
+ created_at = models.DateTimeField(
+ auto_now_add=True,
+ help_text="The timestamp at which this BatchExportSource was created.",
+ )
+ last_updated_at = models.DateTimeField(
+ auto_now=True,
+ help_text="The timestamp at which this BatchExportSource was last updated.",
+ )
+
+
class BatchExportRun(UUIDTModel):
"""A single run of a PostHog batch export defined by its data interval bounds.
@@ -280,6 +317,7 @@ class BatchExport(ModelActivityMixin, UUIDTModel):
EVENTS = "events"
PERSONS = "persons"
SESSIONS = "sessions"
+ HOGQL = "hogql"
team = models.ForeignKey("posthog.Team", on_delete=models.CASCADE, help_text="The team this belongs to.")
name = models.TextField(help_text="A human-readable name for this BatchExport.")
@@ -288,6 +326,13 @@ class BatchExport(ModelActivityMixin, UUIDTModel):
on_delete=models.CASCADE,
help_text="The destination to export data to.",
)
+ source = models.ForeignKey(
+ "BatchExportSource",
+ on_delete=models.SET_NULL,
+ null=True,
+ blank=True,
+ help_text="The source of the data to export. When set, takes precedence over `model`.",
+ )
interval = models.CharField(
max_length=64,
null=False,
@@ -618,6 +663,7 @@ class BatchExportOnDemand(TeamScopedRootMixin, ModelActivityMixin, UUIDTModel):
EVENTS = "events"
PERSONS = "persons"
SESSIONS = "sessions"
+ HOGQL = "hogql"
team = models.ForeignKey("posthog.Team", on_delete=models.CASCADE, help_text="The team this belongs to.")
destination = models.ForeignKey(
@@ -625,6 +671,13 @@ class BatchExportOnDemand(TeamScopedRootMixin, ModelActivityMixin, UUIDTModel):
on_delete=models.CASCADE,
help_text="The destination to export data to.",
)
+ source = models.ForeignKey(
+ "BatchExportSource",
+ on_delete=models.SET_NULL,
+ null=True,
+ blank=True,
+ help_text="The source of the data to export. When set, takes precedence over `model`.",
+ )
deleted = models.BooleanField(default=False, help_text="Whether this is deleted or not.")
created_at = models.DateTimeField(
auto_now_add=True,
diff --git a/products/batch_exports/frontend/generated/api.schemas.ts b/products/batch_exports/frontend/generated/api.schemas.ts
index 1191fbce4d0b..9217bfa0fe54 100644
--- a/products/batch_exports/frontend/generated/api.schemas.ts
+++ b/products/batch_exports/frontend/generated/api.schemas.ts
@@ -11,6 +11,7 @@
* * `events` - Events
* * `persons` - Persons
* * `sessions` - Sessions
+ * * `hogql` - Hogql
*/
export type ModelEnumApi = (typeof ModelEnumApi)[keyof typeof ModelEnumApi]
@@ -18,6 +19,7 @@ export const ModelEnumApi = {
Events: 'events',
Persons: 'persons',
Sessions: 'sessions',
+ Hogql: 'hogql',
} as const
export type BlankEnumApi = (typeof BlankEnumApi)[keyof typeof BlankEnumApi]
@@ -527,7 +529,8 @@ export interface BatchExportApi {
*
* * `events` - Events
* * `persons` - Persons
- * * `sessions` - Sessions */
+ * * `sessions` - Sessions
+ * * `hogql` - Hogql */
model?: ModelEnumApi | BlankEnumApi | null
/** Destination configuration (type, config, and optional integration). */
destination: BatchExportDestinationApi
@@ -1333,7 +1336,8 @@ export interface BatchExportRequestApi {
*
* * `events` - Events
* * `persons` - Persons
- * * `sessions` - Sessions */
+ * * `sessions` - Sessions
+ * * `hogql` - Hogql */
model?: ModelEnumApi
/** Destination configuration. Required integration_id is enforced per destination type. */
destination: BatchExportDestinationRequestApi
@@ -1493,7 +1497,8 @@ export interface PatchedBatchExportRequestApi {
*
* * `events` - Events
* * `persons` - Persons
- * * `sessions` - Sessions */
+ * * `sessions` - Sessions
+ * * `hogql` - Hogql */
model?: ModelEnumApi
/** Destination configuration. Required integration_id is enforced per destination type. */
destination?: BatchExportDestinationRequestApi
diff --git a/products/batch_exports/frontend/generated/api.zod.ts b/products/batch_exports/frontend/generated/api.zod.ts
index 9b55507ccd8b..584c70ce0da7 100644
--- a/products/batch_exports/frontend/generated/api.zod.ts
+++ b/products/batch_exports/frontend/generated/api.zod.ts
@@ -32,11 +32,11 @@ export const BatchExportsCreateBody = /* @__PURE__ */ zod
.object({
name: zod.string().describe('Human-readable name for the batch export.'),
model: zod
- .enum(['events', 'persons', 'sessions'])
- .describe('\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions')
+ .enum(['events', 'persons', 'sessions', 'hogql'])
+ .describe('\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions\n\* `hogql` - Hogql')
.optional()
.describe(
- 'Which data model to export (events, persons, sessions).\n\n\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions'
+ 'Which data model to export (events, persons, sessions).\n\n\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions\n\* `hogql` - Hogql'
),
destination: zod
.union([
@@ -636,11 +636,11 @@ export const BatchExportsUpdateBody = /* @__PURE__ */ zod
.object({
name: zod.string().describe('Human-readable name for the batch export.'),
model: zod
- .enum(['events', 'persons', 'sessions'])
- .describe('\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions')
+ .enum(['events', 'persons', 'sessions', 'hogql'])
+ .describe('\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions\n\* `hogql` - Hogql')
.optional()
.describe(
- 'Which data model to export (events, persons, sessions).\n\n\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions'
+ 'Which data model to export (events, persons, sessions).\n\n\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions\n\* `hogql` - Hogql'
),
destination: zod
.union([
@@ -993,11 +993,11 @@ export const BatchExportsPartialUpdateBody = /* @__PURE__ */ zod
.object({
name: zod.string().optional().describe('Human-readable name for the batch export.'),
model: zod
- .enum(['events', 'persons', 'sessions'])
- .describe('\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions')
+ .enum(['events', 'persons', 'sessions', 'hogql'])
+ .describe('\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions\n\* `hogql` - Hogql')
.optional()
.describe(
- 'Which data model to export (events, persons, sessions).\n\n\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions'
+ 'Which data model to export (events, persons, sessions).\n\n\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions\n\* `hogql` - Hogql'
),
destination: zod
.union([
@@ -1359,14 +1359,16 @@ export const BatchExportsPauseCreateBody = /* @__PURE__ */ zod
model: zod
.union([
zod
- .enum(['events', 'persons', 'sessions'])
- .describe('\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions'),
+ .enum(['events', 'persons', 'sessions', 'hogql'])
+ .describe(
+ '\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions\n\* `hogql` - Hogql'
+ ),
zod.enum(['']),
zod.null(),
])
.optional()
.describe(
- 'Which model this BatchExport is exporting.\n\n\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions'
+ 'Which model this BatchExport is exporting.\n\n\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions\n\* `hogql` - Hogql'
),
destination: zod
.object({
@@ -1697,14 +1699,16 @@ export const BatchExportsRunTestStepCreateBody = /* @__PURE__ */ zod
model: zod
.union([
zod
- .enum(['events', 'persons', 'sessions'])
- .describe('\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions'),
+ .enum(['events', 'persons', 'sessions', 'hogql'])
+ .describe(
+ '\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions\n\* `hogql` - Hogql'
+ ),
zod.enum(['']),
zod.null(),
])
.optional()
.describe(
- 'Which model this BatchExport is exporting.\n\n\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions'
+ 'Which model this BatchExport is exporting.\n\n\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions\n\* `hogql` - Hogql'
),
destination: zod
.object({
@@ -2054,14 +2058,16 @@ export const BatchExportsUnpauseCreateBody = /* @__PURE__ */ zod
model: zod
.union([
zod
- .enum(['events', 'persons', 'sessions'])
- .describe('\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions'),
+ .enum(['events', 'persons', 'sessions', 'hogql'])
+ .describe(
+ '\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions\n\* `hogql` - Hogql'
+ ),
zod.enum(['']),
zod.null(),
])
.optional()
.describe(
- 'Which model this BatchExport is exporting.\n\n\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions'
+ 'Which model this BatchExport is exporting.\n\n\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions\n\* `hogql` - Hogql'
),
destination: zod
.object({
@@ -2396,14 +2402,16 @@ export const BatchExportsRunTestStepNewCreateBody = /* @__PURE__ */ zod
model: zod
.union([
zod
- .enum(['events', 'persons', 'sessions'])
- .describe('\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions'),
+ .enum(['events', 'persons', 'sessions', 'hogql'])
+ .describe(
+ '\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions\n\* `hogql` - Hogql'
+ ),
zod.enum(['']),
zod.null(),
])
.optional()
.describe(
- 'Which model this BatchExport is exporting.\n\n\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions'
+ 'Which model this BatchExport is exporting.\n\n\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions\n\* `hogql` - Hogql'
),
destination: zod
.object({
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index 6c36be3bb104..d0effc64aa73 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -10936,6 +10936,7 @@ export namespace Schemas {
* * `events` - Events
* * `persons` - Persons
* * `sessions` - Sessions
+ * * `hogql` - Hogql
*/
export type ModelEnum = typeof ModelEnum[keyof typeof ModelEnum];
@@ -10944,6 +10945,7 @@ export namespace Schemas {
Events: 'events',
Persons: 'persons',
Sessions: 'sessions',
+ Hogql: 'hogql',
} as const;
/**
@@ -11324,7 +11326,8 @@ export namespace Schemas {
*
* * `events` - Events
* * `persons` - Persons
- * * `sessions` - Sessions */
+ * * `sessions` - Sessions
+ * * `hogql` - Hogql */
model?: ModelEnum | BlankEnum | null;
/** Destination configuration (type, config, and optional integration). */
destination: BatchExportDestination;
@@ -12171,7 +12174,8 @@ export namespace Schemas {
*
* * `events` - Events
* * `persons` - Persons
- * * `sessions` - Sessions */
+ * * `sessions` - Sessions
+ * * `hogql` - Hogql */
model?: ModelEnum;
/** Destination configuration. Required integration_id is enforced per destination type. */
destination: BatchExportDestinationRequest;
@@ -46754,7 +46758,8 @@ export namespace Schemas {
*
* * `events` - Events
* * `persons` - Persons
- * * `sessions` - Sessions */
+ * * `sessions` - Sessions
+ * * `hogql` - Hogql */
model?: ModelEnum;
/** Destination configuration. Required integration_id is enforced per destination type. */
destination?: BatchExportDestinationRequest;
diff --git a/services/mcp/src/generated/batch_exports/api.ts b/services/mcp/src/generated/batch_exports/api.ts
index 08b20796f29b..c65b20369438 100644
--- a/services/mcp/src/generated/batch_exports/api.ts
+++ b/services/mcp/src/generated/batch_exports/api.ts
@@ -52,11 +52,11 @@ export const BatchExportsCreateBody = /* @__PURE__ */ zod
.object({
name: zod.string().describe('Human-readable name for the batch export.'),
model: zod
- .enum(['events', 'persons', 'sessions'])
- .describe('\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions')
+ .enum(['events', 'persons', 'sessions', 'hogql'])
+ .describe('\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions\n\* `hogql` - Hogql')
.optional()
.describe(
- 'Which data model to export (events, persons, sessions).\n\n\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions'
+ 'Which data model to export (events, persons, sessions).\n\n\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions\n\* `hogql` - Hogql'
),
destination: zod
.union([
@@ -410,11 +410,11 @@ export const BatchExportsPartialUpdateBody = /* @__PURE__ */ zod
.object({
name: zod.string().optional().describe('Human-readable name for the batch export.'),
model: zod
- .enum(['events', 'persons', 'sessions'])
- .describe('\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions')
+ .enum(['events', 'persons', 'sessions', 'hogql'])
+ .describe('\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions\n\* `hogql` - Hogql')
.optional()
.describe(
- 'Which data model to export (events, persons, sessions).\n\n\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions'
+ 'Which data model to export (events, persons, sessions).\n\n\* `events` - Events\n\* `persons` - Persons\n\* `sessions` - Sessions\n\* `hogql` - Hogql'
),
destination: zod
.union([
diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/batch-export-create.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/batch-export-create.json
index ba5119806ed7..10c2a3678c39 100644
--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/batch-export-create.json
+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/batch-export-create.json
@@ -404,8 +404,8 @@
"type": "string"
},
"model": {
- "description": "Which data model to export (events, persons, sessions).\n\n* `events` - Events\n* `persons` - Persons\n* `sessions` - Sessions",
- "enum": ["events", "persons", "sessions"],
+ "description": "Which data model to export (events, persons, sessions).\n\n* `events` - Events\n* `persons` - Persons\n* `sessions` - Sessions\n* `hogql` - Hogql",
+ "enum": ["events", "persons", "sessions", "hogql"],
"type": "string"
},
"name": {
diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/batch-export-update.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/batch-export-update.json
index 01ac83accc98..85b8e52e50b1 100644
--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/batch-export-update.json
+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/batch-export-update.json
@@ -407,8 +407,8 @@
"type": "string"
},
"model": {
- "description": "Which data model to export (events, persons, sessions).\n\n* `events` - Events\n* `persons` - Persons\n* `sessions` - Sessions",
- "enum": ["events", "persons", "sessions"],
+ "description": "Which data model to export (events, persons, sessions).\n\n* `events` - Events\n* `persons` - Persons\n* `sessions` - Sessions\n* `hogql` - Hogql",
+ "enum": ["events", "persons", "sessions", "hogql"],
"type": "string"
},
"name": {