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.

feat(logs): scale the logs SQL editor read limit by plan tier (#97479)

Frank Hamand4f7de79ff8b2
diff --git a/posthog/hogql/database/schema/logs.py b/posthog/hogql/database/schema/logs.py
index 66671b779568..3bbb438e7234 100644
--- a/posthog/hogql/database/schema/logs.py
+++ b/posthog/hogql/database/schema/logs.py
@@ -1,4 +1,4 @@
-from typing import TYPE_CHECKING
+from typing import TYPE_CHECKING, Literal
from posthog.hogql.database.models import (
DANGEROUS_NoTeamIdCheckTable,
@@ -16,8 +16,21 @@ from posthog.clickhouse.workload import Workload
if TYPE_CHECKING:
from posthog.hogql.context import HogQLContext
-# 50GB - limit for user-provided HogQL queries on log tables to prevent expensive full scans
-HOGQL_MAX_BYTES_TO_READ_FOR_LOGS_USER_QUERIES = 50_000_000_000
+ from posthog.models.organization import Organization
+
+# Limit for user-provided HogQL queries on log tables to prevent expensive full scans.
+# Paid and enterprise orgs get a higher budget; free stays at the original 50GB cap.
+HOGQL_MAX_BYTES_TO_READ_FOR_LOGS_USER_QUERIES_BY_TIER: dict[Literal["free", "paid", "enterprise"], int] = {
+ "free": 50_000_000_000,
+ "paid": 150_000_000_000,
+ "enterprise": 150_000_000_000,
+}
+
+
+def get_hogql_max_bytes_to_read_for_logs_user_queries(organization: "Organization | None") -> int:
+ """Read-byte budget for user-provided HogQL queries on log tables, scaled by plan tier."""
+ tier = organization.get_plan_tier() if organization is not None else "free"
+ return HOGQL_MAX_BYTES_TO_READ_FOR_LOGS_USER_QUERIES_BY_TIER[tier]
class LogsTable(Table):
diff --git a/posthog/hogql/database/schema/test/test_logs.py b/posthog/hogql/database/schema/test/test_logs.py
new file mode 100644
index 000000000000..9ca965e42be0
--- /dev/null
+++ b/posthog/hogql/database/schema/test/test_logs.py
@@ -0,0 +1,23 @@
+from unittest.mock import Mock
+
+from django.test import SimpleTestCase
+
+from parameterized import parameterized
+
+from posthog.hogql.database.schema.logs import get_hogql_max_bytes_to_read_for_logs_user_queries
+
+
+class TestGetHogqlMaxBytesToReadForLogsUserQueries(SimpleTestCase):
+ @parameterized.expand(
+ [
+ ("free", 50_000_000_000),
+ ("paid", 150_000_000_000),
+ ("enterprise", 150_000_000_000),
+ ]
+ )
+ def test_scales_with_plan_tier(self, tier: str, expected_bytes: int) -> None:
+ organization = Mock(get_plan_tier=Mock(return_value=tier))
+ assert get_hogql_max_bytes_to_read_for_logs_user_queries(organization) == expected_bytes
+
+ def test_defaults_to_free_tier_when_no_organization(self) -> None:
+ assert get_hogql_max_bytes_to_read_for_logs_user_queries(None) == 50_000_000_000
diff --git a/posthog/hogql/query.py b/posthog/hogql/query.py
index d916a55eaf3b..093e9d84eb11 100644
--- a/posthog/hogql/query.py
+++ b/posthog/hogql/query.py
@@ -31,7 +31,7 @@ from posthog.hogql.database.schema.duckdb_table_functions import (
RangeTable,
)
from posthog.hogql.database.schema.information_schema import InformationSchemaTable
-from posthog.hogql.database.schema.logs import HOGQL_MAX_BYTES_TO_READ_FOR_LOGS_USER_QUERIES
+from posthog.hogql.database.schema.logs import get_hogql_max_bytes_to_read_for_logs_user_queries
from posthog.hogql.database.warehouse_usage import WarehouseSourceUsage, extract_warehouse_sources
from posthog.hogql.direct_connection import (
INVALID_CONNECTION_ID_ERROR,
@@ -666,7 +666,9 @@ class HogQLQueryExecutor:
if self.clickhouse_context.workload == Workload.LOGS and self.query_type == "HogQLQuery":
if settings.max_bytes_to_read is None:
- settings.max_bytes_to_read = HOGQL_MAX_BYTES_TO_READ_FOR_LOGS_USER_QUERIES
+ settings.max_bytes_to_read = get_hogql_max_bytes_to_read_for_logs_user_queries(
+ self.team.organization
+ )
if settings.read_overflow_mode is None:
settings.read_overflow_mode = "throw"
diff --git a/posthog/hogql/test/test_log_query_settings.py b/posthog/hogql/test/test_log_query_settings.py
index d1fd0bda013f..97d48a0af721 100644
--- a/posthog/hogql/test/test_log_query_settings.py
+++ b/posthog/hogql/test/test_log_query_settings.py
@@ -40,6 +40,21 @@ class TestLogQuerySettings(ClickhouseTestMixin, APIBaseTest):
sql = self._get_clickhouse_sql_for("SELECT * FROM logs LIMIT 10")
assert f"max_bytes_to_read=" in sql.replace(" ", "")
+ @parameterized.expand(
+ [
+ ("free", None, 50_000_000_000),
+ ("paid", [{"key": "recordings_file_export"}], 150_000_000_000),
+ ("enterprise", [{"key": "role_based_access"}], 150_000_000_000),
+ ]
+ )
+ def test_user_query_on_logs_table_scales_max_bytes_to_read_by_plan_tier(
+ self, _name: str, available_product_features: list[dict[str, str]] | None, expected_bytes: int
+ ) -> None:
+ self.team.organization.available_product_features = available_product_features
+ self.team.organization.save()
+ sql = self._get_clickhouse_sql_for("SELECT * FROM logs LIMIT 10")
+ assert f"max_bytes_to_read={expected_bytes}" in sql.replace(" ", "")
+
def test_user_query_on_logs_table_has_throw_overflow_mode(self):
sql = self._get_clickhouse_sql_for("SELECT * FROM logs LIMIT 10")
assert "read_overflow_mode" in sql