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(billing): prune usage report query-log scans (#63928)

Pawel Cebulaf618c0a2019f
diff --git a/posthog/tasks/test/__snapshots__/test_usage_report.ambr b/posthog/tasks/test/__snapshots__/test_usage_report.ambr
index c2c8e72c07bd..00f8956078b1 100644
--- a/posthog/tasks/test/__snapshots__/test_usage_report.ambr
+++ b/posthog/tasks/test/__snapshots__/test_usage_report.ambr
@@ -682,6 +682,8 @@
WHERE (type = 'QueryFinish'
OR type = 'ExceptionWhileProcessing')
AND is_initial_query = 1
+ AND event_time >= 'today 00:00:00'
+ AND event_time < '2022-01-11 05:59:59'
AND query_start_time >= '2022-01-10 00:00:00'
AND query_start_time < '2022-01-10 23:59:59'
AND access_method = ''
@@ -715,6 +717,8 @@
WHERE (type = 'QueryFinish'
OR type = 'ExceptionWhileProcessing')
AND is_initial_query = 1
+ AND event_time >= 'today 00:00:00'
+ AND event_time < '2022-01-11 05:59:59'
AND query_start_time >= '2022-01-10 00:00:00'
AND query_start_time < '2022-01-10 23:59:59'
AND access_method = ''
@@ -732,6 +736,8 @@
WHERE (type = 'QueryFinish'
OR type = 'ExceptionWhileProcessing')
AND is_initial_query = 1
+ AND event_time >= 'today 00:00:00'
+ AND event_time < '2022-01-11 05:59:59'
AND query_start_time >= '2022-01-10 00:00:00'
AND query_start_time < '2022-01-10 23:59:59'
AND access_method = ''
@@ -749,6 +755,8 @@
WHERE (type = 'QueryFinish'
OR type = 'ExceptionWhileProcessing')
AND is_initial_query = 1
+ AND event_time >= 'today 00:00:00'
+ AND event_time < '2022-01-11 05:59:59'
AND query_start_time >= '2022-01-10 00:00:00'
AND query_start_time < '2022-01-10 23:59:59'
AND access_method = 'personal_api_key'
@@ -766,6 +774,8 @@
WHERE (type = 'QueryFinish'
OR type = 'ExceptionWhileProcessing')
AND is_initial_query = 1
+ AND event_time >= 'today 00:00:00'
+ AND event_time < '2022-01-11 05:59:59'
AND query_start_time >= '2022-01-10 00:00:00'
AND query_start_time < '2022-01-10 23:59:59'
AND access_method = 'personal_api_key'
@@ -783,6 +793,8 @@
WHERE (type = 'QueryFinish'
OR type = 'ExceptionWhileProcessing')
AND is_initial_query = 1
+ AND event_time >= 'today 00:00:00'
+ AND event_time < '2022-01-11 05:59:59'
AND query_start_time >= '2022-01-10 00:00:00'
AND query_start_time < '2022-01-10 23:59:59'
AND access_method = 'personal_api_key'
@@ -801,6 +813,8 @@
OR type = 'ExceptionWhileProcessing')
AND is_initial_query = 1
AND query_type IN (['EventsQuery'])
+ AND event_time >= 'today 00:00:00'
+ AND event_time < '2022-01-11 05:59:59'
AND query_start_time >= '2022-01-10 00:00:00'
AND query_start_time < '2022-01-10 23:59:59'
AND access_method = ''
@@ -819,6 +833,8 @@
OR type = 'ExceptionWhileProcessing')
AND is_initial_query = 1
AND query_type IN (['EventsQuery'])
+ AND event_time >= 'today 00:00:00'
+ AND event_time < '2022-01-11 05:59:59'
AND query_start_time >= '2022-01-10 00:00:00'
AND query_start_time < '2022-01-10 23:59:59'
AND access_method = ''
@@ -837,6 +853,8 @@
OR type = 'ExceptionWhileProcessing')
AND is_initial_query = 1
AND query_type IN (['EventsQuery'])
+ AND event_time >= 'today 00:00:00'
+ AND event_time < '2022-01-11 05:59:59'
AND query_start_time >= '2022-01-10 00:00:00'
AND query_start_time < '2022-01-10 23:59:59'
AND access_method = ''
@@ -855,6 +873,8 @@
OR type = 'ExceptionWhileProcessing')
AND is_initial_query = 1
AND query_type IN (['EventsQuery'])
+ AND event_time >= 'today 00:00:00'
+ AND event_time < '2022-01-11 05:59:59'
AND query_start_time >= '2022-01-10 00:00:00'
AND query_start_time < '2022-01-10 23:59:59'
AND access_method = 'personal_api_key'
@@ -873,6 +893,8 @@
OR type = 'ExceptionWhileProcessing')
AND is_initial_query = 1
AND query_type IN (['EventsQuery'])
+ AND event_time >= 'today 00:00:00'
+ AND event_time < '2022-01-11 05:59:59'
AND query_start_time >= '2022-01-10 00:00:00'
AND query_start_time < '2022-01-10 23:59:59'
AND access_method = 'personal_api_key'
@@ -907,6 +929,8 @@
OR type = 'ExceptionWhileProcessing')
AND is_initial_query = 1
AND query_type IN (['EventsQuery'])
+ AND event_time >= 'today 00:00:00'
+ AND event_time < '2022-01-11 05:59:59'
AND query_start_time >= '2022-01-10 00:00:00'
AND query_start_time < '2022-01-10 23:59:59'
AND access_method = 'personal_api_key'
diff --git a/posthog/tasks/test/test_usage_report.py b/posthog/tasks/test/test_usage_report.py
index 2f9e592005fa..cc48a9e36d52 100644
--- a/posthog/tasks/test/test_usage_report.py
+++ b/posthog/tasks/test/test_usage_report.py
@@ -59,6 +59,7 @@ from posthog.tasks.usage_report import (
_get_teams_for_usage_reports,
capture_event,
get_instance_metadata,
+ get_teams_with_query_metric,
has_non_zero_usage,
send_all_org_usage_reports,
)
@@ -1339,6 +1340,30 @@ class TestHogQLUsageReport(APIBaseTest, ClickhouseTestMixin, ClickhouseDestroyTa
assert report.api_queries_bytes_read > 16000 # locally it's about 16753
+class TestQueryUsageReportSQL:
+ @patch("posthog.tasks.usage_report.sync_execute", return_value=[(1, 100)])
+ def test_get_teams_with_query_metric_uses_event_time_pruning_window(self, mock_sync_execute: MagicMock) -> None:
+ begin = datetime(2026, 6, 15, tzinfo=tzutc())
+ end = begin + timedelta(days=1)
+
+ result = get_teams_with_query_metric(
+ begin=begin,
+ end=end,
+ query_types=["EventsQuery"],
+ access_method="personal_api_key",
+ metric="read_bytes",
+ )
+
+ assert result == [(1, 100)]
+ query = mock_sync_execute.call_args.args[0]
+ params = mock_sync_execute.call_args.args[1]
+ assert "AND event_time >= %(begin)s AND event_time < %(event_time_end)s" in query
+ assert "AND query_start_time >= %(begin)s AND query_start_time < %(end)s" in query
+ assert params["begin"] == begin
+ assert params["end"] == end
+ assert params["event_time_end"] == end + timedelta(hours=6)
+
+
@freeze_time("2022-01-10T00:01:00Z")
class TestFeatureFlagsUsageReport(ClickhouseDestroyTablesMixin, TestCase, ClickhouseTestMixin):
def setUp(self) -> None:
diff --git a/posthog/tasks/usage_report.py b/posthog/tasks/usage_report.py
index 9cde9a6fc470..d773db09c4a6 100644
--- a/posthog/tasks/usage_report.py
+++ b/posthog/tasks/usage_report.py
@@ -933,6 +933,7 @@ def get_teams_with_query_metric(
# :TRICKY: Inlined into the query below.
raise ValueError(f"Invalid metric {metric}")
+ event_time_end = end + timedelta(hours=6)
query_types_clause = "AND query_type IN (%(query_types)s)" if query_types and len(query_types) > 0 else ""
query = f"""
@@ -944,6 +945,8 @@ def get_teams_with_query_metric(
WHERE (type = 'QueryFinish' OR type = 'ExceptionWhileProcessing')
AND is_initial_query = 1
{query_types_clause}
+ -- event_time is part of query_log's primary key; query_start_time preserves billing attribution.
+ AND event_time >= %(begin)s AND event_time < %(event_time_end)s
AND query_start_time >= %(begin)s AND query_start_time < %(end)s
AND access_method = %(access_method)s
GROUP BY team_id
@@ -954,6 +957,7 @@ def get_teams_with_query_metric(
{
"begin": begin,
"end": end,
+ "event_time_end": event_time_end,
"query_types": query_types,
"access_method": access_method,
},