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(dags): retire events_backfill_to_ducklake (#64099)

James Greenhill718fe7b678f7
diff --git a/.test_durations b/.test_durations
index a9649459091e..7b416f6ceb18 100644
--- a/.test_durations
+++ b/.test_durations
@@ -15693,19 +15693,6 @@
"posthog/dags/tests/test_distinct_id_usage.py::TestTruncateDistinctId::test_long_string_truncated": 0.01,
"posthog/dags/tests/test_distinct_id_usage.py::TestTruncateDistinctId::test_short_string_unchanged": 0.01,
"posthog/dags/tests/test_distinct_id_usage.py::test_query_distinct_id_usage": 3.9912897649999195,
- "posthog/dags/tests/test_events_backfill_to_ducklake.py::TestEventsColumnsSchema::test_events_columns_has_expected_columns": 0.01,
- "posthog/dags/tests/test_events_backfill_to_ducklake.py::TestEventsColumnsSchema::test_expected_ducklake_columns_matches_export": 0.01,
- "posthog/dags/tests/test_events_backfill_to_ducklake.py::TestGetPartitionWhereClause::test_custom_timestamp_field": 0.01,
- "posthog/dags/tests/test_events_backfill_to_ducklake.py::TestGetPartitionWhereClause::test_generates_correct_where_clause": 0.01,
- "posthog/dags/tests/test_events_backfill_to_ducklake.py::TestGetS3FunctionArgs::test_local_mode_redacts_credentials": 0.01,
- "posthog/dags/tests/test_events_backfill_to_ducklake.py::TestGetS3FunctionArgs::test_production_mode": 0.01,
- "posthog/dags/tests/test_events_backfill_to_ducklake.py::TestGetS3PathForPartition::test_get_s3_path_for_partition_local": 0.01,
- "posthog/dags/tests/test_events_backfill_to_ducklake.py::TestGetS3PathForPartition::test_get_s3_path_for_partition_prod_0_posthog_ducklake_dev": 0.01,
- "posthog/dags/tests/test_events_backfill_to_ducklake.py::TestGetS3PathForPartition::test_get_s3_path_for_partition_prod_1_posthog_ducklake_prod_eu": 0.01,
- "posthog/dags/tests/test_events_backfill_to_ducklake.py::TestTagsForEventsPartition::test_tags_for_events_partition_0_2025_01_15": 0.01,
- "posthog/dags/tests/test_events_backfill_to_ducklake.py::TestTagsForEventsPartition::test_tags_for_events_partition_1_2025_10_31": 0.01,
- "posthog/dags/tests/test_events_backfill_to_ducklake.py::TestTagsForEventsPartition::test_tags_for_events_partition_2_2025_11_01": 0.01,
- "posthog/dags/tests/test_events_backfill_to_ducklake.py::TestTagsForEventsPartition::test_tags_for_events_partition_3_2019_01_01": 0.01,
"posthog/dags/tests/test_fix_missing_person_overrides.py::TestFixMissingPersonOverridesJob::test_dry_run_does_not_insert": 0.605204201000106,
"posthog/dags/tests/test_fix_missing_person_overrides.py::TestFixMissingPersonOverridesJob::test_inserts_overrides_for_mismatched_distinct_ids": 0.5396306589998403,
"posthog/dags/tests/test_fix_missing_person_overrides.py::TestFixMissingPersonOverridesJob::test_skips_distinct_ids_with_existing_overrides": 0.579883559000109,
diff --git a/posthog/dags/events_backfill_to_ducklake.py b/posthog/dags/events_backfill_to_ducklake.py
deleted file mode 100644
index d76585cd40b7..000000000000
--- a/posthog/dags/events_backfill_to_ducklake.py
+++ /dev/null
@@ -1,719 +0,0 @@
-"""
-Dagster job to backfill ClickHouse events to DuckLake.
-
-This job exports events from ClickHouse's `posthog.events` table to S3 as Parquet files,
-then registers those files with DuckLake using `ducklake_add_data_files`.
-
-The job is partitioned by date to allow incremental backfilling of historical data.
-Within each date partition, events are further chunked by team_id to keep file sizes manageable.
-
-S3 path structure: s3://{bucket}/backfill/events/{team_id}/{year}/{month}/{day}/
-"""
-
-import base64
-from concurrent.futures import ThreadPoolExecutor, as_completed
-from datetime import datetime
-from typing import Any
-
-from django.conf import settings as django_settings
-
-import duckdb
-import dagster
-import structlog
-from clickhouse_driver import Client
-from clickhouse_driver.errors import Error as ClickHouseError
-from dagster import (
- AssetExecutionContext,
- BackfillPolicy,
- Config,
- DailyPartitionsDefinition,
- PartitionedConfig,
- asset,
- define_asset_job,
-)
-from tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_exponential
-
-from posthog.clickhouse.client.connection import NodeRole, Workload
-from posthog.clickhouse.cluster import get_cluster
-from posthog.clickhouse.query_tagging import tags_context
-from posthog.cloud_utils import is_cloud
-from posthog.dags.common import JobOwners, dagster_tags, settings_with_log_comment
-from posthog.ducklake.common import attach_catalog, escape, get_config
-from posthog.ducklake.storage import DuckLakeStorageConfig, configure_connection
-from posthog.settings.base_variables import DEBUG
-from posthog.settings.object_storage import (
- OBJECT_STORAGE_ACCESS_KEY_ID,
- OBJECT_STORAGE_ENDPOINT,
- OBJECT_STORAGE_SECRET_ACCESS_KEY,
-)
-
-logger = structlog.get_logger(__name__)
-
-MAX_PARTITIONS_PER_RUN = 1
-MAX_PARALLEL_CHUNKS = 4
-MAX_RETRY_ATTEMPTS = 3
-
-CONCURRENCY_TAG = {
- "events_ducklake_backfill_concurrency": "events_ducklake_v1",
-}
-
-
-def get_ducklake_bucket() -> str:
- config = get_config()
- return config["DUCKLAKE_BUCKET"]
-
-
-def get_ducklake_region() -> str:
- config = get_config()
- return config["DUCKLAKE_BUCKET_REGION"]
-
-
-BACKFILL_S3_PREFIX = "backfill/events"
-
-
-class EventsBackfillConfig(Config):
- """Config for events backfill to DuckLake jobs."""
-
- clickhouse_settings: dict[str, Any] | None = None
- team_id_chunks: int = 64
- parallel_chunks: int = MAX_PARALLEL_CHUNKS
- skip_ducklake_registration: bool = False
- dry_run: bool = False
- cleanup_prior_run_files: bool = True
-
-
-daily_partitions = DailyPartitionsDefinition(
- start_date="2019-01-01",
- timezone="UTC",
- end_offset=0,
-)
-
-
-ONE_HOUR_IN_SECONDS = 60 * 60
-ONE_GB_IN_BYTES = 1024 * 1024 * 1024
-
-DEFAULT_CLICKHOUSE_SETTINGS = {
- "max_execution_time": 4 * ONE_HOUR_IN_SECONDS,
- "max_memory_usage": 50 * ONE_GB_IN_BYTES,
- "distributed_aggregation_memory_efficient": "1",
-}
-
-
-# Columns to export from ClickHouse events table.
-# This matches the schema that the DuckLake streaming connector (via Kafka) expects.
-# Note: We use toInt64(team_id) as project_id since they're equivalent in PostHog.
-# Materialized columns (dmat_*) are ClickHouse-specific and not present in DuckLake.
-EVENTS_COLUMNS = """
- toString(uuid) as uuid,
- event,
- properties,
- timestamp,
- team_id,
- toInt64(team_id) as project_id,
- distinct_id,
- elements_chain,
- created_at,
- toString(person_id) as person_id,
- person_created_at,
- person_properties,
- group0_properties,
- group1_properties,
- group2_properties,
- group3_properties,
- group4_properties,
- group0_created_at,
- group1_created_at,
- group2_created_at,
- group3_created_at,
- group4_created_at,
- person_mode,
- historical_migration,
- NOW() as _inserted_at
-"""
-
-# Expected columns in the DuckLake events table (for schema validation)
-EXPECTED_DUCKLAKE_COLUMNS = {
- "uuid",
- "event",
- "properties",
- "timestamp",
- "team_id",
- "project_id",
- "distinct_id",
- "elements_chain",
- "created_at",
- "person_id",
- "person_created_at",
- "person_properties",
- "group0_properties",
- "group1_properties",
- "group2_properties",
- "group3_properties",
- "group4_properties",
- "group0_created_at",
- "group1_created_at",
- "group2_created_at",
- "group3_created_at",
- "group4_created_at",
- "person_mode",
- "historical_migration",
- "_inserted_at",
-}
-
-
-def tags_for_events_partition(partition_key: str) -> dict[str, str]:
- """Generate tags for an events backfill partition.
-
- Uses the partition date (YYYYMM) to ensure only one concurrent export
- per DuckLake partition. This prevents race conditions when multiple
- backfill runs try to write to the same partition.
- """
- date = datetime.strptime(partition_key, "%Y-%m-%d")
- return {
- "events_ducklake_partition": f"dl_{date.strftime('%Y%m%d')}",
- }
-
-
-def metabase_debug_query_url(run_id: str) -> str | None:
- """Generate a debug URL for viewing ClickHouse query logs for this run."""
- cloud_deployment = getattr(django_settings, "CLOUD_DEPLOYMENT", None)
- if cloud_deployment == "US":
- return f"https://metabase.prod-us.posthog.dev/question/1671-get-clickhouse-query-log-for-given-dagster-run-id?dagster_run_id={run_id}"
- if cloud_deployment == "EU":
- return f"https://metabase.prod-eu.posthog.dev/question/544-get-clickhouse-query-log-for-given-dagster-run-id?dagster_run_id={run_id}"
- sql = f"""
-SELECT
- hostName() as host,
- event_time,
- type,
- exception IS NOT NULL and exception != '' as has_exception,
- query_duration_ms,
- formatReadableSize(memory_usage) as memory_used,
- formatReadableSize(read_bytes) as data_read,
- JSONExtractString(log_comment, 'dagster', 'run_id') AS dagster_run_id,
- JSONExtractString(log_comment, 'dagster', 'job_name') AS dagster_job_name,
- JSONExtractString(log_comment, 'dagster', 'asset_key') AS dagster_asset_key,
- JSONExtractString(log_comment, 'dagster', 'op_name') AS dagster_op_name,
- exception,
- query
-FROM clusterAllReplicas('posthog', system.query_log)
-WHERE
- dagster_run_id = '{run_id}'
- AND event_date >= today() - 1
-ORDER BY event_time DESC;
-"""
- return f"http://localhost:8123/play?user=default#{base64.b64encode(sql.encode('utf-8')).decode('utf-8')}"
-
-
-# The shared DuckLake table should be partitioned by team_id first (most
-# selective predicate — every query filters by it) then by date.
-DUCKLAKE_EVENTS_PARTITION_EXPR = "team_id, year(timestamp), month(timestamp), day(timestamp)"
-
-
-def validate_ducklake_schema(context: AssetExecutionContext) -> None:
- """Validate that the DuckLake events table schema matches our export columns.
-
- This pre-flight check ensures we don't waste time exporting data that can't
- be registered with DuckLake due to schema mismatches. Also ensures the table
- is partitioned by (team_id, year, month, day) for efficient pruning.
- """
- ducklake_config = get_config()
- storage_config = DuckLakeStorageConfig.from_runtime()
- alias = "ducklake"
-
- conn = duckdb.connect()
- try:
- configure_connection(conn, storage_config)
-
- try:
- attach_catalog(conn, ducklake_config, alias=alias)
- except duckdb.CatalogException as exc:
- if alias not in str(exc):
- raise
-
- result = conn.execute(f"DESCRIBE {alias}.posthog.events").fetchall()
- ducklake_columns = {row[0] for row in result}
-
- missing_in_ducklake = EXPECTED_DUCKLAKE_COLUMNS - ducklake_columns
- if missing_in_ducklake:
- context.log.warning(
- f"DuckLake events table is missing columns that we export: {missing_in_ducklake}. "
- "These columns will be added automatically by ducklake_add_data_files if the table "
- "supports schema evolution."
- )
- logger.warning(
- "ducklake_schema_mismatch",
- missing_columns=list(missing_in_ducklake),
- )
-
- extra_in_ducklake = ducklake_columns - EXPECTED_DUCKLAKE_COLUMNS
- if extra_in_ducklake:
- # Extra columns in DuckLake are fine - they might come from streaming
- # (e.g., _kafka_* metadata columns)
- context.log.info(f"DuckLake has additional columns not in our export: {extra_in_ducklake}")
-
- context.log.info(
- f"Schema validation passed. DuckLake has {len(ducklake_columns)} columns, "
- f"we export {len(EXPECTED_DUCKLAKE_COLUMNS)} columns."
- )
- logger.info(
- "ducklake_schema_validation_passed",
- ducklake_columns=len(ducklake_columns),
- export_columns=len(EXPECTED_DUCKLAKE_COLUMNS),
- )
-
- # Ensure partitioning includes team_id for efficient pruning (idempotent).
- try:
- conn.execute(f"ALTER TABLE {alias}.posthog.events SET PARTITIONED BY ({DUCKLAKE_EVENTS_PARTITION_EXPR})")
- context.log.info(f"Partitioning set: {DUCKLAKE_EVENTS_PARTITION_EXPR}")
- except Exception as exc:
- context.log.warning(f"Failed to set partitioning (non-fatal): {exc}")
- logger.warning(
- "ducklake_partitioning_failed",
- error=str(exc),
- partition_expr=DUCKLAKE_EVENTS_PARTITION_EXPR,
- )
-
- finally:
- conn.close()
-
-
-def get_partition_where_clause(context: AssetExecutionContext, timestamp_field: str = "timestamp") -> str:
- start_incl = context.partition_time_window.start.strftime("%Y-%m-%d")
- end_excl = context.partition_time_window.end.strftime("%Y-%m-%d")
- return f"toDate({timestamp_field}) >= '{start_incl}' AND toDate({timestamp_field}) < '{end_excl}'"
-
-
-def get_s3_path_for_partition(
- bucket: str,
- region: str,
- team_id: int | str,
- date: datetime,
- chunk_id: str,
- is_local: bool = False,
-) -> str:
- """Build S3 path for a partition file.
-
- Path structure: s3://{bucket}/backfill/events/{team_id}/{year}/{month}/{day}/{chunk_id}.parquet
-
- TODO: These paths use plain segments, not Hive-style key=value. This means
- ducklake_add_data_files will fail on partitioned tables (0 Hive keys vs N
- partition fields). Fixing this requires restructuring the chunking strategy:
- the table partitions by team_id (IDENTITY) but each file contains data from
- multiple team_ids (team_id % total_chunks = chunk), so a single file can't
- map to one team_id partition. Either remove team_id from the partition
- expression or export one file per team_id instead of modular chunks.
- """
- year = date.strftime("%Y")
- month = date.strftime("%m")
- day = date.strftime("%d")
-
- filename = f"{BACKFILL_S3_PREFIX}/{team_id}/{year}/{month}/{day}/{chunk_id}.parquet"
-
- if is_local:
- return f"{OBJECT_STORAGE_ENDPOINT}/{bucket}/{filename}"
- else:
- return f"https://{bucket}.s3.{region}.amazonaws.com/{filename}"
-
-
-def get_s3_function_args(s3_path: str, is_local: bool = False) -> tuple[str, str]:
- """Build the arguments for ClickHouse s3() function.
-
- Returns tuple of (args_string, safe_args_string_for_logging).
- """
- if is_local:
- args = f"'{s3_path}', '{OBJECT_STORAGE_ACCESS_KEY_ID}', '{OBJECT_STORAGE_SECRET_ACCESS_KEY}', 'Parquet'"
- safe_args = f"'{s3_path}', '[REDACTED]', '[REDACTED]', 'Parquet'"
- return args, safe_args
- else:
- args = f"'{s3_path}', 'Parquet'"
- return args, args
-
-
-@retry(
- stop=stop_after_attempt(MAX_RETRY_ATTEMPTS),
- wait=wait_exponential(multiplier=1, min=4, max=60),
- retry=retry_if_exception_type((ClickHouseError, OSError, TimeoutError)),
- reraise=True,
-)
-def _execute_export_with_retry(
- client: Client,
- export_sql: str,
- settings: dict[str, Any],
- chunk_info: str,
-) -> None:
- """Execute export SQL with retry logic for transient failures."""
- try:
- client.execute(export_sql, settings=settings)
- except Exception as e:
- logger.warning(
- "export_chunk_retry",
- chunk_info=chunk_info,
- error=str(e),
- error_type=type(e).__name__,
- )
- raise
-
-
-def export_events_to_s3(
- context: AssetExecutionContext,
- client: Client,
- config: EventsBackfillConfig,
- where_clause: str,
- team_id_chunk: int,
- total_chunks: int,
- partition_date: datetime,
- run_id: str,
- settings: dict[str, Any],
-) -> list[str]:
- """Export events for a specific team_id chunk to S3 as Parquet.
-
- Returns list of S3 paths that were written.
- """
- is_local = DEBUG
- bucket = get_ducklake_bucket()
- region = get_ducklake_region()
- team_id_expr = f"team_id % {total_chunks} = {team_id_chunk}"
- chunk_where = f"({where_clause}) AND ({team_id_expr})"
-
- chunk_id = f"chunk_{team_id_chunk:04d}_run_{run_id}"
- s3_path = get_s3_path_for_partition(
- bucket=bucket,
- region=region,
- team_id=f"mod{total_chunks}eq{team_id_chunk}",
- date=partition_date,
- chunk_id=chunk_id,
- is_local=is_local,
- )
-
- s3_args, safe_s3_args = get_s3_function_args(s3_path, is_local=is_local)
-
- export_sql = f"""
- INSERT INTO FUNCTION s3({s3_args})
- SELECT
- {EVENTS_COLUMNS}
- FROM events
- WHERE {chunk_where}
- SETTINGS s3_truncate_on_insert=1, use_hive_partitioning=0
- """
-
- chunk_info = f"{team_id_chunk + 1}/{total_chunks}"
-
- if config.dry_run:
- # Log with redacted credentials
- safe_sql = f"""
- INSERT INTO FUNCTION s3({safe_s3_args})
- SELECT
- {EVENTS_COLUMNS}
- FROM events
- WHERE {chunk_where}
- SETTINGS s3_truncate_on_insert=1, use_hive_partitioning=0
- """
- context.log.info(f"[DRY RUN] Would export chunk {chunk_info} with SQL: {safe_sql[:800]}...")
- return []
-
- context.log.info(f"Exporting events chunk {chunk_info} to {s3_path}")
- logger.info(
- "export_chunk_start",
- chunk=team_id_chunk,
- total_chunks=total_chunks,
- s3_path=s3_path,
- partition_date=partition_date.isoformat(),
- )
-
- try:
- _execute_export_with_retry(client, export_sql, settings, chunk_info)
- context.log.info(f"Successfully exported chunk {chunk_info}")
- logger.info("export_chunk_success", chunk=team_id_chunk, total_chunks=total_chunks)
- return [s3_path]
- except Exception:
- context.log.exception(f"Failed to export chunk {chunk_info} after {MAX_RETRY_ATTEMPTS} attempts")
- logger.exception("export_chunk_failed", chunk=team_id_chunk, total_chunks=total_chunks)
- raise
-
-
-def cleanup_prior_run_files(
- context: AssetExecutionContext,
- partition_date: datetime,
- current_run_id: str,
-) -> int:
- """Clean up S3 files from prior failed runs for this partition.
-
- This prevents orphaned files from accumulating when runs fail partway through.
- Only deletes files that don't match the current run_id.
-
- Returns the number of files deleted.
- """
- import boto3
-
- is_local = DEBUG
- if is_local:
- context.log.info("Skipping cleanup in local mode")
- return 0
-
- bucket = get_ducklake_bucket()
- region = get_ducklake_region()
-
- year = partition_date.strftime("%Y")
- month = partition_date.strftime("%m")
- day = partition_date.strftime("%d")
-
- s3_client = boto3.client("s3", region_name=region)
- deleted_count = 0
-
- # List all objects under the backfill prefix for this date
- # We need to check each team_id partition
- prefix = f"{BACKFILL_S3_PREFIX}/"
-
- paginator = s3_client.get_paginator("list_objects_v2")
- for page in paginator.paginate(Bucket=bucket, Prefix=prefix):
- for obj in page.get("Contents", []):
- key = obj["Key"]
- # Check if this file is for our partition date but not our run
- if f"/year={year}/month={month}/day={day}/" in key:
- if f"_run_{current_run_id}" not in key:
- context.log.info(f"Deleting orphaned file: {key}")
- logger.info("cleanup_orphaned_file", key=key, bucket=bucket)
- s3_client.delete_object(Bucket=bucket, Key=key)
- deleted_count += 1
-
- if deleted_count > 0:
- context.log.info(f"Cleaned up {deleted_count} orphaned files from prior runs")
- logger.info(
- "cleanup_complete",
- deleted_count=deleted_count,
- partition_date=partition_date.isoformat(),
- )
-
- return deleted_count
-
-
-def register_files_with_ducklake(
- context: AssetExecutionContext,
- s3_paths: list[str],
- config: EventsBackfillConfig,
-) -> int:
- """Register exported Parquet files with DuckLake using ducklake_add_data_files.
-
- Returns the number of files successfully registered.
- """
- if config.skip_ducklake_registration:
- context.log.info("Skipping DuckLake registration (skip_ducklake_registration=True)")
- return 0
-
- if not s3_paths:
- context.log.info("No files to register with DuckLake")
- return 0
-
- if config.dry_run:
- context.log.info(f"[DRY RUN] Would register {len(s3_paths)} files with DuckLake")
- return 0
-
- ducklake_config = get_config()
- storage_config = DuckLakeStorageConfig.from_runtime()
- alias = "ducklake"
-
- conn = duckdb.connect()
- registered_count = 0
-
- try:
- configure_connection(conn, storage_config)
-
- # Check if the DuckLake catalog is already attached in a robust way by querying DuckDB
- existing_catalogs = {
- row[0] for row in conn.execute("SELECT catalog_name FROM duckdb_catalog.get_catalogs()").fetchall()
- }
-
- if alias in existing_catalogs:
- context.log.info(f"DuckLake catalog '{alias}' already attached")
- else:
- attach_catalog(conn, ducklake_config, alias=alias)
-
- for s3_path in s3_paths:
- try:
- context.log.info(f"Registering file with DuckLake: {s3_path}")
- # Use escape() to prevent SQL injection
- conn.execute(
- f"CALL ducklake_add_data_files('{alias}', 'events', '{escape(s3_path)}', schema => 'posthog')"
- )
- registered_count += 1
- context.log.info(f"Successfully registered: {s3_path}")
- logger.info("ducklake_file_registered", s3_path=s3_path)
- except Exception:
- context.log.exception(f"Failed to register file {s3_path}")
- logger.exception("ducklake_file_registration_failed", s3_path=s3_path)
- raise
-
- finally:
- conn.close()
-
- context.log.info(f"Registered {registered_count}/{len(s3_paths)} files with DuckLake")
- logger.info(
- "ducklake_registration_complete",
- registered=registered_count,
- total=len(s3_paths),
- )
- return registered_count
-
-
-events_backfill_partitioned_config = PartitionedConfig(
- partitions_def=daily_partitions,
- run_config_for_partition_key_fn=lambda partition_key: {},
- tags_for_partition_key_fn=tags_for_events_partition,
-)
-
-
-@asset(
- partitions_def=daily_partitions,
- name="events_ducklake_backfill",
- backfill_policy=BackfillPolicy.multi_run(max_partitions_per_run=MAX_PARTITIONS_PER_RUN),
- tags={"owner": JobOwners.TEAM_DATA_STACK.value, **CONCURRENCY_TAG},
-)
-def events_ducklake_backfill(context: AssetExecutionContext, config: EventsBackfillConfig) -> None:
- """Backfill events from ClickHouse to DuckLake.
-
- This asset:
- 1. Validates DuckLake schema compatibility before starting
- 2. Cleans up orphaned files from prior failed runs
- 3. Exports events for the partition date from ClickHouse to S3 as Parquet
- 4. Registers the Parquet files with DuckLake using ducklake_add_data_files
-
- Events are chunked by team_id modulo and exported in parallel for better performance.
- """
- where_clause = get_partition_where_clause(context)
- partition_range = context.partition_key_range
- partition_range_str = f"{partition_range.start} to {partition_range.end}"
- partition_date = context.partition_time_window.start
- run_id = context.run.run_id[:8]
-
- context.log.info(f"Config: {config}")
- logger.info(
- "events_backfill_start",
- partition_range=partition_range_str,
- run_id=run_id,
- team_id_chunks=config.team_id_chunks,
- parallel_chunks=config.parallel_chunks,
- )
-
- # Log debug query URL for observability
- if debug_url := metabase_debug_query_url(context.run.run_id):
- context.log.info(f"Debug query URL: {debug_url}")
-
- # Validate DuckLake schema before starting export
- if not config.dry_run and not config.skip_ducklake_registration:
- context.log.info("Validating DuckLake schema compatibility...")
- validate_ducklake_schema(context)
-
- # Clean up orphaned files from prior failed runs
- if config.cleanup_prior_run_files and not config.dry_run:
- context.log.info("Cleaning up orphaned files from prior runs...")
- cleanup_prior_run_files(context, partition_date, run_id)
-
- merged_settings = DEFAULT_CLICKHOUSE_SETTINGS.copy()
- # Add query tagging for observability
- merged_settings.update(settings_with_log_comment(context))
- if config.clickhouse_settings:
- merged_settings.update(config.clickhouse_settings)
- context.log.info(f"Using custom ClickHouse settings: {config.clickhouse_settings}")
-
- team_id_chunks = max(1, config.team_id_chunks)
- parallel_chunks = min(max(1, config.parallel_chunks), team_id_chunks)
-
- context.log.info(
- f"Running events backfill for partitions {partition_range_str} "
- f"(where='{where_clause}') "
- f"with {team_id_chunks} team_id chunks, {parallel_chunks} parallel"
- )
-
- cluster = get_cluster()
- tags = dagster_tags(context)
- workload = Workload.OFFLINE if is_cloud() else Workload.DEFAULT
-
- all_s3_paths: list[str] = []
-
- def export_single_chunk(chunk_i: int) -> list[str]:
- """Export a single chunk with its own client connection.
-
- Each thread gets its own ClickHouse client via any_host_by_role() because
- clickhouse-driver is NOT thread-safe - concurrent queries from different
- threads cannot use the same Client instance.
- """
-
- def do_export(client: Client) -> list[str]:
- with tags_context(kind="dagster", dagster=tags):
- return export_events_to_s3(
- context=context,
- client=client,
- config=config,
- where_clause=where_clause,
- team_id_chunk=chunk_i,
- total_chunks=team_id_chunks,
- partition_date=partition_date,
- run_id=run_id,
- settings=merged_settings,
- )
-
- return cluster.any_host_by_role(
- fn=do_export,
- workload=workload,
- node_role=NodeRole.DATA,
- ).result()
-
- if parallel_chunks > 1:
- context.log.info(f"Exporting {team_id_chunks} chunks with {parallel_chunks} parallel workers")
- with ThreadPoolExecutor(max_workers=parallel_chunks) as executor:
- futures = {executor.submit(export_single_chunk, i): i for i in range(team_id_chunks)}
- for future in as_completed(futures):
- chunk_i = futures[future]
- try:
- paths = future.result()
- all_s3_paths.extend(paths)
- context.log.info(f"Completed chunk {chunk_i + 1}/{team_id_chunks}")
- except Exception:
- context.log.exception(f"Chunk {chunk_i + 1}/{team_id_chunks} failed")
- raise
- else:
- for chunk_i in range(team_id_chunks):
- context.log.info(f"Processing chunk {chunk_i + 1}/{team_id_chunks}")
- paths = export_single_chunk(chunk_i)
- all_s3_paths.extend(paths)
- context.log.info(f"Completed chunk {chunk_i + 1}/{team_id_chunks}")
-
- context.log.info(f"Exported {len(all_s3_paths)} files to S3")
- logger.info("export_complete", files_exported=len(all_s3_paths))
-
- registered_count = register_files_with_ducklake(context, all_s3_paths, config)
-
- context.add_output_metadata(
- {
- "partition_date": partition_range_str,
- "team_id_chunks": team_id_chunks,
- "parallel_chunks": parallel_chunks,
- "files_exported": len(all_s3_paths),
- "files_registered": registered_count,
- "s3_paths": dagster.MetadataValue.json(all_s3_paths[:10]),
- }
- )
-
- context.log.info(
- f"Successfully backfilled events for partitions {partition_range_str}: "
- f"{len(all_s3_paths)} files exported, {registered_count} files registered with DuckLake"
- )
- logger.info(
- "events_backfill_complete",
- partition_range=partition_range_str,
- files_exported=len(all_s3_paths),
- files_registered=registered_count,
- )
-
-
-events_ducklake_backfill_job = define_asset_job(
- name="events_ducklake_backfill_job",
- selection=["events_ducklake_backfill"],
- config=events_backfill_partitioned_config,
- tags={
- "owner": JobOwners.TEAM_DATA_STACK.value,
- "disable_slack_notifications": True, # Squelch notifications until this job is fully in production
- **CONCURRENCY_TAG,
- },
-)
diff --git a/posthog/dags/events_backfill_to_duckling.py b/posthog/dags/events_backfill_to_duckling.py
index 318b306e8710..364a6db91028 100644
--- a/posthog/dags/events_backfill_to_duckling.py
+++ b/posthog/dags/events_backfill_to_duckling.py
@@ -4,9 +4,8 @@ Dagster job to backfill ClickHouse events to customer-specific ducklings.
This job exports events from ClickHouse's `posthog.events` table to customer S3 buckets
as Parquet files, then registers those files with their DuckLake catalog.
-Unlike the main DuckLake backfill (events_backfill_to_ducklake.py) which targets PostHog's
-shared DuckLake, this job targets individual customer "ducklings" - isolated DuckLake
-instances with their own RDS catalog and S3 bucket.
+This job targets individual customer "ducklings" - isolated DuckLake instances with their
+own RDS catalog and S3 bucket.
Architecture:
DuckLakeCatalog (Django model)
@@ -66,11 +65,6 @@ from posthog.clickhouse.cluster import ClickhouseCluster, get_cluster
from posthog.clickhouse.query_tagging import tags_context
from posthog.cloud_utils import is_cloud
from posthog.dags.common import JobOwners, dagster_tags, settings_with_log_comment
-from posthog.dags.events_backfill_to_ducklake import (
- DEFAULT_CLICKHOUSE_SETTINGS,
- EXPECTED_DUCKLAKE_COLUMNS,
- MAX_RETRY_ATTEMPTS,
-)
from posthog.ducklake.client import make_duckgres_conninfo
from posthog.ducklake.common import get_duckgres_server_for_organization, get_ducklake_catalog_by_team_org
from posthog.ducklake.models import DuckLakeBackfill, DuckLakeCatalog
@@ -82,6 +76,17 @@ logger = structlog.get_logger(__name__)
# so the DAG can hardcode it everywhere instead of threading a config value.
DUCKLAKE_ALIAS = "ducklake"
+MAX_RETRY_ATTEMPTS = 3
+
+ONE_HOUR_IN_SECONDS = 60 * 60
+ONE_GB_IN_BYTES = 1024 * 1024 * 1024
+
+DEFAULT_CLICKHOUSE_SETTINGS = {
+ "max_execution_time": 4 * ONE_HOUR_IN_SECONDS,
+ "max_memory_usage": 50 * ONE_GB_IN_BYTES,
+ "distributed_aggregation_memory_efficient": "1",
+}
+
# Duckgres connect timeout bounds the TCP+TLS handshake only. A backfill
# connection may have to wait for duckgres to spin up a fresh worker (a cold
# worker can require provisioning a new node, which takes minutes), so the
@@ -386,6 +391,35 @@ EVENTS_COLUMNS = """
now64(6) as _inserted_at
"""
+# Expected columns in the duckling's events table for schema validation
+EXPECTED_DUCKLAKE_EVENTS_COLUMNS = {
+ "uuid",
+ "event",
+ "properties",
+ "timestamp",
+ "team_id",
+ "project_id",
+ "distinct_id",
+ "elements_chain",
+ "created_at",
+ "person_id",
+ "person_created_at",
+ "person_properties",
+ "group0_properties",
+ "group1_properties",
+ "group2_properties",
+ "group3_properties",
+ "group4_properties",
+ "group0_created_at",
+ "group1_created_at",
+ "group2_created_at",
+ "group3_created_at",
+ "group4_created_at",
+ "person_mode",
+ "historical_migration",
+ "_inserted_at",
+}
+
BACKFILL_EVENTS_S3_PREFIX = "backfill/events"
BACKFILL_PERSONS_S3_PREFIX = "backfill/persons"
@@ -893,7 +927,7 @@ def validate_duckling_schema(
cur.execute(f"DESCRIBE {alias}.posthog.events")
ducklake_columns = {row[0] for row in cur.fetchall()}
- missing_in_ducklake = EXPECTED_DUCKLAKE_COLUMNS - ducklake_columns
+ missing_in_ducklake = EXPECTED_DUCKLAKE_EVENTS_COLUMNS - ducklake_columns
if missing_in_ducklake:
context.log.warning(
f"Duckling events table is missing columns that we export: {missing_in_ducklake}. "
@@ -906,19 +940,19 @@ def validate_duckling_schema(
missing_columns=list(missing_in_ducklake),
)
- extra_in_ducklake = ducklake_columns - EXPECTED_DUCKLAKE_COLUMNS
+ extra_in_ducklake = ducklake_columns - EXPECTED_DUCKLAKE_EVENTS_COLUMNS
if extra_in_ducklake:
context.log.info(f"Duckling has additional columns not in our export: {extra_in_ducklake}")
context.log.info(
f"Schema validation passed. Duckling has {len(ducklake_columns)} columns, "
- f"we export {len(EXPECTED_DUCKLAKE_COLUMNS)} columns."
+ f"we export {len(EXPECTED_DUCKLAKE_EVENTS_COLUMNS)} columns."
)
logger.info(
"duckling_schema_validation_passed",
team_id=catalog.team_id,
ducklake_columns=len(ducklake_columns),
- export_columns=len(EXPECTED_DUCKLAKE_COLUMNS),
+ export_columns=len(EXPECTED_DUCKLAKE_EVENTS_COLUMNS),
)
diff --git a/posthog/dags/locations/data_stack.py b/posthog/dags/locations/data_stack.py
index 36fb70aebb5a..1bf885c487bd 100644
--- a/posthog/dags/locations/data_stack.py
+++ b/posthog/dags/locations/data_stack.py
@@ -1,6 +1,6 @@
import dagster
-from posthog.dags import events_backfill_to_ducklake, events_backfill_to_duckling
+from posthog.dags import events_backfill_to_duckling
from products.data_warehouse.dags import managed_viewset_sync
@@ -8,13 +8,11 @@ from . import loggers, resources
defs = dagster.Definitions(
assets=[
- events_backfill_to_ducklake.events_ducklake_backfill,
events_backfill_to_duckling.duckling_events_backfill,
events_backfill_to_duckling.duckling_persons_backfill,
],
jobs=[
managed_viewset_sync.sync_managed_viewsets_job,
- events_backfill_to_ducklake.events_ducklake_backfill_job,
events_backfill_to_duckling.duckling_events_backfill_job,
events_backfill_to_duckling.duckling_persons_backfill_job,
],
diff --git a/posthog/dags/test_events_backfill_to_duckling.py b/posthog/dags/test_events_backfill_to_duckling.py
index 5bd84239d73a..267279d5fb61 100644
--- a/posthog/dags/test_events_backfill_to_duckling.py
+++ b/posthog/dags/test_events_backfill_to_duckling.py
@@ -14,7 +14,7 @@ from posthog.dags.events_backfill_to_duckling import (
EVENTS_COLUMNS,
EVENTS_CONCURRENCY_TAG,
EVENTS_TABLE_DDL,
- EXPECTED_DUCKLAKE_COLUMNS,
+ EXPECTED_DUCKLAKE_EVENTS_COLUMNS,
EXPECTED_DUCKLAKE_PERSONS_COLUMNS,
PERSONS_COLUMNS,
PERSONS_CONCURRENCY_TAG,
@@ -159,7 +159,7 @@ class TestEventsDDL:
result = conn.execute("DESCRIBE memory.posthog.events").fetchall()
column_names = {row[0] for row in result}
- assert column_names == EXPECTED_DUCKLAKE_COLUMNS
+ assert column_names == EXPECTED_DUCKLAKE_EVENTS_COLUMNS
conn.close()
def test_events_ddl_is_idempotent(self):
diff --git a/posthog/dags/tests/test_events_backfill_to_ducklake.py b/posthog/dags/tests/test_events_backfill_to_ducklake.py
deleted file mode 100644
index 8046f5a3de51..000000000000
--- a/posthog/dags/tests/test_events_backfill_to_ducklake.py
+++ /dev/null
@@ -1,195 +0,0 @@
-from datetime import datetime
-
-from unittest.mock import MagicMock, patch
-
-from parameterized import parameterized
-
-from posthog.dags.events_backfill_to_ducklake import (
- EVENTS_COLUMNS,
- EXPECTED_DUCKLAKE_COLUMNS,
- get_partition_where_clause,
- get_s3_function_args,
- get_s3_path_for_partition,
- tags_for_events_partition,
-)
-
-
-class TestTagsForEventsPartition:
- @parameterized.expand(
- [
- ("2025-01-15", {"events_ducklake_partition": "dl_20250115"}),
- ("2025-10-31", {"events_ducklake_partition": "dl_20251031"}),
- ("2025-11-01", {"events_ducklake_partition": "dl_20251101"}),
- ("2019-01-01", {"events_ducklake_partition": "dl_20190101"}),
- ]
- )
- def test_tags_for_events_partition(self, partition_key, expected_tags):
- result = tags_for_events_partition(partition_key)
- assert result == expected_tags
-
-
-class TestGetS3PathForPartition:
- @parameterized.expand(
- [
- (
- "posthog-ducklake-dev",
- "us-east-1",
- "mod64eq0",
- datetime(2025, 1, 15),
- "chunk_0000_run_abc12345",
- False,
- "https://posthog-ducklake-dev.s3.us-east-1.amazonaws.com/backfill/events/mod64eq0/2025/01/15/chunk_0000_run_abc12345.parquet",
- ),
- (
- "posthog-ducklake-prod-eu",
- "eu-central-1",
- "mod64eq63",
- datetime(2025, 12, 31),
- "chunk_0063_run_xyz98765",
- False,
- "https://posthog-ducklake-prod-eu.s3.eu-central-1.amazonaws.com/backfill/events/mod64eq63/2025/12/31/chunk_0063_run_xyz98765.parquet",
- ),
- ]
- )
- def test_get_s3_path_for_partition_prod(
- self,
- bucket,
- region,
- team_id,
- date,
- chunk_id,
- is_local,
- expected_path,
- ):
- result = get_s3_path_for_partition(
- bucket=bucket,
- region=region,
- team_id=team_id,
- date=date,
- chunk_id=chunk_id,
- is_local=is_local,
- )
- assert result == expected_path
-
- def test_get_s3_path_for_partition_local(self):
- with patch(
- "posthog.dags.events_backfill_to_ducklake.OBJECT_STORAGE_ENDPOINT",
- "http://localhost:19000",
- ):
- result = get_s3_path_for_partition(
- bucket="posthog-ducklake-dev",
- region="us-east-1",
- team_id="mod64eq0",
- date=datetime(2025, 1, 15),
- chunk_id="chunk_0000_run_abc12345",
- is_local=True,
- )
- assert (
- result
- == "http://localhost:19000/posthog-ducklake-dev/backfill/events/mod64eq0/2025/01/15/chunk_0000_run_abc12345.parquet"
- )
-
-
-class TestGetS3FunctionArgs:
- def test_production_mode(self):
- args, safe_args = get_s3_function_args(
- "https://bucket.s3.us-east-1.amazonaws.com/path/file.parquet",
- is_local=False,
- )
- assert args == "'https://bucket.s3.us-east-1.amazonaws.com/path/file.parquet', 'Parquet'"
- assert safe_args == args
-
- def test_local_mode_redacts_credentials(self):
- with patch(
- "posthog.dags.events_backfill_to_ducklake.OBJECT_STORAGE_ACCESS_KEY_ID",
- "test_key_id",
- ):
- with patch(
- "posthog.dags.events_backfill_to_ducklake.OBJECT_STORAGE_SECRET_ACCESS_KEY",
- "test_secret",
- ):
- args, safe_args = get_s3_function_args(
- "http://localhost:19000/bucket/path/file.parquet",
- is_local=True,
- )
- assert "test_key_id" in args
- assert "test_secret" in args
- assert "[REDACTED]" in safe_args
- assert "test_key_id" not in safe_args
- assert "test_secret" not in safe_args
-
-
-class TestGetPartitionWhereClause:
- def test_generates_correct_where_clause(self):
- mock_context = MagicMock()
- mock_context.partition_time_window.start = datetime(2025, 1, 15)
- mock_context.partition_time_window.end = datetime(2025, 1, 16)
-
- result = get_partition_where_clause(mock_context)
-
- assert result == "toDate(timestamp) >= '2025-01-15' AND toDate(timestamp) < '2025-01-16'"
-
- def test_custom_timestamp_field(self):
- mock_context = MagicMock()
- mock_context.partition_time_window.start = datetime(2025, 1, 15)
- mock_context.partition_time_window.end = datetime(2025, 1, 16)
-
- result = get_partition_where_clause(mock_context, timestamp_field="created_at")
-
- assert result == "toDate(created_at) >= '2025-01-15' AND toDate(created_at) < '2025-01-16'"
-
-
-class TestEventsColumnsSchema:
- def test_events_columns_has_expected_columns(self):
- columns_in_sql = set()
- for col in EVENTS_COLUMNS.strip().split("\n"):
- if col.strip():
- # Handle columns with 'AS' alias (e.g., "toString(uuid) as uuid")
- if " as " in col.lower():
- # Extract the alias after 'as'
- alias = col.lower().split(" as ")[1].split(",")[0].strip()
- columns_in_sql.add(alias)
- else:
- # No alias, use the column name directly
- columns_in_sql.add(col.strip().split()[0].rstrip(","))
-
- assert "uuid" in columns_in_sql
- assert "event" in columns_in_sql
- assert "properties" in columns_in_sql
- assert "timestamp" in columns_in_sql
- assert "team_id" in columns_in_sql
- assert "project_id" in columns_in_sql
- assert "distinct_id" in columns_in_sql
- assert "person_id" in columns_in_sql
- assert "person_mode" in columns_in_sql
- assert "historical_migration" in columns_in_sql
-
- def test_expected_ducklake_columns_matches_export(self):
- export_columns = {
- "uuid",
- "event",
- "properties",
- "timestamp",
- "team_id",
- "project_id",
- "distinct_id",
- "elements_chain",
- "created_at",
- "person_id",
- "person_created_at",
- "person_properties",
- "group0_properties",
- "group1_properties",
- "group2_properties",
- "group3_properties",
- "group4_properties",
- "group0_created_at",
- "group1_created_at",
- "group2_created_at",
- "group3_created_at",
- "group4_created_at",
- "person_mode",
- "historical_migration",
- "_inserted_at",
- }
- assert EXPECTED_DUCKLAKE_COLUMNS == export_columns