diff --git a/synapse/storage/databases/main/media_repository.py b/synapse/storage/databases/main/media_repository.py
index 8a5ba44922..04866524e3 100644
--- a/synapse/storage/databases/main/media_repository.py
+++ b/synapse/storage/databases/main/media_repository.py
@@ -999,6 +999,15 @@ class MediaRepositoryStore(MediaRepositoryBackgroundUpdateStore):
None if the media_id doesn't exist.
"""
+ # If we don't have the index yet, performance tanks, so we return False.
+ # In the background updates, remote_media_cache_sha256_idx is created
+ # after local_media_repository_sha256_idx, which is why we only need to
+ # check for the completion of the former.
+ if not await self.db_pool.updates.has_completed_background_update(
+ "remote_media_cache_sha256_idx"
+ ):
+ return False
+
def get_matching_media_txn(
txn: LoggingTransaction, table: str, sha256: str
) -> bool:
diff --git a/synapse/storage/schema/main/delta/91/01_media_hash.sql b/synapse/storage/schema/main/delta/91/01_media_hash.sql
index 2dbd2c7df7..34a372f1ed 100644
--- a/synapse/storage/schema/main/delta/91/01_media_hash.sql
+++ b/synapse/storage/schema/main/delta/91/01_media_hash.sql
@@ -16,6 +16,13 @@ ALTER TABLE local_media_repository ADD COLUMN sha256 TEXT;
ALTER TABLE remote_media_cache ADD COLUMN sha256 TEXT;
-- Add a background updates to handle creating the new index.
-INSERT INTO background_updates (ordering, update_name, progress_json) VALUES
- (9101, 'local_media_repository_sha256_idx', '{}'),
- (9101, 'remote_media_cache_sha256_idx', '{}');
\ No newline at end of file
+--
+-- Note that the ordering of the update is not following the usual scheme. This
+-- is because when upgrading from Synapse 1.127, this index is fairly important
+-- to have up quickly, so that it doesn't tank performance, which is why it is
+-- scheduled before other background updates in the 1.127 -> 1.128 upgrade
+INSERT INTO
+ background_updates (ordering, update_name, progress_json)
+VALUES
+ (8890, 'local_media_repository_sha256_idx', '{}'),
+ (8891, 'remote_media_cache_sha256_idx', '{}');
|