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.

migrations: Add support for concurrent/non-concurrent index creation.

John Seekinsf5df3e718d31
diff --git a/confirmation/migrations/0014_confirmation_confirmatio_content_80155a_idx.py b/confirmation/migrations/0014_confirmation_confirmatio_content_80155a_idx.py
index eb4abd7a0ae..6db85f15aa4 100644
--- a/confirmation/migrations/0014_confirmation_confirmatio_content_80155a_idx.py
+++ b/confirmation/migrations/0014_confirmation_confirmatio_content_80155a_idx.py
@@ -1,9 +1,11 @@
-from django.contrib.postgres.operations import AddIndexConcurrently
+from django.conf import settings
from django.db import migrations, models
+from zerver.lib.migrate import add_index
+
class Migration(migrations.Migration):
- atomic = False
+ atomic = not settings.MIGRATIONS_ADD_REMOVE_INDEXES_CONCURRENTLY
dependencies = [
("confirmation", "0013_alter_realmcreationkey_id"),
@@ -11,7 +13,7 @@ class Migration(migrations.Migration):
]
operations = [
- AddIndexConcurrently(
+ add_index(
model_name="confirmation",
index=models.Index(
fields=["content_type", "object_id"], name="confirmatio_content_80155a_idx"
diff --git a/docs/subsystems/schema-migrations.md b/docs/subsystems/schema-migrations.md
index 076be82953b..550529d7b0f 100644
--- a/docs/subsystems/schema-migrations.md
+++ b/docs/subsystems/schema-migrations.md
@@ -207,21 +207,34 @@ handle it.
## Indexes
-### Adding indexes
+### Adding and dropping indexes
Django's regular `AddIndex` operation (corresponding to `CREATE
INDEX` in SQL) takes a lock that blocks writes to the affected
table for the duration of the build. On large tables (`Message`,
`UserMessage`, `Subscription`, `RealmAuditLog`, etc.) that lock
-can last long enough to affect production. Use
-`AddIndexConcurrently` (corresponding to `CREATE INDEX
-CONCURRENTLY`) for any index on those tables, and as a default
-for any index on a table large enough that the lock would be
-noticeable.
-
-`AddIndexConcurrently` requires `atomic = False` on the
-migration class — `CREATE INDEX CONCURRENTLY` cannot run inside
-a transaction.
+can last long enough to affect production, which instead needs
+`CREATE INDEX CONCURRENTLY` to avoid it; self-hosted deployments,
+which have less data, can usually just take the lock. The
+`add_index` helper (from `zerver.lib.migrate`) handles this
+distinction — use it rather than `AddIndex` for any index on
+those tables, and as a default for any index on a table large
+enough that the lock would be noticeable.
+
+Dropping an index has the same problem: `RemoveIndex` (`DROP
+INDEX`) takes an `ACCESS EXCLUSIVE` lock on the table. Use
+`remove_index` (from `zerver.lib.migrate`) rather than
+`RemoveIndex` in the same situations.
+
+Whether these helpers run the operation `CONCURRENTLY` is
+governed by the `MIGRATIONS_ADD_REMOVE_INDEXES_CONCURRENTLY`
+setting, which is enabled on Zulip Cloud. Because the
+`CONCURRENTLY` operations cannot run inside a transaction, a migration
+using either helper must set `atomic = not
+settings.MIGRATIONS_ADD_REMOVE_INDEXES_CONCURRENTLY` — unless another
+operation in the migration requires `atomic = False` regardless, such
+as a large `RunPython` backfill that must not run in a single
+transaction.
For an index on plain table columns, `CREATE INDEX` populates
the index relation's own size/row statistics and the planner
@@ -230,9 +243,21 @@ statistics — no follow-up `ANALYZE` is needed. For **expression
indexes** (e.g., `Upper(subject)`), PostgreSQL only gathers
per-expression statistics in `pg_statistic` when `ANALYZE`
runs, so add a `RunSQL("ANALYZE zerver_<table>")` after the
-`AddIndexConcurrently` so the planner has the distribution info
+`add_index` so the planner has the distribution info
it needs to use the new index.
+#### Dropping auto-generated indexes
+
+`remove_index` only handles indexes declared in `Meta.indexes`.
+To drop an auto-created index concurrently — for example, the
+index on a foreign key whose `db_index` you're setting to
+`False` — use
+[SeparateDatabaseAndState](#separating-database-and-state)
+instead. Django's `AlterField(db_index=False)` issues a regular
+(locking) `DROP INDEX`; the separate-state pattern lets you keep
+Django's model tracking in sync while doing the actual drop
+concurrently.
+
### Renaming indexes
When the index naming scheme changes, prefer `RenameIndex` to
@@ -241,16 +266,6 @@ dropping and rebuilding (see
expression indexes; note that it mistakenly omits the `ANALYZE`
that those would need).
-### Dropping indexes
-
-To drop an index `CONCURRENTLY` — for example, the auto-created
-index on a foreign key whose `db_index` you're setting to
-`False` — use
-[SeparateDatabaseAndState](#separating-database-and-state).
-Django's `AlterField(db_index=False)` issues a regular (locking)
-`DROP INDEX`; the separate-state pattern lets you keep Django's
-model tracking in sync while doing the actual drop concurrently.
-
## Constraints on large tables
Django's `AddConstraint` takes an `ACCESS EXCLUSIVE` lock on the
diff --git a/tools/semgrep-py.yml b/tools/semgrep-py.yml
index 859cd291557..b6e94356ad0 100644
--- a/tools/semgrep-py.yml
+++ b/tools/semgrep-py.yml
@@ -120,9 +120,11 @@ rules:
- zerver/migrations/0493_rename_userhotspot_to_onboardingstep.py
- pgroonga/migrations/0002_html_escape_subject.py
- - id: use-addindexconcurrently
- pattern: django.db.migrations.AddIndex(...)
- message: "Import and use AddIndexConcurrently from django.contrib.postgres.operations rather than AddIndex"
+ - id: use-add_index
+ pattern-either:
+ - pattern: django.db.migrations.AddIndex(...)
+ - pattern: django.contrib.postgres.operations.AddIndexConcurrently(...)
+ message: "Import and use add_index from zerver/lib/migrate.py rather than AddIndex/AddIndexConcurrently"
languages: [python]
severity: ERROR
paths:
@@ -152,17 +154,17 @@ rules:
- zilencer/migrations/0058_remoteinstallationcount_add_mobile_pushes_forwarded_index.py
- zilencer/migrations/0059_remoterealmauditlog_add_synced_billing_event_type_index.py
- - id: use-removeindexconcurrently
- pattern: django.db.migrations.RemoveIndex(...)
- message: "Import and use RemoveIndexConcurrently from django.contrib.postgres.operations rather than RemoveIndex"
+ - id: use-remove_index
+ pattern-either:
+ - pattern: django.db.migrations.RemoveIndex(...)
+ - pattern: django.contrib.postgres.operations.RemoveIndexConcurrently(...)
+ message: "Import and use remove_index from zerver/lib/migrate.py rather than RemoveIndex/RemoveIndexConcurrently"
languages: [python]
severity: ERROR
paths:
include:
- "**/migrations"
exclude:
- - zerver/migrations/0473_remove_message_non_realm_id_indexes.py
- - zerver/migrations/0486_clear_old_data_for_unused_usermessage_flags.py
- zilencer/migrations/0038_unique_server_remote_id.py
- id: html-format
diff --git a/zerver/lib/migrate.py b/zerver/lib/migrate.py
index e45c328522f..f5d565a0590 100644
--- a/zerver/lib/migrate.py
+++ b/zerver/lib/migrate.py
@@ -2,10 +2,14 @@ import time
from collections.abc import Callable
from typing import Any
+from django.conf import settings
+from django.contrib.postgres.operations import AddIndexConcurrently, RemoveIndexConcurrently
from django.db import connection
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django.db.backends.utils import CursorWrapper
+from django.db.migrations import AddIndex, RemoveIndex
from django.db.migrations.state import StateApps
+from django.db.models import Index
from psycopg2.sql import SQL, Composable, Identifier
@@ -137,3 +141,25 @@ def rename_indexes_constraints(
)
return inner_migration
+
+
+def add_index(model_name: str, index: Index) -> AddIndex:
+ """
+ Gracefully handle concurrent index migration setting
+ when adding indexes with ./manage.py migrate
+ """
+ if settings.MIGRATIONS_ADD_REMOVE_INDEXES_CONCURRENTLY:
+ return AddIndexConcurrently(model_name, index)
+ else:
+ return AddIndex(model_name, index)
+
+
+def remove_index(model_name: str, name: str) -> RemoveIndex:
+ """
+ Gracefully handle concurrent index migration setting
+ when removing indexes with ./manage.py migrate
+ """
+ if settings.MIGRATIONS_ADD_REMOVE_INDEXES_CONCURRENTLY:
+ return RemoveIndexConcurrently(model_name, name)
+ else:
+ return RemoveIndex(model_name, name)
diff --git a/zerver/migrations/0244_message_copy_pub_date_to_date_sent.py b/zerver/migrations/0244_message_copy_pub_date_to_date_sent.py
index 131d3bc1df6..c334d58a9c5 100644
--- a/zerver/migrations/0244_message_copy_pub_date_to_date_sent.py
+++ b/zerver/migrations/0244_message_copy_pub_date_to_date_sent.py
@@ -1,12 +1,13 @@
import time
-from django.contrib.postgres.operations import AddIndexConcurrently
from django.db import connection, migrations, models
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django.db.migrations.state import StateApps
from django.db.models import Min
from psycopg2.sql import SQL
+from zerver.lib.migrate import add_index
+
BATCH_SIZE = 1000
@@ -81,7 +82,7 @@ class Migration(migrations.Migration):
migrations.RunPython(copy_pub_date_to_date_sent, elidable=True),
migrations.SeparateDatabaseAndState(
database_operations=[
- AddIndexConcurrently(
+ add_index(
model_name="message",
index=models.Index("date_sent", name="zerver_message_date_sent_3b5b05d8"),
),
diff --git a/zerver/migrations/0279_message_recipient_subject_indexes.py b/zerver/migrations/0279_message_recipient_subject_indexes.py
index 6c4b29855fc..63e02307d2f 100644
--- a/zerver/migrations/0279_message_recipient_subject_indexes.py
+++ b/zerver/migrations/0279_message_recipient_subject_indexes.py
@@ -1,20 +1,22 @@
# Generated by Django 2.2.12 on 2020-04-30 00:35
-from django.contrib.postgres.operations import AddIndexConcurrently
+from django.conf import settings
from django.db import migrations, models
from django.db.models import F
from django.db.models.functions import Upper
+from zerver.lib.migrate import add_index
+
class Migration(migrations.Migration):
- atomic = False
+ atomic = not settings.MIGRATIONS_ADD_REMOVE_INDEXES_CONCURRENTLY
dependencies = [
("zerver", "0278_remove_userprofile_alert_words"),
]
operations = [
- AddIndexConcurrently(
+ add_index(
model_name="message",
index=models.Index(
"recipient",
@@ -23,7 +25,7 @@ class Migration(migrations.Migration):
name="zerver_message_recipient_upper_subject",
),
),
- AddIndexConcurrently(
+ add_index(
model_name="message",
index=models.Index(
"recipient",
diff --git a/zerver/migrations/0313_finish_is_user_active_migration.py b/zerver/migrations/0313_finish_is_user_active_migration.py
index 1989c9b3023..ea55643df41 100644
--- a/zerver/migrations/0313_finish_is_user_active_migration.py
+++ b/zerver/migrations/0313_finish_is_user_active_migration.py
@@ -1,10 +1,11 @@
# Generated by Django 3.1.5 on 2021-02-07 14:56
-from django.contrib.postgres.operations import AddIndexConcurrently
from django.db import connection, migrations, models
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django.db.migrations.state import StateApps
+from zerver.lib.migrate import add_index
+
def backfill_is_user_active(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor) -> None:
Subscription = apps.get_model("zerver", "Subscription")
@@ -50,7 +51,7 @@ class Migration(migrations.Migration):
name="is_user_active",
field=models.BooleanField(),
),
- AddIndexConcurrently(
+ add_index(
model_name="subscription",
index=models.Index(
condition=models.Q(("active", True), ("is_user_active", True)),
diff --git a/zerver/migrations/0472_add_message_realm_id_indexes.py b/zerver/migrations/0472_add_message_realm_id_indexes.py
index 485a26ef491..3a302d8baff 100644
--- a/zerver/migrations/0472_add_message_realm_id_indexes.py
+++ b/zerver/migrations/0472_add_message_realm_id_indexes.py
@@ -1,10 +1,12 @@
import django.db.models.functions.text
-from django.contrib.postgres.operations import AddIndexConcurrently
+from django.conf import settings
from django.db import migrations, models
+from zerver.lib.migrate import add_index
+
class Migration(migrations.Migration):
- atomic = False
+ atomic = not settings.MIGRATIONS_ADD_REMOVE_INDEXES_CONCURRENTLY
dependencies = [
("zerver", "0471_alter_realm_create_multiuse_invite_group"),
@@ -12,7 +14,7 @@ class Migration(migrations.Migration):
# The non-realm_id-prefixed versions will be removed in the next migration.
operations = [
- AddIndexConcurrently(
+ add_index(
model_name="message",
index=models.Index(
models.F("realm_id"),
@@ -21,7 +23,7 @@ class Migration(migrations.Migration):
name="zerver_message_realm_recipient_id",
),
),
- AddIndexConcurrently(
+ add_index(
model_name="message",
index=models.Index(
models.F("realm_id"),
@@ -30,7 +32,7 @@ class Migration(migrations.Migration):
name="zerver_message_realm_recipient_date_sent",
),
),
- AddIndexConcurrently(
+ add_index(
model_name="message",
index=models.Index(
models.F("realm_id"),
@@ -39,13 +41,13 @@ class Migration(migrations.Migration):
name="zerver_message_realm_sender_recipient",
),
),
- AddIndexConcurrently(
+ add_index(
model_name="message",
index=models.Index(
models.F("realm_id"), models.F("date_sent"), name="zerver_message_realm_date_sent"
),
),
- AddIndexConcurrently(
+ add_index(
model_name="message",
index=models.Index(
models.F("realm_id"),
@@ -54,7 +56,7 @@ class Migration(migrations.Migration):
name="zerver_message_realm_upper_subject",
),
),
- AddIndexConcurrently(
+ add_index(
model_name="message",
index=models.Index(
models.F("realm_id"),
@@ -64,7 +66,7 @@ class Migration(migrations.Migration):
name="zerver_message_realm_recipient_upper_subject",
),
),
- AddIndexConcurrently(
+ add_index(
model_name="message",
index=models.Index(
models.F("realm_id"),
@@ -74,7 +76,7 @@ class Migration(migrations.Migration):
name="zerver_message_realm_recipient_subject",
),
),
- AddIndexConcurrently(
+ add_index(
model_name="message",
index=models.Index(
models.F("realm_id"),
@@ -82,7 +84,7 @@ class Migration(migrations.Migration):
name="zerver_message_realm_id",
),
),
- AddIndexConcurrently(
+ add_index(
model_name="scheduledmessage",
index=models.Index(
condition=models.Q(("delivered", False)),
diff --git a/zerver/migrations/0473_remove_message_non_realm_id_indexes.py b/zerver/migrations/0473_remove_message_non_realm_id_indexes.py
index cc969468b8b..7899b5e3162 100644
--- a/zerver/migrations/0473_remove_message_non_realm_id_indexes.py
+++ b/zerver/migrations/0473_remove_message_non_realm_id_indexes.py
@@ -1,28 +1,30 @@
-from django.contrib.postgres.operations import RemoveIndexConcurrently
+from django.conf import settings
from django.db import migrations
+from zerver.lib.migrate import remove_index
+
class Migration(migrations.Migration):
- atomic = False
+ atomic = not settings.MIGRATIONS_ADD_REMOVE_INDEXES_CONCURRENTLY
dependencies = [
("zerver", "0472_add_message_realm_id_indexes"),
]
operations = [
- RemoveIndexConcurrently(
+ remove_index(
model_name="message",
name="upper_subject_idx",
),
- RemoveIndexConcurrently(
+ remove_index(
model_name="message",
name="zerver_message_recipient_upper_subject",
),
- RemoveIndexConcurrently(
+ remove_index(
model_name="message",
name="zerver_message_recipient_subject",
),
- RemoveIndexConcurrently(
+ remove_index(
model_name="scheduledmessage",
name="zerver_unsent_scheduled_messages_by_user",
),
diff --git a/zerver/migrations/0485_alter_usermessage_flags_and_add_index.py b/zerver/migrations/0485_alter_usermessage_flags_and_add_index.py
index 992a550ec6f..54161168e9d 100644
--- a/zerver/migrations/0485_alter_usermessage_flags_and_add_index.py
+++ b/zerver/migrations/0485_alter_usermessage_flags_and_add_index.py
@@ -1,12 +1,14 @@
# Generated by Django 4.2.6 on 2023-10-19 06:37
import bitfield.models
-from django.contrib.postgres.operations import AddIndexConcurrently
+from django.conf import settings
from django.db import migrations, models
+from zerver.lib.migrate import add_index
+
class Migration(migrations.Migration):
- atomic = False
+ atomic = not settings.MIGRATIONS_ADD_REMOVE_INDEXES_CONCURRENTLY
dependencies = [
("zerver", "0484_preregistrationrealm_default_language"),
@@ -57,7 +59,7 @@ class Migration(migrations.Migration):
default=0,
),
),
- AddIndexConcurrently(
+ add_index(
model_name="usermessage",
index=models.Index(
"user_profile",
diff --git a/zerver/migrations/0486_clear_old_data_for_unused_usermessage_flags.py b/zerver/migrations/0486_clear_old_data_for_unused_usermessage_flags.py
index f961443ade6..4beedac21da 100644
--- a/zerver/migrations/0486_clear_old_data_for_unused_usermessage_flags.py
+++ b/zerver/migrations/0486_clear_old_data_for_unused_usermessage_flags.py
@@ -1,9 +1,10 @@
-from django.contrib.postgres.operations import AddIndexConcurrently, RemoveIndexConcurrently
from django.db import connection, migrations, models
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django.db.migrations.state import StateApps
from psycopg2.sql import SQL
+from zerver.lib.migrate import add_index, remove_index
+
def clear_old_data_for_unused_usermessage_flags(
apps: StateApps, schema_editor: BaseDatabaseSchemaEditor
@@ -52,7 +53,7 @@ class Migration(migrations.Migration):
]
operations = [
- AddIndexConcurrently(
+ add_index(
model_name="usermessage",
index=models.Index(
"id",
@@ -65,7 +66,7 @@ class Migration(migrations.Migration):
reverse_code=migrations.RunPython.noop,
elidable=True,
),
- RemoveIndexConcurrently(
+ remove_index(
model_name="usermessage",
name="zerver_usermessage_temp_clear_flags",
),
diff --git a/zerver/migrations/0510_add_realmauditlog_realm_event_type_index.py b/zerver/migrations/0510_add_realmauditlog_realm_event_type_index.py
index cc9e6b6924e..ed0821e345b 100644
--- a/zerver/migrations/0510_add_realmauditlog_realm_event_type_index.py
+++ b/zerver/migrations/0510_add_realmauditlog_realm_event_type_index.py
@@ -1,18 +1,20 @@
# Generated by Django 4.2.12 on 2024-04-17 10:33
-from django.contrib.postgres.operations import AddIndexConcurrently
+from django.conf import settings
from django.db import migrations, models
+from zerver.lib.migrate import add_index
+
class Migration(migrations.Migration):
- atomic = False
+ atomic = not settings.MIGRATIONS_ADD_REMOVE_INDEXES_CONCURRENTLY
dependencies = [
("zerver", "0509_fix_emoji_metadata"),
]
operations = [
- AddIndexConcurrently(
+ add_index(
model_name="realmauditlog",
index=models.Index(
fields=["realm", "event_type", "event_time"],
diff --git a/zerver/migrations/0520_attachment_zerver_attachment_realm_create_time.py b/zerver/migrations/0520_attachment_zerver_attachment_realm_create_time.py
index 2a3cd5b1827..8912f4b7c10 100644
--- a/zerver/migrations/0520_attachment_zerver_attachment_realm_create_time.py
+++ b/zerver/migrations/0520_attachment_zerver_attachment_realm_create_time.py
@@ -1,18 +1,20 @@
# Generated by Django 5.0.6 on 2024-05-08 01:16
-from django.contrib.postgres.operations import AddIndexConcurrently
+from django.conf import settings
from django.db import migrations, models
+from zerver.lib.migrate import add_index
+
class Migration(migrations.Migration):
- atomic = False
+ atomic = not settings.MIGRATIONS_ADD_REMOVE_INDEXES_CONCURRENTLY
dependencies = [
("zerver", "0519_archivetransaction_restored_timestamp"),
]
operations = [
- AddIndexConcurrently(
+ add_index(
model_name="attachment",
index=models.Index(
models.F("realm"),
diff --git a/zerver/migrations/0526_user_presence_backfill_last_update_id_to_0.py b/zerver/migrations/0526_user_presence_backfill_last_update_id_to_0.py
index babf4367308..cbffb348afa 100644
--- a/zerver/migrations/0526_user_presence_backfill_last_update_id_to_0.py
+++ b/zerver/migrations/0526_user_presence_backfill_last_update_id_to_0.py
@@ -1,8 +1,9 @@
-from django.contrib.postgres.operations import AddIndexConcurrently
from django.db import migrations, models
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django.db.migrations.state import StateApps
+from zerver.lib.migrate import add_index
+
def backfill_user_presence_last_update_id(
apps: StateApps, schema_editor: BaseDatabaseSchemaEditor
@@ -47,7 +48,7 @@ class Migration(migrations.Migration):
name="last_update_id",
field=models.PositiveBigIntegerField(db_index=True, default=0),
),
- AddIndexConcurrently(
+ add_index(
model_name="userpresence",
index=models.Index(
fields=["realm", "last_update_id"],
diff --git a/zerver/migrations/0528_realmauditlog_zerver_realmauditlog_user_activations_idx.py b/zerver/migrations/0528_realmauditlog_zerver_realmauditlog_user_activations_idx.py
index 2bbe6a6fb91..9bcee66b549 100644
--- a/zerver/migrations/0528_realmauditlog_zerver_realmauditlog_user_activations_idx.py
+++ b/zerver/migrations/0528_realmauditlog_zerver_realmauditlog_user_activations_idx.py
@@ -1,16 +1,18 @@
-from django.contrib.postgres.operations import AddIndexConcurrently
+from django.conf import settings
from django.db import migrations, models
+from zerver.lib.migrate import add_index
+
class Migration(migrations.Migration):
- atomic = False
+ atomic = not settings.MIGRATIONS_ADD_REMOVE_INDEXES_CONCURRENTLY
dependencies = [
("zerver", "0527_presencesequence"),
]
operations = [
- AddIndexConcurrently(
+ add_index(
model_name="realmauditlog",
index=models.Index(
# event_type__in:
diff --git a/zerver/migrations/0679_zerver_message_edit_history_id.py b/zerver/migrations/0679_zerver_message_edit_history_id.py
index 83e6d31fd25..c206a948f3a 100644
--- a/zerver/migrations/0679_zerver_message_edit_history_id.py
+++ b/zerver/migrations/0679_zerver_message_edit_history_id.py
@@ -1,18 +1,20 @@
# Generated by Django 5.1.6 on 2025-02-26 17:25
-from django.contrib.postgres.operations import AddIndexConcurrently
+from django.conf import settings
from django.db import migrations, models
+from zerver.lib.migrate import add_index
+
class Migration(migrations.Migration):
- atomic = False
+ atomic = not settings.MIGRATIONS_ADD_REMOVE_INDEXES_CONCURRENTLY
dependencies = [
("zerver", "0678_remove_realm_allow_edit_history"),
]
operations = [
- AddIndexConcurrently(
+ add_index(
model_name="message",
index=models.Index(
condition=models.Q(("edit_history__isnull", False)),
diff --git a/zerver/migrations/0693_add_conditional_indexes_for_topic.py b/zerver/migrations/0693_add_conditional_indexes_for_topic.py
index 53ff12a5e68..b1dd1aed3fc 100644
--- a/zerver/migrations/0693_add_conditional_indexes_for_topic.py
+++ b/zerver/migrations/0693_add_conditional_indexes_for_topic.py
@@ -1,10 +1,12 @@
import django.db.models.functions.text
-from django.contrib.postgres.operations import AddIndexConcurrently
+from django.conf import settings
from django.db import migrations, models
+from zerver.lib.migrate import add_index
+
class Migration(migrations.Migration):
- atomic = False
+ atomic = not settings.MIGRATIONS_ADD_REMOVE_INDEXES_CONCURRENTLY
dependencies = [
("zerver", "0692_alter_message_is_channel_message"),
@@ -26,7 +28,7 @@ class Migration(migrations.Migration):
new_name="zerver_message_realm_recipient_subject_all",
old_name="zerver_message_realm_recipient_subject",
),
- AddIndexConcurrently(
+ add_index(
model_name="message",
index=models.Index(
models.F("realm_id"),
@@ -36,7 +38,7 @@ class Migration(migrations.Migration):
name="zerver_message_realm_upper_subject",
),
),
- AddIndexConcurrently(
+ add_index(
model_name="message",
index=models.Index(
models.F("realm_id"),
@@ -47,7 +49,7 @@ class Migration(migrations.Migration):
name="zerver_message_realm_recipient_upper_subject",
),
),
- AddIndexConcurrently(
+ add_index(
model_name="message",
index=models.Index(
models.F("realm_id"),
diff --git a/zerver/migrations/0694_remove_message_unconditional_topic_indexes.py b/zerver/migrations/0694_remove_message_unconditional_topic_indexes.py
index 08a3f51abe2..5079b747219 100644
--- a/zerver/migrations/0694_remove_message_unconditional_topic_indexes.py
+++ b/zerver/migrations/0694_remove_message_unconditional_topic_indexes.py
@@ -1,24 +1,26 @@
-from django.contrib.postgres.operations import RemoveIndexConcurrently
+from django.conf import settings
from django.db import migrations
+from zerver.lib.migrate import remove_index
+
class Migration(migrations.Migration):
- atomic = False
+ atomic = not settings.MIGRATIONS_ADD_REMOVE_INDEXES_CONCURRENTLY
dependencies = [
("zerver", "0693_add_conditional_indexes_for_topic"),
]
operations = [
- RemoveIndexConcurrently(
+ remove_index(
model_name="message",
name="zerver_message_realm_upper_subject_all",
),
- RemoveIndexConcurrently(
+ remove_index(
model_name="message",
name="zerver_message_realm_recipient_upper_subject_all",
),
- RemoveIndexConcurrently(
+ remove_index(
model_name="message",
name="zerver_message_realm_recipient_subject_all",
),
diff --git a/zerver/migrations/0741_pushdevice_zerver_pushdevice_user_bouncer_device_id_idx.py b/zerver/migrations/0741_pushdevice_zerver_pushdevice_user_bouncer_device_id_idx.py
index 35301052c56..b9a876984da 100644
--- a/zerver/migrations/0741_pushdevice_zerver_pushdevice_user_bouncer_device_id_idx.py
+++ b/zerver/migrations/0741_pushdevice_zerver_pushdevice_user_bouncer_device_id_idx.py
@@ -1,18 +1,20 @@
# Generated by Django 5.2.4 on 2025-07-22 11:57
-from django.contrib.postgres.operations import AddIndexConcurrently
+from django.conf import settings
from django.db import migrations, models
+from zerver.lib.migrate import add_index
+
class Migration(migrations.Migration):
- atomic = False
+ atomic = not settings.MIGRATIONS_ADD_REMOVE_INDEXES_CONCURRENTLY
dependencies = [
("zerver", "0740_pushdevicetoken_apns_case_insensitive"),
]
operations = [
- AddIndexConcurrently(
+ add_index(
model_name="pushdevice",
index=models.Index(
condition=models.Q(("bouncer_device_id__isnull", False)),
diff --git a/zerver/migrations/0742_usermessage_zerver_usermessage_is_private_unread_message_id.py b/zerver/migrations/0742_usermessage_zerver_usermessage_is_private_unread_message_id.py
index 4561718db18..7b24a154288 100644
--- a/zerver/migrations/0742_usermessage_zerver_usermessage_is_private_unread_message_id.py
+++ b/zerver/migrations/0742_usermessage_zerver_usermessage_is_private_unread_message_id.py
@@ -1,15 +1,18 @@
-from django.contrib.postgres.operations import AddIndexConcurrently
+from django.conf import settings
from django.db import migrations, models
+from zerver.lib.migrate import add_index
+
class Migration(migrations.Migration):
- atomic = False
+ atomic = not settings.MIGRATIONS_ADD_REMOVE_INDEXES_CONCURRENTLY
+
dependencies = [
("zerver", "0741_pushdevice_zerver_pushdevice_user_bouncer_device_id_idx"),
]
operations = [
- AddIndexConcurrently(
+ add_index(
model_name="usermessage",
index=models.Index(
models.F("user_profile"),
diff --git a/zerver/migrations/0755_usermessage_zerver_usermessage_message_active_mobile_push_notification_idx.py b/zerver/migrations/0755_usermessage_zerver_usermessage_message_active_mobile_push_notification_idx.py
index 86cda0d6515..b707e202707 100644
--- a/zerver/migrations/0755_usermessage_zerver_usermessage_message_active_mobile_push_notification_idx.py
+++ b/zerver/migrations/0755_usermessage_zerver_usermessage_message_active_mobile_push_notification_idx.py
@@ -1,17 +1,20 @@
# Generated by Django 5.2.7 on 2025-10-29 05:28
-from django.contrib.postgres.operations import AddIndexConcurrently
+from django.conf import settings
from django.db import migrations, models
+from zerver.lib.migrate import add_index
+
class Migration(migrations.Migration):
- atomic = False
+ atomic = not settings.MIGRATIONS_ADD_REMOVE_INDEXES_CONCURRENTLY
+
dependencies = [
("zerver", "0754_merge_20251014_1855"),
]
operations = [
- AddIndexConcurrently(
+ add_index(
model_name="usermessage",
index=models.Index(
models.F("message"),
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
index 8b6b9e43464..131830699d6 100644
--- a/zerver/migrations/0807_usertopic_zerver_usertopic_user_visibility_recipient_idx.py
+++ b/zerver/migrations/0807_usertopic_zerver_usertopic_user_visibility_recipient_idx.py
@@ -1,19 +1,21 @@
# 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.conf import settings
from django.db import migrations, models
+from zerver.lib.migrate import add_index
+
class Migration(migrations.Migration):
- atomic = False
+ atomic = not settings.MIGRATIONS_ADD_REMOVE_INDEXES_CONCURRENTLY
dependencies = [
("zerver", "0806_stream_default_push_notifications"),
]
operations = [
- AddIndexConcurrently(
+ add_index(
model_name="usertopic",
index=models.Index(
models.F("user_profile"),
diff --git a/zerver/migrations/0808_message_topic_indexes_nulls_first.py b/zerver/migrations/0808_message_topic_indexes_nulls_first.py
index 93b8dee2f43..49ca6b981c9 100644
--- a/zerver/migrations/0808_message_topic_indexes_nulls_first.py
+++ b/zerver/migrations/0808_message_topic_indexes_nulls_first.py
@@ -1,10 +1,12 @@
import django.db.models.functions.text
-from django.contrib.postgres.operations import AddIndexConcurrently
+from django.conf import settings
from django.db import migrations, models
+from zerver.lib.migrate import add_index
+
class Migration(migrations.Migration):
- atomic = False
+ atomic = not settings.MIGRATIONS_ADD_REMOVE_INDEXES_CONCURRENTLY
dependencies = [
("zerver", "0807_usertopic_zerver_usertopic_user_visibility_recipient_idx"),
@@ -31,7 +33,7 @@ class Migration(migrations.Migration):
new_name="zerver_message_realm_id_nulls_last",
old_name="zerver_message_realm_id",
),
- AddIndexConcurrently(
+ add_index(
model_name="message",
index=models.Index(
models.F("realm_id"),
@@ -41,7 +43,7 @@ class Migration(migrations.Migration):
name="zerver_message_realm_upper_subject",
),
),
- AddIndexConcurrently(
+ add_index(
model_name="message",
index=models.Index(
models.F("realm_id"),
@@ -52,7 +54,7 @@ class Migration(migrations.Migration):
name="zerver_message_realm_recipient_upper_subject",
),
),
- AddIndexConcurrently(
+ add_index(
model_name="message",
index=models.Index(
models.F("realm_id"),
@@ -63,7 +65,7 @@ class Migration(migrations.Migration):
name="zerver_message_realm_recipient_subject",
),
),
- AddIndexConcurrently(
+ add_index(
model_name="message",
index=models.Index(
models.F("realm_id"),
diff --git a/zerver/migrations/0809_remove_message_nulls_last_topic_indexes.py b/zerver/migrations/0809_remove_message_nulls_last_topic_indexes.py
index 17ae7152727..90a4712b832 100644
--- a/zerver/migrations/0809_remove_message_nulls_last_topic_indexes.py
+++ b/zerver/migrations/0809_remove_message_nulls_last_topic_indexes.py
@@ -1,28 +1,30 @@
-from django.contrib.postgres.operations import RemoveIndexConcurrently
+from django.conf import settings
from django.db import migrations
+from zerver.lib.migrate import remove_index
+
class Migration(migrations.Migration):
- atomic = False
+ atomic = not settings.MIGRATIONS_ADD_REMOVE_INDEXES_CONCURRENTLY
dependencies = [
("zerver", "0808_message_topic_indexes_nulls_first"),
]
operations = [
- RemoveIndexConcurrently(
+ remove_index(
model_name="message",
name="zerver_message_realm_upper_subject_nulls_last",
),
- RemoveIndexConcurrently(
+ remove_index(
model_name="message",
name="zerver_message_realm_recipient_upper_subject_nulls_last",
),
- RemoveIndexConcurrently(
+ remove_index(
model_name="message",
name="zerver_message_realm_recipient_subject_nulls_last",
),
- RemoveIndexConcurrently(
+ remove_index(
model_name="message",
name="zerver_message_realm_id_nulls_last",
),
diff --git a/zilencer/migrations/0045_remoterealmauditlog_zilencer_remoterealmauditlog_server_realm_and_more.py b/zilencer/migrations/0045_remoterealmauditlog_zilencer_remoterealmauditlog_server_realm_and_more.py
index ce9b0253b98..0b18e73054f 100644
--- a/zilencer/migrations/0045_remoterealmauditlog_zilencer_remoterealmauditlog_server_realm_and_more.py
+++ b/zilencer/migrations/0045_remoterealmauditlog_zilencer_remoterealmauditlog_server_realm_and_more.py
@@ -1,18 +1,20 @@
# Generated by Django 4.2.7 on 2023-12-05 19:33
-from django.contrib.postgres.operations import AddIndexConcurrently
+from django.conf import settings
from django.db import migrations, models
+from zerver.lib.migrate import add_index
+
class Migration(migrations.Migration):
- atomic = False
+ atomic = not settings.MIGRATIONS_ADD_REMOVE_INDEXES_CONCURRENTLY
dependencies = [
("zilencer", "0044_remoterealmbillinguser"),
]
operations = [
- AddIndexConcurrently(
+ add_index(
model_name="remoterealmauditlog",
index=models.Index(
condition=models.Q(("remote_realm__isnull", True)),
@@ -20,7 +22,7 @@ class Migration(migrations.Migration):
name="zilencer_remoterealmauditlog_server_realm",
),
),
- AddIndexConcurrently(
+ add_index(
model_name="remoterealmauditlog",
index=models.Index(
condition=models.Q(("remote_realm__isnull", True)),
@@ -28,7 +30,7 @@ class Migration(migrations.Migration):
name="zilencer_remoterealmauditlog_server",
),
),
- AddIndexConcurrently(
+ add_index(
model_name="remoterealmcount",
index=models.Index(
condition=models.Q(("remote_realm__isnull", True)),
@@ -36,7 +38,7 @@ class Migration(migrations.Migration):
name="zilencer_remoterealmcount_server_realm",
),
),
- AddIndexConcurrently(
+ add_index(
model_name="remoterealmcount",
index=models.Index(
condition=models.Q(("remote_realm__isnull", True)),
diff --git a/zilencer/migrations/0070_remove_remoterealmauditlog_zilencer_remoterealmauditlog_synced_billing_events_and_more.py b/zilencer/migrations/0070_remove_remoterealmauditlog_zilencer_remoterealmauditlog_synced_billing_events_and_more.py
index 576c7a4c02b..d81550c0b81 100644
--- a/zilencer/migrations/0070_remove_remoterealmauditlog_zilencer_remoterealmauditlog_synced_billing_events_and_more.py
+++ b/zilencer/migrations/0070_remove_remoterealmauditlog_zilencer_remoterealmauditlog_synced_billing_events_and_more.py
@@ -1,22 +1,24 @@
# Generated by Django 5.2.12 on 2026-04-08 16:02
-from django.contrib.postgres.operations import AddIndexConcurrently, RemoveIndexConcurrently
+from django.conf import settings
from django.db import migrations, models
+from zerver.lib.migrate import add_index, remove_index
+
class Migration(migrations.Migration):
- atomic = False
+ atomic = not settings.MIGRATIONS_ADD_REMOVE_INDEXES_CONCURRENTLY
dependencies = [
("zilencer", "0069_remove_remotepushdevice_push_account_id"),
]
operations = [
- RemoveIndexConcurrently(
+ remove_index(
model_name="remoterealmauditlog",
name="zilencer_remoterealmauditlog_synced_billing_events",
),
- AddIndexConcurrently(
+ add_index(
model_name="remoterealmauditlog",
index=models.Index(
condition=models.Q(
diff --git a/zproject/default_settings.py b/zproject/default_settings.py
index 926bbda403e..65e399d94fe 100644
--- a/zproject/default_settings.py
+++ b/zproject/default_settings.py
@@ -789,3 +789,13 @@ SCIM_CONFIG: dict[str, SCIMConfigDict] = {}
# Minimum number of subscribers in a channel for us to no longer
# send full subscriber data to the client.
MIN_PARTIAL_SUBSCRIBERS_CHANNEL_SIZE = 1000
+
+# Whether we should use CREATE/DROP INDEX CONCURRENTLY
+# during database migrations. Otherwise, regular
+# CREATE/DROP INDEX will be used, allowing relevant migrations
+# to run in atomic transactions. The latter gives simpler
+# behavior of migrations, at the cost of locking db tables,
+# and is the recommended mode for self-hosted deployments
+# which tend to have less data and run migrations with the server
+# down anyway.
+MIGRATIONS_ADD_REMOVE_INDEXES_CONCURRENTLY = False