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

Commit 32ef24f

Browse files
authoredMay 17, 2022
Add index to cache invalidations (#12747)
For workers that rarely write to the cache the `get_all_updated_caches` query can become expensive if the worker falls behind when reading the cache.
1 parent fcf951d commit 32ef24f

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed
 

‎changelog.d/12747.bugfix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix poor database performance when reading the cache invalidation stream for large servers with lots of workers.

‎synapse/storage/databases/main/cache.py

+8
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ def __init__(
5757

5858
self._instance_name = hs.get_instance_name()
5959

60+
self.db_pool.updates.register_background_index_update(
61+
update_name="cache_invalidation_index_by_instance",
62+
index_name="cache_invalidation_stream_by_instance_instance_index",
63+
table="cache_invalidation_stream_by_instance",
64+
columns=("instance_name", "stream_id"),
65+
psql_only=True, # The table is only on postgres DBs.
66+
)
67+
6068
async def get_all_updated_caches(
6169
self, instance_name: str, last_id: int, current_id: int, limit: int
6270
) -> Tuple[List[Tuple[int, tuple]], int, bool]:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
-- Background update to clear the inboxes of hidden and deleted devices.
17+
INSERT INTO background_updates (ordering, update_name, progress_json) VALUES
18+
(6902, 'cache_invalidation_index_by_instance', '{}');

0 commit comments

Comments
 (0)
This repository has been archived.