Avoid table-scanning users at startup (#8271)

This takes about 10 seconds in the best case; often more.
This commit is contained in:
Richard van der Hoff 2020-09-07 16:48:52 +01:00 committed by GitHub
parent a55e2707d7
commit ef2804d27c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 11 deletions

1
changelog.d/8271.bugfix Normal file
View file

@ -0,0 +1 @@
Fix slow start times for large servers by removing a table scan of the `users` table from startup code.

View file

@ -29,6 +29,7 @@ from synapse.storage.util.id_generators import (
MultiWriterIdGenerator, MultiWriterIdGenerator,
StreamIdGenerator, StreamIdGenerator,
) )
from synapse.types import get_domain_from_id
from synapse.util.caches.stream_change_cache import StreamChangeCache from synapse.util.caches.stream_change_cache import StreamChangeCache
from .account_data import AccountDataStore from .account_data import AccountDataStore
@ -591,22 +592,24 @@ def check_database_before_upgrade(cur, database_engine, config: HomeServerConfig
"""Called before upgrading an existing database to check that it is broadly sane """Called before upgrading an existing database to check that it is broadly sane
compared with the configuration. compared with the configuration.
""" """
logger.info("Running sanity-checks on database...") logger.info("Checking database for consistency with configuration...")
domain = config.server_name
sql = database_engine.convert_param_style( # if there are any users in the database, check that the username matches our
"SELECT COUNT(*) FROM users WHERE name NOT LIKE ?" # configured server name.
)
pat = "%:" + domain cur.execute("SELECT name FROM users LIMIT 1")
cur.execute(sql, (pat,)) rows = cur.fetchall()
num_not_matching = cur.fetchall()[0][0] if not rows:
if num_not_matching == 0: return
user_domain = get_domain_from_id(rows[0][0])
if user_domain == config.server_name:
return return
raise Exception( raise Exception(
"Found users in database not native to %s!\n" "Found users in database not native to %s!\n"
"You cannot changed a synapse server_name after it's been configured" "You cannot change a synapse server_name after it's been configured"
% (domain,) % (config.server_name,)
) )