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/19923.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add an index to `sliding_sync_connection_lazy_members` to speed up deleting old sliding sync connection positions.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This speeds up the cascading delete from sliding_sync_connection_positions, which without an index on connection_position requires a sequential scan of the whole table for each deleted position.

We already have an index on connection_key and we expect only two entries at a time.

Seems weird that an index would help.

8 changes: 8 additions & 0 deletions synapse/storage/databases/main/sliding_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ def __init__(
where_clause="last_used_ts IS NOT NULL",
)

self.db_pool.updates.register_background_index_update(
update_name="sliding_sync_connection_lazy_members_conn_pos_idx",
index_name="sliding_sync_connection_lazy_members_conn_pos_idx",
table="sliding_sync_connection_lazy_members",
columns=("connection_position",),
where_clause="connection_position IS NOT NULL",
)

if self.hs.config.worker.run_background_tasks:
self.clock.looping_call(
self.delete_old_sliding_sync_connections,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--
-- 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 `sliding_sync_connection_lazy_members(connection_position)`
-- so that deleting from `sliding_sync_connection_positions` is efficient. This
-- is needed because `connection_position` has an `ON DELETE CASCADE` foreign key
-- constraint, and without this index Postgres has to sequentially scan the whole
-- table for each deleted position.
--
-- This is a partial index as we only ever need to find rows with a non-NULL
-- `connection_position`.
INSERT INTO background_updates (ordering, update_name, progress_json) VALUES
(9407, 'sliding_sync_connection_lazy_members_conn_pos_idx', '{}');
Loading