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(logs): use per-partition checkpoint in log list query (#62695)

Frank Hamand977adfecb0a7
diff --git a/products/logs/backend/alert_check_query.py b/products/logs/backend/alert_check_query.py
index d357ee0e4c21..451d81f5714f 100644
--- a/products/logs/backend/alert_check_query.py
+++ b/products/logs/backend/alert_check_query.py
@@ -25,7 +25,7 @@ from posthog.hogql_queries.utils.query_date_range import QueryDateRange
from posthog.models import Team
from products.logs.backend.alert_utils import MAX_BYTES_TO_READ
-from products.logs.backend.logs_query_runner import LogsFilterBuilder
+from products.logs.backend.logs_query_runner import LIVE_LOGS_CHECKPOINT_QUERY, LogsFilterBuilder
from products.logs.backend.models import LogsAlertConfiguration
@@ -505,18 +505,6 @@ class BatchedAlertCheckQuery:
)
-# Explicit per-partition `GROUP BY` is required on both the dev `MergeTree`
-# shard (no auto-merge at all) and the prod `AggregatingMergeTree` between
-# merges. A bare `min(max_observed_timestamp)` scans every raw insert and
-# returns the oldest value ever written.
-_LIVE_LOGS_CHECKPOINT_SQL = """
- SELECT min(partition_checkpoint) FROM (
- SELECT _topic, _partition, max(max_observed_timestamp) AS partition_checkpoint
- FROM logs_kafka_metrics
- GROUP BY _topic, _partition
- )
-"""
-
# Fall back to `now` when the checkpoint is older than this — a quiet partition
# can pin `min(...)` hours behind while other partitions have fresh data.
CHECKPOINT_MAX_STALENESS = dt.timedelta(minutes=5)
@@ -531,7 +519,7 @@ def fetch_live_logs_checkpoint(team: Team) -> dt.datetime | None:
)
response = execute_hogql_query(
query_type="alert_check_checkpoint",
- query=parse_select(_LIVE_LOGS_CHECKPOINT_SQL),
+ query=LIVE_LOGS_CHECKPOINT_QUERY,
team=team,
workload=Workload.LOGS,
modifiers=HogQLQueryModifiers(convertToProjectTimezone=False),
diff --git a/products/logs/backend/logs_query_runner.py b/products/logs/backend/logs_query_runner.py
index 99163dc13b30..5fb497a87fcc 100644
--- a/products/logs/backend/logs_query_runner.py
+++ b/products/logs/backend/logs_query_runner.py
@@ -31,6 +31,17 @@ if TYPE_CHECKING:
from posthog.models import Team, User
+LIVE_LOGS_CHECKPOINT_QUERY = parse_select(
+ """
+ SELECT min(partition_checkpoint) FROM (
+ SELECT _topic, _partition, max(max_observed_timestamp) AS partition_checkpoint
+ FROM logs_kafka_metrics
+ GROUP BY _topic, _partition
+ )
+"""
+)
+
+
def _trace_id_normalise_to_base64(value: str) -> str:
"""Accept either hex or base64 encoded trace_id/span_id values.
@@ -558,12 +569,13 @@ class LogsQueryRunner(AnalyticsQueryRunner[LogsQueryResponse], LogsQueryRunnerMi
resource_fingerprint,
instrumentation_scope,
event_name,
- (select min(max_observed_timestamp) from logs_kafka_metrics) as live_logs_checkpoint
+ {live_logs_checkpoint} as live_logs_checkpoint
FROM logs
WHERE {where}
""",
placeholders={
"where": self.where(),
+ "live_logs_checkpoint": LIVE_LOGS_CHECKPOINT_QUERY,
# Attribute maps dominate payload size. When excluded we still SELECT a column
# (an empty map) so the positional result mapping in _calculate stays stable.
"attributes": parse_expr("map() AS attributes" if self.query.excludeAttributes else "attributes"),