diff --git a/changelog.d/20495.misc b/changelog.d/20495.misc new file mode 100644 index 0000000000..878440c5ec --- /dev/null +++ b/changelog.d/20495.misc @@ -0,0 +1 @@ +Mitigate potential database performance issues with Postgres 18 by updating the statistics for `device_lists_changes_in_room`. diff --git a/synapse/storage/schema/main/delta/95/03_device_lists_changes_in_room_stats.sql.postgres b/synapse/storage/schema/main/delta/95/03_device_lists_changes_in_room_stats.sql.postgres new file mode 100644 index 0000000000..0e8eddf601 --- /dev/null +++ b/synapse/storage/schema/main/delta/95/03_device_lists_changes_in_room_stats.sql.postgres @@ -0,0 +1,22 @@ +-- +-- 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: +-- . + + +-- We need to increase the number of rows to sample for `room_id` column, +-- otherwise the stats will underestimate the number of distinct rooms and make +-- the query planner use the wrong index. +ALTER TABLE device_lists_changes_in_room ALTER COLUMN room_id SET STATISTICS 1000; + +-- Manually tell the query planner that we expect the number of distinct values +-- in the `stream_id` column to be proportional to the number of rows. +ALTER TABLE device_lists_changes_in_room ALTER COLUMN stream_id SET (n_distinct = -0.05);