Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

user_topics: Index the per-message visibility policy lookup.

Aman Agrawal316c88ce82dd
diff --git a/zerver/migrations/0807_usertopic_zerver_usertopic_user_visibility_recipient_idx.py b/zerver/migrations/0807_usertopic_zerver_usertopic_user_visibility_recipient_idx.py
new file mode 100644
index 00000000000..8b6b9e43464
--- /dev/null
+++ b/zerver/migrations/0807_usertopic_zerver_usertopic_user_visibility_recipient_idx.py
@@ -0,0 +1,29 @@
+# Generated by Django 5.2.16 on 2026-08-06 07:24
+
+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", "0806_stream_default_push_notifications"),
+ ]
+
+ operations = [
+ AddIndexConcurrently(
+ model_name="usertopic",
+ index=models.Index(
+ models.F("user_profile"),
+ models.F("visibility_policy"),
+ models.F("recipient"),
+ django.db.models.functions.text.Upper("topic_name"),
+ name="zerver_usertopic_user_visibility_recipient_idx",
+ ),
+ ),
+ # The index is on an expression, whose statistics only exist once
+ # the table has been analyzed.
+ migrations.RunSQL("ANALYZE zerver_usertopic", reverse_sql=migrations.RunSQL.noop),
+ ]
diff --git a/zerver/models/user_topics.py b/zerver/models/user_topics.py
index 0af96bd1068..cbf92101d23 100644
--- a/zerver/models/user_topics.py
+++ b/zerver/models/user_topics.py
@@ -69,6 +69,17 @@ class UserTopic(models.Model):
fields=("user_profile", "visibility_policy", "stream", "topic_name"),
name="zerver_usertopic_user_visibility_idx",
),
+ # Supports the per-message visibility policy lookup in
+ # matching_user_topic_exists_q, which has the recipient
+ # rather than the channel and matches topic names
+ # case-insensitively, so the index above cannot serve it.
+ models.Index(
+ "user_profile",
+ "visibility_policy",
+ "recipient",
+ Upper("topic_name"),
+ name="zerver_usertopic_user_visibility_recipient_idx",
+ ),
]
@override