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.

perf(signals): index scratchpad search order and followup key prefix (#110408)

posthog[bot]c8d2b931a2ee
diff --git a/products/signals/backend/migrations/0166_signalscratchpad_indexes.py b/products/signals/backend/migrations/0166_signalscratchpad_indexes.py
new file mode 100644
index 000000000000..e710890c0534
--- /dev/null
+++ b/products/signals/backend/migrations/0166_signalscratchpad_indexes.py
@@ -0,0 +1,26 @@
+from django.db import migrations, models
+
+from posthog.migration_helpers import SafeAddIndexConcurrently
+
+
+class Migration(migrations.Migration):
+ atomic = False
+
+ dependencies = [
+ ("signals", "0165_scout_config_precheck"),
+ ]
+
+ operations = [
+ SafeAddIndexConcurrently(
+ model_name="signalscratchpad",
+ index=models.Index(fields=["team", "-updated_at", "-id"], name="signal_scratchpad_team_upd_idx"),
+ ),
+ SafeAddIndexConcurrently(
+ model_name="signalscratchpad",
+ index=models.Index(
+ fields=["team", "key"],
+ name="signal_scratchpad_key_like_idx",
+ opclasses=["int4_ops", "varchar_pattern_ops"],
+ ),
+ ),
+ ]
diff --git a/products/signals/backend/migrations/max_migration.txt b/products/signals/backend/migrations/max_migration.txt
index 4497d66df879..164746f9f297 100644
--- a/products/signals/backend/migrations/max_migration.txt
+++ b/products/signals/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0165_scout_config_precheck
+0166_signalscratchpad_indexes
diff --git a/products/signals/backend/models.py b/products/signals/backend/models.py
index 65a4ce084af6..50c403e4330d 100644
--- a/products/signals/backend/models.py
+++ b/products/signals/backend/models.py
@@ -3255,6 +3255,15 @@ class SignalScratchpad(TeamScopedRootMixin, UUIDModel):
constraints = [
models.UniqueConstraint(fields=["team", "key"], name="signal_scratchpad_unique_team_key"),
]
+ indexes = [
+ models.Index(fields=["team", "-updated_at", "-id"], name="signal_scratchpad_team_upd_idx"),
+ # The unique `(team, key)` index cannot serve a `key` prefix LIKE under a non-C collation.
+ models.Index(
+ fields=["team", "key"],
+ name="signal_scratchpad_key_like_idx",
+ opclasses=["int4_ops", "varchar_pattern_ops"],
+ ),
+ ]
class SignalScoutNote(TeamScopedRootMixin, UUIDModel):