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): rescue a skewed datetime table stuck at its finest tier (#92920)

Tom Owers45f532e02dca
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/repartition.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/repartition.py
index 76818ffdc9f6..19efdceeaa95 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/repartition.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/repartition.py
@@ -538,8 +538,9 @@ def select_repartition_target(
one format tier finer. An unpartitioned table gets an auto target, which sizes its bucket count
the same way so md5 is reachable whatever its keys turn out to be. When no target is chosen the
reason explains why (reported in metrics so a skipped table is diagnosable): `within_budget`,
- `datetime_at_finest_tier`, `numerical_cannot_shrink`, `numerical_no_size`, or
- `unpartitionable_no_keys`. A chosen target carries reason `selected`.
+ `datetime_at_finest_tier` (only when there is no primary key distinct from the partition key to
+ hash — otherwise a datetime table out of tiers falls back to md5), `numerical_cannot_shrink`,
+ `numerical_no_size`, or `unpartitionable_no_keys`. A chosen target carries reason `selected`.
"""
if not partition_bytes:
return None, "no_partitions"
@@ -580,8 +581,25 @@ def select_repartition_target(
# no-op'd to `hour` before the ceiling existed) has nothing finer to gain either.
ceiling_index = DATETIME_FORMAT_TIERS.index(_datetime_tier_ceiling(schema))
if current_index >= ceiling_index:
- # Already at the finest usable tier — can't go finer. Caller alerts.
- return None, "datetime_at_finest_tier"
+ # No finer tier left, yet the table is still over budget. The key itself is skewed — a
+ # backfill that stamped one timestamp across millions of rows puts more than the budget
+ # in a single bucket, and splitting time more finely cannot divide rows that share a
+ # value. md5 escapes that by bucketing on a hash rather than on the value, so partition
+ # size follows the row count. It must hash the PRIMARY KEY, not the datetime partition
+ # key: hashing the skewed column maps every row that shares a timestamp to the same
+ # bucket and reproduces the skew exactly. The cost is time-range pruning at query time,
+ # which an over-budget partition already outweighs by OOMing the merge and stalling the
+ # sync. With no distinct primary key to hash there is nothing better to move to, so the
+ # table stays parked at the finest tier for the caller to alert on.
+ hash_keys = schema.primary_key_columns or []
+ if not hash_keys or set(hash_keys) == set(keys):
+ return None, "datetime_at_finest_tier"
+ return RepartitionTarget(
+ partition_keys=hash_keys,
+ trigger_reason="",
+ partition_mode="md5",
+ partition_count=max(1, math.ceil(total_bytes / target_partition_bytes)),
+ ), "selected"
return RepartitionTarget(
partition_keys=keys,
trigger_reason="",
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test_repartition.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test_repartition.py
index dc5af9566764..369f78c2c962 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test_repartition.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test_repartition.py
@@ -228,6 +228,40 @@ class TestSelectRepartitionTarget:
1000,
{"partition_mode": "datetime", "partition_format": "hour"},
),
+ # Out of datetime tiers and still over budget: a skewed key (one timestamp across many
+ # rows) can't be split by time, so the table switches to hashed buckets rather than
+ # staying over budget forever and OOMing every merge. The hash is over the primary key —
+ # hashing the skewed datetime key would put every row sharing a timestamp in one bucket.
+ (
+ "datetime_at_finest_tier_falls_back_to_md5_on_primary_key",
+ {
+ "partition_mode": "datetime",
+ "partition_format": "hour",
+ "partitioning_keys": ["created_at"],
+ "primary_key_columns": ["subscription_id", "valid_from"],
+ },
+ {"2024-01-01T00": 5000},
+ 1000,
+ {
+ "partition_mode": "md5",
+ "partition_keys": ["subscription_id", "valid_from"],
+ "partition_count": 5,
+ },
+ ),
+ # A date-typed key caps at `day`, so it reaches the same fallback one tier earlier.
+ (
+ "date_typed_key_at_day_ceiling_falls_back_to_md5",
+ {
+ "partition_mode": "datetime",
+ "partition_format": "day",
+ "partitioning_keys": ["report_date"],
+ "primary_key_columns": ["id"],
+ "schema_metadata": {"columns": [{"name": "report_date", "data_type": "date32[day]"}]},
+ },
+ {"2024-01-01": 5000},
+ 1000,
+ {"partition_mode": "md5", "partition_keys": ["id"], "partition_count": 5},
+ ),
(
"unpartitioned_with_keys_enables_partitioning",
{"partition_mode": None, "primary_key_columns": ["id"]},
@@ -260,6 +294,17 @@ class TestSelectRepartitionTarget:
@parameterized.expand(
[
("datetime_hour", {"partition_mode": "datetime", "partition_format": "hour"}, "datetime_at_finest_tier"),
+ # Hashing the partition key itself would rebuild the same skew, so this is still a skip.
+ (
+ "datetime_hour_primary_key_is_the_skewed_key",
+ {
+ "partition_mode": "datetime",
+ "partition_format": "hour",
+ "partitioning_keys": ["created_at"],
+ "primary_key_columns": ["created_at"],
+ },
+ "datetime_at_finest_tier",
+ ),
("unpartitionable", {"partition_mode": None}, "unpartitionable_no_keys"),
]
)