Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 11e15d7

Browse files
authored
Fix a performance issue introduced in Synapse v1.83.0 which meant that purging rooms was very slow and database-intensive. (#15693)
* Add indices required to efficiently validate new foreign key constraints on stream_ordering * Newsfile Signed-off-by: Olivier Wilkinson (reivilibre) <[email protected]> --------- Signed-off-by: Olivier Wilkinson (reivilibre) <[email protected]>
1 parent 7477810 commit 11e15d7

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

changelog.d/15693.bugfix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a performance issue introduced in Synapse v1.83.0 which meant that purging rooms was very slow and database-intensive.

synapse/storage/databases/state/bg_updates.py

+31
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,16 @@ class StateBackgroundUpdateStore(StateGroupBackgroundUpdateStore):
261261
STATE_GROUPS_ROOM_INDEX_UPDATE_NAME = "state_groups_room_id_idx"
262262
STATE_GROUP_EDGES_UNIQUE_INDEX_UPDATE_NAME = "state_group_edges_unique_idx"
263263

264+
CURRENT_STATE_EVENTS_STREAM_ORDERING_INDEX_UPDATE_NAME = (
265+
"current_state_events_stream_ordering_idx"
266+
)
267+
ROOM_MEMBERSHIPS_STREAM_ORDERING_INDEX_UPDATE_NAME = (
268+
"room_memberships_stream_ordering_idx"
269+
)
270+
LOCAL_CURRENT_MEMBERSHIP_STREAM_ORDERING_INDEX_UPDATE_NAME = (
271+
"local_current_membership_stream_ordering_idx"
272+
)
273+
264274
def __init__(
265275
self,
266276
database: DatabasePool,
@@ -297,6 +307,27 @@ def __init__(
297307
replaces_index="state_group_edges_idx",
298308
)
299309

310+
# These indices are needed to validate the foreign key constraint
311+
# when events are deleted.
312+
self.db_pool.updates.register_background_index_update(
313+
self.CURRENT_STATE_EVENTS_STREAM_ORDERING_INDEX_UPDATE_NAME,
314+
index_name="current_state_events_stream_ordering_idx",
315+
table="current_state_events",
316+
columns=["event_stream_ordering"],
317+
)
318+
self.db_pool.updates.register_background_index_update(
319+
self.ROOM_MEMBERSHIPS_STREAM_ORDERING_INDEX_UPDATE_NAME,
320+
index_name="room_memberships_stream_ordering_idx",
321+
table="room_memberships",
322+
columns=["event_stream_ordering"],
323+
)
324+
self.db_pool.updates.register_background_index_update(
325+
self.LOCAL_CURRENT_MEMBERSHIP_STREAM_ORDERING_INDEX_UPDATE_NAME,
326+
index_name="local_current_membership_stream_ordering_idx",
327+
table="local_current_membership",
328+
columns=["event_stream_ordering"],
329+
)
330+
300331
async def _background_deduplicate_state(
301332
self, progress: dict, batch_size: int
302333
) -> int:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* Copyright 2023 The Matrix.org Foundation C.I.C.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
INSERT INTO background_updates (ordering, update_name, progress_json)
17+
VALUES
18+
(7714, 'current_state_events_stream_ordering_idx', '{}'),
19+
(7714, 'local_current_membership_stream_ordering_idx', '{}'),
20+
(7714, 'room_memberships_stream_ordering_idx', '{}');

0 commit comments

Comments
 (0)