From 3545303b950279ef72cfe4cac12fdfca0b92f2e8 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Tue, 7 Jul 2026 17:17:36 +0100 Subject: [PATCH] Add index to sliding_sync_connection_lazy_members on connection_position 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. Co-Authored-By: Claude Opus 4.8 --- changelog.d/19923.misc | 1 + .../storage/databases/main/sliding_sync.py | 8 +++++++ ...iding_sync_lazy_members_position_index.sql | 24 +++++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 changelog.d/19923.misc create mode 100644 synapse/storage/schema/main/delta/94/07_sliding_sync_lazy_members_position_index.sql diff --git a/changelog.d/19923.misc b/changelog.d/19923.misc new file mode 100644 index 00000000000..4a52d1cca2a --- /dev/null +++ b/changelog.d/19923.misc @@ -0,0 +1 @@ +Add an index to `sliding_sync_connection_lazy_members` to speed up deleting old sliding sync connection positions. diff --git a/synapse/storage/databases/main/sliding_sync.py b/synapse/storage/databases/main/sliding_sync.py index dbb9efc2acf..a5d6cd25489 100644 --- a/synapse/storage/databases/main/sliding_sync.py +++ b/synapse/storage/databases/main/sliding_sync.py @@ -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, diff --git a/synapse/storage/schema/main/delta/94/07_sliding_sync_lazy_members_position_index.sql b/synapse/storage/schema/main/delta/94/07_sliding_sync_lazy_members_position_index.sql new file mode 100644 index 00000000000..7c557638283 --- /dev/null +++ b/synapse/storage/schema/main/delta/94/07_sliding_sync_lazy_members_position_index.sql @@ -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: +-- . + + +-- 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', '{}');