diff --git a/synapse/storage/background_updates.py b/synapse/storage/background_updates.py
index 37f2d6c644..b1e5208c76 100644
--- a/synapse/storage/background_updates.py
+++ b/synapse/storage/background_updates.py
@@ -535,6 +535,7 @@ class BackgroundUpdater:
where_clause: Optional[str] = None,
unique: bool = False,
psql_only: bool = False,
+ replaces_index: Optional[str] = None,
) -> None:
"""Helper for store classes to do a background index addition
@@ -554,6 +555,8 @@ class BackgroundUpdater:
unique: true to make a UNIQUE index
psql_only: true to only create this index on psql databases (useful
for virtual sqlite tables)
+ replaces_index: The name of an index that this index replaces.
+ The named index will be dropped upon completion of the new index.
"""
def create_index_psql(conn: Connection) -> None:
@@ -585,6 +588,12 @@ class BackgroundUpdater:
}
logger.debug("[SQL] %s", sql)
c.execute(sql)
+
+ if replaces_index is not None:
+ # We drop the old index as the new index has now been created.
+ sql = f"DROP INDEX IF EXISTS {replaces_index}"
+ logger.debug("[SQL] %s", sql)
+ c.execute(sql)
finally:
conn.set_session(autocommit=False) # type: ignore
@@ -613,6 +622,12 @@ class BackgroundUpdater:
logger.debug("[SQL] %s", sql)
c.execute(sql)
+ if replaces_index is not None:
+ # We drop the old index as the new index has now been created.
+ sql = f"DROP INDEX IF EXISTS {replaces_index}"
+ logger.debug("[SQL] %s", sql)
+ c.execute(sql)
+
if isinstance(self.db_pool.engine, engines.PostgresEngine):
runner: Optional[Callable[[Connection], None]] = create_index_psql
elif psql_only:
|