From f40b352493a8f7c7b2ebce69c2feb0ff8bad966a Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Tue, 16 Dec 2025 16:17:33 -0700 Subject: [PATCH] Add indexes for quarantined media lookups --- .../databases/main/media_repository.py | 29 +++++++++++++++++++ synapse/storage/databases/main/room.py | 5 ++++ .../93/04_add_quarantined_ts_to_media.sql | 3 +- .../main/delta/93/05_quarantined_ts_index.sql | 18 ++++++++++++ 4 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 synapse/storage/schema/main/delta/93/05_quarantined_ts_index.sql diff --git a/synapse/storage/databases/main/media_repository.py b/synapse/storage/databases/main/media_repository.py index c27c68fbc2..b29cc3d685 100644 --- a/synapse/storage/databases/main/media_repository.py +++ b/synapse/storage/databases/main/media_repository.py @@ -177,6 +177,35 @@ class MediaRepositoryBackgroundUpdateStore(SQLBaseStore): ], ) + self.db_pool.updates.register_background_index_update( + update_name="local_media_repository_quarantined_ts_idx", + index_name="local_media_repository_quarantined_by_quarantined_ts_media_id", + table="local_media_repository", + where_clause="quarantined_by IS NOT NULL", + columns=[ + # We include columns in both the WHERE and ORDER BY clauses to make + # the resulting query a bit more efficient. + "quarantined_by", + "quarantined_ts", + "media_id", + ], + ) + + self.db_pool.updates.register_background_index_update( + update_name="remote_media_cache_quarantined_ts_idx", + index_name="remote_media_cache_quarantined_by_quarantined_ts_media_origin_media_id", + table="remote_media_cache", + where_clause="quarantined_by IS NOT NULL", + columns=[ + # We include columns in both the WHERE and ORDER BY clauses to make + # the resulting query a bit more efficient. + "quarantined_by", + "quarantined_ts", + "media_origin", + "media_id", + ], + ) + self.db_pool.updates.register_background_update_handler( BG_UPDATE_REMOVE_MEDIA_REPO_INDEX_WITHOUT_METHOD_2, self._drop_media_index_without_method, diff --git a/synapse/storage/databases/main/room.py b/synapse/storage/databases/main/room.py index 182e55743a..b914ae9d30 100644 --- a/synapse/storage/databases/main/room.py +++ b/synapse/storage/databases/main/room.py @@ -969,6 +969,11 @@ class RoomWorkerStore(CacheInvalidationWorkerStore): ) -> list[str]: # We order by quarantined timestamp *and* media ID (including origin, when # known) to ensure the ordering is stable for established servers. + # + # Note: these queries are backed by indexes. If the queries change, update + # them: + # - local_media_repository_quarantined_by_quarantined_ts_media_id + # - remote_media_cache_quarantined_by_quarantined_ts_media_origin_media_id if local: sql = "SELECT '' as media_origin, media_id FROM local_media_repository WHERE quarantined_by IS NOT NULL ORDER BY quarantined_ts, media_id ASC LIMIT ? OFFSET ?" else: diff --git a/synapse/storage/schema/main/delta/93/04_add_quarantined_ts_to_media.sql b/synapse/storage/schema/main/delta/93/04_add_quarantined_ts_to_media.sql index 18b76804ff..a09430aff3 100644 --- a/synapse/storage/schema/main/delta/93/04_add_quarantined_ts_to_media.sql +++ b/synapse/storage/schema/main/delta/93/04_add_quarantined_ts_to_media.sql @@ -23,5 +23,4 @@ ALTER TABLE remote_media_cache ADD COLUMN quarantined_ts BIGINT; UPDATE local_media_repository SET quarantined_ts = 0 WHERE quarantined_by IS NOT NULL; UPDATE remote_media_cache SET quarantined_ts = 0 WHERE quarantined_by IS NOT NULL; --- Note: We *probably* should have an index on quarantined_ts, but we're going --- to try to defer that to a future migration after seeing the performance impact. +-- Note: the index is added in 05_add_quarantined_by_index.sql diff --git a/synapse/storage/schema/main/delta/93/05_quarantined_ts_index.sql b/synapse/storage/schema/main/delta/93/05_quarantined_ts_index.sql new file mode 100644 index 0000000000..440658a58f --- /dev/null +++ b/synapse/storage/schema/main/delta/93/05_quarantined_ts_index.sql @@ -0,0 +1,18 @@ +-- +-- This file is licensed under the Affero General Public License (AGPL) version 3. +-- +-- Copyright (C) 2025 Element Creations, Ltd +-- +-- This program is free software: you can redistribute it and/or modify +-- it under the terms of the GNU Affero General Public License as +-- published by the Free Software Foundation, either version 3 of the +-- License, or (at your option) any later version. +-- +-- See the GNU Affero General Public License for more details: +-- . + +INSERT INTO background_updates (ordering, update_name, progress_json) VALUES + -- we should start at ordering 9305, but there's higher conflict if we steal 9306's ordering, so + -- we'll steal 9304's ordering instead. 9304 is where the applicable columns were added. + (9304, 'local_media_repository_quarantined_ts_idx', '{}'), + (9305, 'remote_media_cache_quarantined_ts_idx', '{}');