Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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/20369.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add an index on `lower(name)` to the `users` table, to speed up checking whether a username is available.
7 changes: 7 additions & 0 deletions synapse/storage/databases/main/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2611,6 +2611,13 @@ def __init__(
columns=["creation_ts"],
)

self.db_pool.updates.register_background_index_update(
"users_lower_name",
index_name="users_lower_name",
table="users",
columns=["lower(name)"],
)
Comment on lines +2614 to +2619

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I was a little concerned that on m.org we would first drop the existing index then spend 20min recreating it, and have to do seq scan during that time. To avoid this, I renamed the index on Matrix.org, and we'll need to remember to remove it later:

ALTER INDEX users_lower_name RENAME TO temp_quenting_pr20369_users_lower_name;


self.db_pool.updates.register_background_update_handler(
"users_set_deactivated_flag", self._background_update_set_deactivated_flag
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--
-- This file is licensed under the Affero General Public License (AGPL) version 3.
--
-- Copyright (C) 2026 Element Creations Ltd
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Affero General Public License as
-- published by the Free Software Foundation, either version 3 of the
-- License, or (at your option) any later version.
--
-- See the GNU Affero General Public License for more details:
-- <https://www.gnu.org/licenses/agpl-3.0.html>.

-- Add an index on `lower(name)` to `users`, so that case-insensitive user ID
-- lookups (e.g. when checking whether a username is available) don't need to
-- scan the whole table.
INSERT INTO background_updates (ordering, update_name, progress_json) VALUES
(9502, 'users_lower_name', '{}');
Loading