Skip to content

feat: Add GIN index on Identity.identifier #5369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Generated by Django 4.2.18 on 2025-04-22 10:15

from django.contrib.postgres.indexes import GinIndex
from django.contrib.postgres.operations import TrigramExtension
from django.db import migrations
import django.contrib.postgres.indexes
import django.db.models.functions.text

from core.migration_helpers import PostgresOnlyRunSQL


_create_index_sql = "CREATE INDEX CONCURRENTLY IF NOT EXISTS identity_identifier_idx ON environments_identity USING GIN (UPPER(identifier) gin_trgm_ops);"
_create_index_reverse_sql = (
"DROP INDEX CONCURRENTLY IF EXISTS identity_identifier_idx;"
)


class Migration(migrations.Migration):
atomic = False

dependencies = [
("identities", "0002_alter_identity_index_together"),
]

operations = [
migrations.SeparateDatabaseAndState(
state_operations=[
migrations.AddIndex(
model_name="identity",
index=GinIndex(
django.contrib.postgres.indexes.OpClass(
django.db.models.functions.text.Upper("identifier"),
name="gin_trgm_ops",
),
name="identity_identifier_idx",
),
),
],
database_operations=[
TrigramExtension(),
PostgresOnlyRunSQL(
sql=_create_index_sql,
reverse_sql=_create_index_reverse_sql,
),
],
),
]
8 changes: 8 additions & 0 deletions api/environments/identities/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import typing
from itertools import chain

from django.contrib.postgres.indexes import GinIndex, OpClass
from django.db import models
from django.db.models import Prefetch, Q
from django.db.models.functions import Upper
from django.utils import timezone
from flag_engine.segments.evaluator import evaluate_identity_in_segment

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

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