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(file-system): add a team-scoped trigram index for file search (#120317)

Shy Alter202ead738f9a
diff --git a/posthog/management/commands/setup_test_environment.py b/posthog/management/commands/setup_test_environment.py
index 33a86e186ae6..a70aa470542a 100644
--- a/posthog/management/commands/setup_test_environment.py
+++ b/posthog/management/commands/setup_test_environment.py
@@ -113,6 +113,7 @@ def disable_migrations() -> None:
with connection.cursor() as cursor:
cursor.execute("CREATE EXTENSION IF NOT EXISTS pg_trgm")
cursor.execute("CREATE EXTENSION IF NOT EXISTS ltree")
+ cursor.execute("CREATE EXTENSION IF NOT EXISTS btree_gin")
return super().handle(*args, **kwargs)
diff --git a/posthog/migrations/1406_file_system_team_text_trgm_index.py b/posthog/migrations/1406_file_system_team_text_trgm_index.py
new file mode 100644
index 000000000000..a5487393e3bd
--- /dev/null
+++ b/posthog/migrations/1406_file_system_team_text_trgm_index.py
@@ -0,0 +1,36 @@
+import django.contrib.postgres.indexes
+import django.db.models.functions.text
+from django.contrib.postgres.operations import BtreeGinExtension
+from django.db import migrations, models
+
+from posthog.migration_helpers import SafeAddIndexConcurrently
+
+
+class Migration(migrations.Migration):
+ atomic = False
+
+ dependencies = [
+ ("posthog", "1405_user_onboarding_skipped_reason_connector"),
+ ]
+
+ operations = [
+ BtreeGinExtension(),
+ SafeAddIndexConcurrently(
+ model_name="filesystem",
+ index=django.contrib.postgres.indexes.GinIndex(
+ models.F("team_id"),
+ django.contrib.postgres.indexes.OpClass(
+ django.db.models.functions.text.Lower(
+ django.db.models.functions.text.Concat(
+ models.F("path"),
+ models.Value(" "),
+ models.F("type"),
+ output_field=models.TextField(),
+ )
+ ),
+ name="gin_trgm_ops",
+ ),
+ name="posthog_fs_team_text_trgm",
+ ),
+ ),
+ ]
diff --git a/posthog/migrations/max_migration.txt b/posthog/migrations/max_migration.txt
index b0acb460d5cb..a15841f66090 100644
--- a/posthog/migrations/max_migration.txt
+++ b/posthog/migrations/max_migration.txt
@@ -1 +1 @@
-1405_user_onboarding_skipped_reason_connector
+1406_file_system_team_text_trgm_index
diff --git a/posthog/models/file_system/file_system.py b/posthog/models/file_system/file_system.py
index 995aed467114..01e65fee29fc 100644
--- a/posthog/models/file_system/file_system.py
+++ b/posthog/models/file_system/file_system.py
@@ -1,9 +1,11 @@
from datetime import datetime
from typing import Optional
+from django.contrib.postgres.indexes import GinIndex, OpClass
from django.db import models
-from django.db.models import Q
+from django.db.models import Q, TextField, Value
from django.db.models.expressions import F
+from django.db.models.functions import Concat, Lower
from django.utils import timezone
from posthog.models.file_system.constants import DEFAULT_SURFACE, surface_q
@@ -12,6 +14,8 @@ from posthog.models.team import Team
from posthog.models.user import User
from posthog.models.utils import uuid7
+SEARCH_TEXT = Lower(Concat(F("path"), Value(" "), F("type"), output_field=TextField()))
+
class FileSystem(models.Model):
"""
@@ -41,6 +45,7 @@ class FileSystem(models.Model):
models.Index(F("team_id"), F("surface"), F("path"), name="posthog_fs_team_s_path"),
models.Index(F("team_id"), F("surface"), F("depth"), name="posthog_fs_team_s_depth"),
models.Index(F("team_id"), F("surface"), F("type"), F("ref"), name="posthog_fs_team_s_typeref"),
+ GinIndex(F("team_id"), OpClass(SEARCH_TEXT, name="gin_trgm_ops"), name="posthog_fs_team_text_trgm"),
]
def __str__(self):