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

Commit ef2804d

Browse files
authored
Avoid table-scanning users at startup (#8271)
This takes about 10 seconds in the best case; often more.
1 parent a55e270 commit ef2804d

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

changelog.d/8271.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix slow start times for large servers by removing a table scan of the `users` table from startup code.

synapse/storage/databases/main/__init__.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
MultiWriterIdGenerator,
3030
StreamIdGenerator,
3131
)
32+
from synapse.types import get_domain_from_id
3233
from synapse.util.caches.stream_change_cache import StreamChangeCache
3334

3435
from .account_data import AccountDataStore
@@ -591,22 +592,24 @@ def check_database_before_upgrade(cur, database_engine, config: HomeServerConfig
591592
"""Called before upgrading an existing database to check that it is broadly sane
592593
compared with the configuration.
593594
"""
594-
logger.info("Running sanity-checks on database...")
595-
domain = config.server_name
595+
logger.info("Checking database for consistency with configuration...")
596596

597-
sql = database_engine.convert_param_style(
598-
"SELECT COUNT(*) FROM users WHERE name NOT LIKE ?"
599-
)
600-
pat = "%:" + domain
601-
cur.execute(sql, (pat,))
602-
num_not_matching = cur.fetchall()[0][0]
603-
if num_not_matching == 0:
597+
# if there are any users in the database, check that the username matches our
598+
# configured server name.
599+
600+
cur.execute("SELECT name FROM users LIMIT 1")
601+
rows = cur.fetchall()
602+
if not rows:
603+
return
604+
605+
user_domain = get_domain_from_id(rows[0][0])
606+
if user_domain == config.server_name:
604607
return
605608

606609
raise Exception(
607610
"Found users in database not native to %s!\n"
608-
"You cannot changed a synapse server_name after it's been configured"
609-
% (domain,)
611+
"You cannot change a synapse server_name after it's been configured"
612+
% (config.server_name,)
610613
)
611614

612615

0 commit comments

Comments
 (0)