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

Avoid table-scanning users at startup #8271

Merged
merged 4 commits into from
Sep 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/8271.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove slow table scan of `users` table from startup code.
richvdh marked this conversation as resolved.
Show resolved Hide resolved
23 changes: 13 additions & 10 deletions synapse/storage/databases/main/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
MultiWriterIdGenerator,
StreamIdGenerator,
)
from synapse.types import UserID
from synapse.util.caches.stream_change_cache import StreamChangeCache

from .account_data import AccountDataStore
Expand Down Expand Up @@ -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
compared with the configuration.
"""
logger.info("Running sanity-checks on database...")
domain = config.server_name
logger.info("Checking database for consistency with configuration...")

sql = database_engine.convert_param_style(
"SELECT COUNT(*) FROM users WHERE name NOT LIKE ?"
)
pat = "%:" + domain
cur.execute(sql, (pat,))
num_not_matching = cur.fetchall()[0][0]
if num_not_matching == 0:
# if there are any users in the database, check that the username matches our
# configured server name.

cur.execute("SELECT name FROM users LIMIT 1")
rows = cur.fetchall()
if not rows:
return

mxid = UserID.from_string(rows[0][0])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably mildly prefer get_domain_from_id here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

if mxid.domain == config.server_name:
return

raise Exception(
"Found users in database not native to %s!\n"
"You cannot changed a synapse server_name after it's been configured"
richvdh marked this conversation as resolved.
Show resolved Hide resolved
% (domain,)
% (config.server_name,)
)


Expand Down