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

Commit a3d499a

Browse files
committed
Add a unique index on state_group_edges. Fixes #11779.
1 parent 2e6068b commit a3d499a

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

synapse/storage/databases/state/bg_updates.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ class StateBackgroundUpdateStore(StateGroupBackgroundUpdateStore):
195195
STATE_GROUP_DEDUPLICATION_UPDATE_NAME = "state_group_state_deduplication"
196196
STATE_GROUP_INDEX_UPDATE_NAME = "state_group_state_type_index"
197197
STATE_GROUPS_ROOM_INDEX_UPDATE_NAME = "state_groups_room_id_idx"
198+
STATE_GROUP_EDGES_UNIQUE_INDEX_UPDATE_NAME = "state_group_edges_unique_idx"
198199

199200
def __init__(
200201
self,
@@ -217,6 +218,21 @@ def __init__(
217218
columns=["room_id"],
218219
)
219220

221+
# `state_group_edges` can cause severe performance issues if duplicate
222+
# rows are introduced, which can accidentally be done by well-meaning
223+
# server admins when trying to restore a database dump, etc.
224+
# See https://github.com/matrix-org/synapse/issues/11779.
225+
# Introduce a unique index to guard against that.
226+
self.db_pool.updates.register_background_index_update(
227+
self.STATE_GROUP_EDGES_UNIQUE_INDEX_UPDATE_NAME,
228+
index_name="state_group_edges_unique_idx",
229+
table="state_group_edges",
230+
columns=["state_group", "prev_state_group"],
231+
unique=True,
232+
# The old index was on (state_group) and was not unique.
233+
replaces_index="state_group_edges_idx",
234+
)
235+
220236
async def _background_deduplicate_state(
221237
self, progress: dict, batch_size: int
222238
) -> int:
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* Copyright 2022 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) VALUES
17+
(6908, 'state_group_edges_unique_idx', '{}');

0 commit comments

Comments
 (0)