Repository navigation
Conversation
Case-insensitive user ID lookups (`get_users_by_id_case_insensitive`, used when checking whether a username is available) filter on `lower(name)`, which neither `users_name_key` nor `users_creation_ts` can serve, so every lookup scanned the whole table. Add the index as a background update. It reuses the name of the index matrix.org created by hand, so that it is rebuilt there rather than duplicated.
Contributor
Schema DiffPlease check that this looks as expected! common (sqlite)Unchanged common (postgres)Unchanged main (sqlite)--- /tmp/schema_diff_dkflz9tq/before/main/full_schemas/9999/full.sql.sqlite 2026-10-16 13:18:22.144218654 +0000
+++ /tmp/schema_diff_dkflz9tq/after/main/full_schemas/9999/full.sql.sqlite 2026-10-16 13:18:09.690286674 +0000
@@ -1253,20 +1253,21 @@
CREATE INDEX sliding_sync_membership_snapshots_membership_event_id_idx ON sliding_sync_membership_snapshots (membership_event_id) ;
CREATE INDEX sliding_sync_membership_snapshots_user_id_stream_ordering ON sliding_sync_membership_snapshots (user_id, event_stream_ordering) ;
CREATE INDEX device_federation_inbox_received_ts_index ON device_federation_inbox (received_ts) ;
CREATE UNIQUE INDEX event_txn_id_device_id_txn_id2 ON event_txn_id_device_id (user_id, device_id, room_id, txn_id) ;
CREATE UNIQUE INDEX delayed_events_idx ON delayed_events (delay_id) ;
CREATE INDEX device_lists_changes_in_room_inserted_ts_idx ON device_lists_changes_in_room (inserted_ts) WHERE inserted_ts IS NOT NULL;
CREATE INDEX sliding_sync_connections_last_used_ts_idx ON sliding_sync_connections (last_used_ts) WHERE last_used_ts IS NOT NULL;
CREATE INDEX sliding_sync_connection_lazy_members_conn_pos_idx ON sliding_sync_connection_lazy_members (connection_position) WHERE connection_position IS NOT NULL;
CREATE INDEX device_lists_changes_in_room_unconverted_idx ON device_lists_changes_in_room (user_id, device_id, room_id, stream_id) WHERE NOT converted_to_destinations;
CREATE UNIQUE INDEX e2e_cross_signing_signatures_idx3 ON e2e_cross_signing_signatures (user_id, target_user_id, target_device_id, key_id) ;
+CREATE INDEX users_lower_name ON users (lower(name)) ;
/* WARNING: Script requires that SQLITE_DBCONFIG_DEFENSIVE be disabled */
INSERT INTO appservice_stream_position VALUES('X',0);
INSERT INTO federation_stream_position VALUES('federation',-1,'master');
INSERT INTO federation_stream_position VALUES('events',-1,'master');
INSERT INTO event_push_summary_stream_ordering VALUES('X',0);
INSERT INTO user_directory_stream_pos VALUES('X',-1);
INSERT INTO stats_incremental_position VALUES('X',1);
INSERT INTO event_push_summary_last_receipt_stream_id VALUES('X',0);
INSERT INTO device_lists_changes_converted_stream_position VALUES('X',1,'',NULL);
INSERT INTO room_forgetter_stream_pos VALUES('X',0);
main (postgres)--- /tmp/schema_diff_dkflz9tq/before/main/full_schemas/9999/full.sql.postgres 2026-10-16 13:18:22.703215783 +0000
+++ /tmp/schema_diff_dkflz9tq/after/main/full_schemas/9999/full.sql.postgres 2026-10-16 13:18:10.360283287 +0000
@@ -1838,20 +1838,21 @@
CREATE INDEX user_reports_target_user_id ON user_reports USING btree (target_user_id);
CREATE INDEX user_reports_user_id ON user_reports USING btree (user_id);
CREATE UNIQUE INDEX user_signature_stream_idx ON user_signature_stream USING btree (stream_id);
CREATE UNIQUE INDEX user_threepid_id_server_idx ON user_threepid_id_server USING btree (user_id, medium, address, id_server);
CREATE INDEX user_threepids_medium_address ON user_threepids USING btree (medium, address);
CREATE INDEX user_threepids_user_id ON user_threepids USING btree (user_id);
CREATE INDEX users_creation_ts ON users USING btree (creation_ts);
CREATE INDEX users_have_local_media ON local_media_repository USING btree (user_id, created_ts);
CREATE INDEX users_in_public_rooms_r_idx ON users_in_public_rooms USING btree (room_id);
CREATE UNIQUE INDEX users_in_public_rooms_u_idx ON users_in_public_rooms USING btree (user_id, room_id);
+CREATE INDEX users_lower_name ON users USING btree (lower(name));
CREATE INDEX users_who_share_private_rooms_o_idx ON users_who_share_private_rooms USING btree (other_user_id);
CREATE INDEX users_who_share_private_rooms_r_idx ON users_who_share_private_rooms USING btree (room_id);
CREATE UNIQUE INDEX users_who_share_private_rooms_u_idx ON users_who_share_private_rooms USING btree (user_id, other_user_id, room_id);
CREATE UNIQUE INDEX worker_locks_key ON worker_locks USING btree (lock_name, lock_key);
CREATE UNIQUE INDEX worker_read_write_locks_key ON worker_read_write_locks USING btree (lock_name, lock_key, token);
CREATE UNIQUE INDEX worker_read_write_locks_mode_key ON worker_read_write_locks_mode USING btree (lock_name, lock_key);
CREATE UNIQUE INDEX worker_read_write_locks_mode_type ON worker_read_write_locks_mode USING btree (lock_name, lock_key, write_lock);
CREATE UNIQUE INDEX worker_read_write_locks_write ON worker_read_write_locks USING btree (lock_name, lock_key) WHERE write_lock;
CREATE TRIGGER check_event_stream_ordering BEFORE INSERT OR UPDATE ON current_state_events FOR EACH ROW EXECUTE FUNCTION check_event_stream_ordering();
CREATE TRIGGER check_event_stream_ordering BEFORE INSERT OR UPDATE ON local_current_membership FOR EACH ROW EXECUTE FUNCTION check_event_stream_ordering();
state (sqlite)Unchanged state (postgres)Unchanged |
sandhose
reviewed
Oct 16, 2026
| " WHERE lower(name) = lower(?)", | ||
| ("@Alice:test",), | ||
| ) | ||
| return "\n".join(str(row) for row in txn.fetchall()) |
Member
There was a problem hiding this comment.
That feels a bit excessive to parse the query plan to make sure the index is being used? Do we really need to test any of this?
Comment on lines
+2614
to
+2619
| self.db_pool.updates.register_background_index_update( | ||
| "users_lower_name", | ||
| index_name="users_lower_name", | ||
| table="users", | ||
| columns=["lower(name)"], | ||
| ) |
Member
There was a problem hiding this comment.
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;
sandhose
approved these changes
Oct 16, 2026
sandhose
enabled auto-merge (squash)
October 16, 2026 13:18
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Checking whether a username is available looks users up case-insensitively, with
WHERE lower(name) = lower(?). But there is currently no index onlower(name), so each check scans the whole table.This backs registration,
GET /_synapse/admin/v1/username_availableandGET /_synapse/mas/is_localpart_available, which Matrix Authentication Service will call as the user types in its registration form (Interactive feedback for username availability in the registration form (element-hq/matrix-authentication-service#5977)).users_lower_nameonusers (lower(name)), built as a background update, on both PostgreSQL and SQLite.Pull Request Checklist
EventStoretoEventWorkerStore.".code blocks.