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(error-tracking): keep the source maps count out of frame contents (#98346)

posthog[bot]f10ed05a1522
diff --git a/products/error_tracking/backend/logic/recommendations/source_maps.py b/products/error_tracking/backend/logic/recommendations/source_maps.py
index f6910b3342ff..e9813c75064e 100644
--- a/products/error_tracking/backend/logic/recommendations/source_maps.py
+++ b/products/error_tracking/backend/logic/recommendations/source_maps.py
@@ -1,7 +1,7 @@
from datetime import timedelta
from typing import Any
-from django.db.models import Count, Q
+from django.db.models import Count
from django.utils import timezone
from products.error_tracking.backend.models import ErrorTrackingStackFrame
@@ -30,19 +30,25 @@ class SourceMapsRecommendation(Recommendation):
# care about here.
since = timezone.now() - timedelta(hours=LOOKBACK_HOURS)
- counts_by_team = {
- row["team_id"]: row
- for row in ErrorTrackingStackFrame.objects.filter(
+ # This aggregate must read no column outside et_frame_team_created_js_idx. A count over
+ # `id` pulls the heap row, which detoasts the wide `contents` column with it.
+ rows = (
+ ErrorTrackingStackFrame.objects.filter(
team_id__in=team_ids,
created_at__gte=since,
contents__lang="javascript",
)
- .values("team_id")
- .annotate(
- total=Count("id"),
- unresolved=Count("id", filter=Q(resolved=False)),
- )
- }
+ .values("team_id", "resolved")
+ .annotate(frames=Count("*"))
+ )
+
+ counts_by_team: dict[int, dict[str, int]] = {}
+ for row in rows:
+ counts = counts_by_team.setdefault(row["team_id"], {"total": 0, "unresolved": 0})
+ counts["total"] += row["frames"]
+ if not row["resolved"]:
+ counts["unresolved"] += row["frames"]
+
return {team_id: self._build_meta(counts_by_team.get(team_id)) for team_id in team_ids}
@staticmethod
diff --git a/products/error_tracking/backend/migrations/0036_add_stackframe_js_frames_index.py b/products/error_tracking/backend/migrations/0036_add_stackframe_js_frames_index.py
new file mode 100644
index 000000000000..74c4c1e8e5c1
--- /dev/null
+++ b/products/error_tracking/backend/migrations/0036_add_stackframe_js_frames_index.py
@@ -0,0 +1,23 @@
+from django.db import migrations, models
+
+from posthog.migration_helpers import SafeAddIndexConcurrently
+
+
+class Migration(migrations.Migration):
+ atomic = False
+
+ dependencies = [
+ ("error_tracking", "0035_alter_errortrackingstackframe_team"),
+ ]
+
+ operations = [
+ SafeAddIndexConcurrently(
+ model_name="errortrackingstackframe",
+ index=models.Index(
+ fields=["team", "created_at"],
+ include=["resolved"],
+ condition=models.Q(contents__lang="javascript"),
+ name="et_frame_team_created_js_idx",
+ ),
+ ),
+ ]
diff --git a/products/error_tracking/backend/migrations/max_migration.txt b/products/error_tracking/backend/migrations/max_migration.txt
index 2f718f67759e..c49f8d127df4 100644
--- a/products/error_tracking/backend/migrations/max_migration.txt
+++ b/products/error_tracking/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0035_alter_errortrackingstackframe_team
+0036_add_stackframe_js_frames_index
diff --git a/products/error_tracking/backend/models.py b/products/error_tracking/backend/models.py
index bd54b34d77d9..7834de626176 100644
--- a/products/error_tracking/backend/models.py
+++ b/products/error_tracking/backend/models.py
@@ -617,6 +617,15 @@ class ErrorTrackingStackFrame(UUIDTModel):
# Recent-frames-per-team scans, such as the source maps recommendation. Without
# created_at in the index, a 24h window has to visit every frame the team ever stored.
models.Index(fields=["team", "created_at"], name="et_frame_team_created_at_idx"),
+ # Covers the source maps recommendation's count of recent JavaScript frames. The
+ # predicate holds the language test, so Postgres answers the count from the index
+ # and never detoasts the wide `contents` column.
+ models.Index(
+ fields=["team", "created_at"],
+ include=["resolved"],
+ condition=models.Q(contents__lang="javascript"),
+ name="et_frame_team_created_js_idx",
+ ),
]
constraints = [