diff --git a/zerver/migrations/0693_add_conditional_indexes_for_topic.py b/zerver/migrations/0693_add_conditional_indexes_for_topic.py new file mode 100644 index 0000000000..53ff12a5e6 --- /dev/null +++ b/zerver/migrations/0693_add_conditional_indexes_for_topic.py @@ -0,0 +1,61 @@ +import django.db.models.functions.text +from django.contrib.postgres.operations import AddIndexConcurrently +from django.db import migrations, models + + +class Migration(migrations.Migration): + atomic = False + + dependencies = [ + ("zerver", "0692_alter_message_is_channel_message"), + ] + + operations = [ + migrations.RenameIndex( + model_name="message", + new_name="zerver_message_realm_upper_subject_all", + old_name="zerver_message_realm_upper_subject", + ), + migrations.RenameIndex( + model_name="message", + new_name="zerver_message_realm_recipient_upper_subject_all", + old_name="zerver_message_realm_recipient_upper_subject", + ), + migrations.RenameIndex( + model_name="message", + new_name="zerver_message_realm_recipient_subject_all", + old_name="zerver_message_realm_recipient_subject", + ), + AddIndexConcurrently( + model_name="message", + index=models.Index( + models.F("realm_id"), + django.db.models.functions.text.Upper("subject"), + models.OrderBy(models.F("id"), descending=True, nulls_last=True), + condition=models.Q(("is_channel_message", True)), + name="zerver_message_realm_upper_subject", + ), + ), + AddIndexConcurrently( + model_name="message", + index=models.Index( + models.F("realm_id"), + models.F("recipient_id"), + django.db.models.functions.text.Upper("subject"), + models.OrderBy(models.F("id"), descending=True, nulls_last=True), + condition=models.Q(("is_channel_message", True)), + name="zerver_message_realm_recipient_upper_subject", + ), + ), + AddIndexConcurrently( + model_name="message", + index=models.Index( + models.F("realm_id"), + models.F("recipient_id"), + models.F("subject"), + models.OrderBy(models.F("id"), descending=True, nulls_last=True), + condition=models.Q(("is_channel_message", True)), + name="zerver_message_realm_recipient_subject", + ), + ), + ] diff --git a/zerver/models/messages.py b/zerver/models/messages.py index a79bb7b777..9def87c474 100644 --- a/zerver/models/messages.py +++ b/zerver/models/messages.py @@ -205,6 +205,29 @@ class Message(AbstractMessage): "date_sent", name="zerver_message_realm_date_sent", ), + models.Index( + # To be removed shortly + "realm_id", + Upper("subject"), + F("id").desc(nulls_last=True), + name="zerver_message_realm_upper_subject_all", + ), + models.Index( + # To be removed shortly + "realm_id", + "recipient_id", + Upper("subject"), + F("id").desc(nulls_last=True), + name="zerver_message_realm_recipient_upper_subject_all", + ), + models.Index( + # To be removed shortly + "realm_id", + "recipient_id", + "subject", + F("id").desc(nulls_last=True), + name="zerver_message_realm_recipient_subject_all", + ), models.Index( # For users searching by topic (but not stream), which # is done case-insensitively @@ -212,6 +235,7 @@ class Message(AbstractMessage): Upper("subject"), F("id").desc(nulls_last=True), name="zerver_message_realm_upper_subject", + condition=Q(is_channel_message=True), ), models.Index( # Most stream/topic searches are case-insensitive by @@ -223,6 +247,7 @@ class Message(AbstractMessage): Upper("subject"), F("id").desc(nulls_last=True), name="zerver_message_realm_recipient_upper_subject", + condition=Q(is_channel_message=True), ), models.Index( # Used by already_sent_mirrored_message_id, and when @@ -233,6 +258,7 @@ class Message(AbstractMessage): "subject", F("id").desc(nulls_last=True), name="zerver_message_realm_recipient_subject", + condition=Q(is_channel_message=True), ), models.Index( # Only used by update_first_visible_message_id