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(activity-log): drop the redundant jsonb_ops GIN index on detail (#94875)

posthog[bot]36f61318fdca
diff --git a/posthog/migrations/1347_drop_activitylog_detail_jsonb_ops_gin.py b/posthog/migrations/1347_drop_activitylog_detail_jsonb_ops_gin.py
new file mode 100644
index 000000000000..a821f6343bb5
--- /dev/null
+++ b/posthog/migrations/1347_drop_activitylog_detail_jsonb_ops_gin.py
@@ -0,0 +1,40 @@
+import django.contrib.postgres.indexes
+from django.db import migrations
+
+from posthog.migration_helpers.concurrent_index import SafeRemoveIndexConcurrently
+
+
+class Migration(migrations.Migration):
+ atomic = False
+
+ dependencies = [
+ ("posthog", "1346_untrack_organization_is_hipaa"),
+ ]
+
+ operations = [
+ SafeRemoveIndexConcurrently(
+ model_name="activitylog",
+ name="activitylog_detail_gin",
+ ),
+ # Migration 0854 declared `idx_alog_detail_gin_path_ops` partial in state but created it
+ # unfiltered in the database, so both prod regions carry the unfiltered index. Correct the
+ # state to match. `detail` is non-null on effectively every row, so the predicate excluded
+ # nothing, and rebuilding the index to add it would cost a concurrent GIN build for no gain.
+ migrations.SeparateDatabaseAndState(
+ state_operations=[
+ migrations.RemoveIndex(
+ model_name="activitylog",
+ name="idx_alog_detail_gin_path_ops",
+ ),
+ migrations.AddIndex(
+ model_name="activitylog",
+ index=django.contrib.postgres.indexes.GinIndex(
+ fields=["detail"],
+ name="idx_alog_detail_gin_path_ops",
+ opclasses=["jsonb_path_ops"],
+ ),
+ ),
+ ],
+ database_operations=[],
+ ),
+ ]
diff --git a/posthog/migrations/max_migration.txt b/posthog/migrations/max_migration.txt
index 971cef9a73c5..5d9e938849b0 100644
--- a/posthog/migrations/max_migration.txt
+++ b/posthog/migrations/max_migration.txt
@@ -1 +1 @@
-1346_untrack_organization_is_hipaa
+1347_drop_activitylog_detail_jsonb_ops_gin
diff --git a/posthog/models/activity_logging/activity_log.py b/posthog/models/activity_logging/activity_log.py
index 56b6230b53c4..1aabb132ff00 100644
--- a/posthog/models/activity_logging/activity_log.py
+++ b/posthog/models/activity_logging/activity_log.py
@@ -185,18 +185,17 @@ class ActivityLog(UUIDTModel):
name="idx_alog_org_detail_exists",
condition=models.Q(detail__isnull=False) & models.Q(detail__jsonb_typeof="object"),
),
- # Used for searching on the detail field, e.g. containing a specific value
- GinIndex(
- name="activitylog_detail_gin",
- fields=["detail"],
- opclasses=["jsonb_ops"],
- ),
- # Used primarily for available_filters queries
+ # Serves whole-column containment (`detail @> ...`), the only detail lookup an index
+ # can answer. Key-path lookups and the `detail::text` search are not GIN-servable
+ # under any opclass. `jsonb_path_ops` stores one hash per root-to-leaf path, so it is
+ # smaller and cheaper to maintain than `jsonb_ops`, whose only extra operators are the
+ # key-existence family (`?`, `?|`, `?&`) that no query path uses. It also stores no
+ # entry for a JSON structure that holds no scalar, so containment against an empty
+ # object or array (`detail @> '{"changes": []}'`) falls back to a full index scan.
GinIndex(
name="idx_alog_detail_gin_path_ops",
fields=["detail"],
opclasses=["jsonb_path_ops"],
- condition=models.Q(detail__isnull=False),
),
# User-specific filtered queries
models.Index(
diff --git a/products/experiments/backend/test/test_migration_0035.py b/products/experiments/backend/test/test_migration_0035.py
index c848a7520e0d..821d58b7e140 100644
--- a/products/experiments/backend/test/test_migration_0035.py
+++ b/products/experiments/backend/test/test_migration_0035.py
@@ -1,9 +1,11 @@
from typing import Any
-from posthog.test.base import TestMigrations
+from posthog.test.base import NonAtomicTestMigrations
-class StripUnknownExposureCriteriaKeysMigrationTest(TestMigrations):
+# Rewinding to migrate_from unapplies posthog migrations that use index DDL
+# Postgres refuses inside a transaction, so this suite needs the non-atomic base.
+class StripUnknownExposureCriteriaKeysMigrationTest(NonAtomicTestMigrations):
migrate_from = "0034_backfill_precomputation_enabled_set_by"
migrate_to = "0035_strip_unknown_exposure_criteria_keys"