Skip to content

Commit 31bd046

Browse files
authored
feat: Add GIN index on Identity.identifier (#5369)
1 parent 6b711e2 commit 31bd046

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Generated by Django 4.2.18 on 2025-04-22 10:15
2+
3+
from django.contrib.postgres.indexes import GinIndex
4+
from django.contrib.postgres.operations import TrigramExtension
5+
from django.db import migrations
6+
import django.contrib.postgres.indexes
7+
import django.db.models.functions.text
8+
9+
from core.migration_helpers import PostgresOnlyRunSQL
10+
11+
12+
_create_index_sql = "CREATE INDEX CONCURRENTLY IF NOT EXISTS identity_identifier_idx ON environments_identity USING GIN (UPPER(identifier) gin_trgm_ops);"
13+
_create_index_reverse_sql = (
14+
"DROP INDEX CONCURRENTLY IF EXISTS identity_identifier_idx;"
15+
)
16+
17+
18+
class Migration(migrations.Migration):
19+
atomic = False
20+
21+
dependencies = [
22+
("identities", "0002_alter_identity_index_together"),
23+
]
24+
25+
operations = [
26+
migrations.SeparateDatabaseAndState(
27+
state_operations=[
28+
migrations.AddIndex(
29+
model_name="identity",
30+
index=GinIndex(
31+
django.contrib.postgres.indexes.OpClass(
32+
django.db.models.functions.text.Upper("identifier"),
33+
name="gin_trgm_ops",
34+
),
35+
name="identity_identifier_idx",
36+
),
37+
),
38+
],
39+
database_operations=[
40+
TrigramExtension(),
41+
PostgresOnlyRunSQL(
42+
sql=_create_index_sql,
43+
reverse_sql=_create_index_reverse_sql,
44+
),
45+
],
46+
),
47+
]

api/environments/identities/models.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import typing
22
from itertools import chain
33

4+
from django.contrib.postgres.indexes import GinIndex, OpClass
45
from django.db import models
56
from django.db.models import Prefetch, Q
7+
from django.db.models.functions import Upper
68
from django.utils import timezone
79
from flag_engine.segments.evaluator import evaluate_identity_in_segment
810

@@ -40,6 +42,12 @@ class Meta:
4042
# avoid any downtime. If people using MySQL / Oracle have issues with poor performance on the identities table,
4143
# we can provide them the SQL to add it manually in a small window of downtime.
4244
index_together = (("environment", "created_date"),)
45+
indexes = [
46+
GinIndex(
47+
OpClass(Upper("identifier"), name="gin_trgm_ops"),
48+
name="identity_identifier_idx",
49+
),
50+
]
4351

4452
def natural_key(self): # type: ignore[no-untyped-def]
4553
return self.identifier, self.environment.api_key

0 commit comments

Comments
 (0)