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.

fix(data-warehouse): partition matomo visits by month, not per row (#71931)

Luke Beltonaf731094f249
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/matomo/matomo.py b/products/warehouse_sources/backend/temporal/data_imports/sources/matomo/matomo.py
index 9ddbd9338ddc..d0e293a4f29c 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/matomo/matomo.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/matomo/matomo.py
@@ -274,8 +274,9 @@ def matomo_source(
db_incremental_field_last_value=db_incremental_field_last_value,
),
primary_keys=list(config.primary_keys),
- partition_count=1,
- partition_size=1,
+ partition_mode=config.partition_mode,
+ partition_keys=config.partition_keys,
+ partition_format=config.partition_format,
# Visits are fetched ascending by serverTimestamp; report days are
# walked oldest-first — both cursors only move forward.
sort_mode="asc",
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/matomo/settings.py b/products/warehouse_sources/backend/temporal/data_imports/sources/matomo/settings.py
index c32d67d99dff..07150254446f 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/matomo/settings.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/matomo/settings.py
@@ -1,6 +1,10 @@
from dataclasses import dataclass, field
-from typing import Literal
+from typing import Literal, Optional
+from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline.typings import (
+ PartitionFormat,
+ PartitionMode,
+)
from products.warehouse_sources.backend.types import IncrementalField, IncrementalFieldType
# Reports for recent days change until archiving completes, so incremental
@@ -33,6 +37,12 @@ class MatomoEndpointConfig:
method: str
primary_keys: list[str] = field(default_factory=lambda: ["id"])
incremental_fields: list[IncrementalField] = field(default_factory=list)
+ # Delta partitioning for this endpoint. Left unset (None) means the table is written
+ # unpartitioned — correct for the tiny per-day report tables. `visits` sets a datetime
+ # scheme so merges only touch recent partitions instead of one partition per row.
+ partition_mode: Optional[PartitionMode] = None
+ partition_keys: Optional[list[str]] = None
+ partition_format: Optional[PartitionFormat] = None
MATOMO_ENDPOINTS: dict[str, MatomoEndpointConfig] = {
@@ -51,6 +61,14 @@ MATOMO_ENDPOINTS: dict[str, MatomoEndpointConfig] = {
"field_type": IncrementalFieldType.Numeric,
},
],
+ # Bucket by month of the visit's server timestamp. idVisit is an incrementing integer,
+ # so without this the pipeline auto-selects numerical mode and (with the old
+ # partition_size=1) created one Delta partition per visit. Month is the coarsest tier;
+ # the auto-repartitioner only steps finer (month -> week -> day -> hour), so starting
+ # here gives it the most headroom if a partition ever outgrows the byte budget.
+ partition_mode="datetime",
+ partition_keys=["serverTimestamp"],
+ partition_format="month",
),
# Per-day aggregate reports, date-partitioned via period=day&date=...
# with the day injected as `_date`.
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/matomo/tests/test_matomo.py b/products/warehouse_sources/backend/temporal/data_imports/sources/matomo/tests/test_matomo.py
index 66413f605ca0..f1d0e87d6856 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/matomo/tests/test_matomo.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/matomo/tests/test_matomo.py
@@ -283,3 +283,23 @@ class TestMatomoSourceResponse:
assert response.name == endpoint
assert response.primary_keys == config.primary_keys
assert response.sort_mode == "asc"
+
+ @pytest.mark.parametrize(
+ "endpoint,expected_mode,expected_keys,expected_format",
+ [
+ # visits' primary key idVisit is an incrementing int, so it must ship an explicit
+ # datetime scheme — otherwise the pipeline buckets one Delta partition per visit.
+ ("visits", "datetime", ["serverTimestamp"], "month"),
+ ("visits_summary", None, None, None),
+ ("referrers", None, None, None),
+ ],
+ )
+ def test_partition_scheme_per_endpoint(self, endpoint, expected_mode, expected_keys, expected_format):
+ response = matomo_source("https://m.example.com", "1", "token", endpoint, mock.MagicMock(), _make_manager())
+
+ assert response.partition_mode == expected_mode
+ assert response.partition_keys == expected_keys
+ assert response.partition_format == expected_format
+ # partition_size=1 is what caused the per-row explosion; it must never come back.
+ assert response.partition_size is None
+ assert response.partition_count is None