Skip to content

Commit 1092a35

Browse files
authored
Speed up slow initial sliding syncs on large servers (#17946)
This was due to a missing index, which meant that deleting previous connections associated with the device and `conn_id` took a long time.
1 parent c5e89f5 commit 1092a35

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

changelog.d/17946.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Speed up slow initial sliding syncs on large servers.

synapse/_scripts/synapse_port_db.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
from synapse.storage.databases.main.room import RoomBackgroundUpdateStore
8989
from synapse.storage.databases.main.roommember import RoomMemberBackgroundUpdateStore
9090
from synapse.storage.databases.main.search import SearchBackgroundUpdateStore
91+
from synapse.storage.databases.main.sliding_sync import SlidingSyncStore
9192
from synapse.storage.databases.main.state import MainStateBackgroundUpdateStore
9293
from synapse.storage.databases.main.stats import StatsStore
9394
from synapse.storage.databases.main.user_directory import (
@@ -255,6 +256,7 @@ class Store(
255256
ReceiptsBackgroundUpdateStore,
256257
RelationsWorkerStore,
257258
EventFederationWorkerStore,
259+
SlidingSyncStore,
258260
):
259261
def execute(self, f: Callable[..., R], *args: Any, **kwargs: Any) -> Awaitable[R]:
260262
return self.db_pool.runInteraction(f.__name__, f, *args, **kwargs)

synapse/storage/databases/main/sliding_sync.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121
from synapse.api.errors import SlidingSyncUnknownPosition
2222
from synapse.logging.opentracing import log_kv
2323
from synapse.storage._base import SQLBaseStore, db_to_json
24-
from synapse.storage.database import LoggingTransaction
24+
from synapse.storage.database import (
25+
DatabasePool,
26+
LoggingDatabaseConnection,
27+
LoggingTransaction,
28+
)
2529
from synapse.types import MultiWriterStreamToken, RoomStreamToken
2630
from synapse.types.handlers.sliding_sync import (
2731
HaveSentRoom,
@@ -35,12 +39,28 @@
3539
from synapse.util.caches.descriptors import cached
3640

3741
if TYPE_CHECKING:
42+
from synapse.server import HomeServer
3843
from synapse.storage.databases.main import DataStore
3944

4045
logger = logging.getLogger(__name__)
4146

4247

4348
class SlidingSyncStore(SQLBaseStore):
49+
def __init__(
50+
self,
51+
database: DatabasePool,
52+
db_conn: LoggingDatabaseConnection,
53+
hs: "HomeServer",
54+
):
55+
super().__init__(database, db_conn, hs)
56+
57+
self.db_pool.updates.register_background_index_update(
58+
update_name="sliding_sync_connection_room_configs_required_state_id_idx",
59+
index_name="sliding_sync_connection_room_configs_required_state_id_idx",
60+
table="sliding_sync_connection_room_configs",
61+
columns=("required_state_id",),
62+
)
63+
4464
async def get_latest_bump_stamp_for_room(
4565
self,
4666
room_id: str,
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--
2+
-- This file is licensed under the Affero General Public License (AGPL) version 3.
3+
--
4+
-- Copyright (C) 2024 New Vector, Ltd
5+
--
6+
-- This program is free software: you can redistribute it and/or modify
7+
-- it under the terms of the GNU Affero General Public License as
8+
-- published by the Free Software Foundation, either version 3 of the
9+
-- License, or (at your option) any later version.
10+
--
11+
-- See the GNU Affero General Public License for more details:
12+
-- <https://www.gnu.org/licenses/agpl-3.0.html>.
13+
14+
15+
-- Add an index on sliding_sync_connection_room_configs(required_state_id), so
16+
-- that when we delete entries in `sliding_sync_connection_required_state` it's
17+
-- efficient for Postgres to check they've been deleted from
18+
-- `sliding_sync_connection_room_configs` too
19+
INSERT INTO background_updates (ordering, update_name, progress_json) VALUES
20+
(8805, 'sliding_sync_connection_room_configs_required_state_id_idx', '{}');

0 commit comments

Comments
 (0)