Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit f2d12cc

Browse files
authored
Use partial indices on SQLIte. (#13802)
Partial indices have been supported since SQLite 3.8, but Synapse now requires >= 3.27, so we can enable support for them. This requires rebuilding previous indices which were partial on PostgreSQL, but not on SQLite.
1 parent 6302753 commit f2d12cc

File tree

4 files changed

+59
-5
lines changed

4 files changed

+59
-5
lines changed

changelog.d/13802.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use partial indices on SQLite.

synapse/storage/background_updates.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -581,22 +581,20 @@ def create_index_psql(conn: Connection) -> None:
581581
def create_index_sqlite(conn: Connection) -> None:
582582
# Sqlite doesn't support concurrent creation of indexes.
583583
#
584-
# We don't use partial indices on SQLite as it wasn't introduced
585-
# until 3.8, and wheezy and CentOS 7 have 3.7
586-
#
587584
# We assume that sqlite doesn't give us invalid indices; however
588585
# we may still end up with the index existing but the
589586
# background_updates not having been recorded if synapse got shut
590587
# down at the wrong moment - hance we use IF NOT EXISTS. (SQLite
591588
# has supported CREATE TABLE|INDEX IF NOT EXISTS since 3.3.0.)
592589
sql = (
593590
"CREATE %(unique)s INDEX IF NOT EXISTS %(name)s ON %(table)s"
594-
" (%(columns)s)"
591+
" (%(columns)s) %(where_clause)s"
595592
) % {
596593
"unique": "UNIQUE" if unique else "",
597594
"name": index_name,
598595
"table": table,
599596
"columns": ", ".join(columns),
597+
"where_clause": "WHERE " + where_clause if where_clause else "",
600598
}
601599

602600
c = conn.cursor()

synapse/storage/databases/main/event_push_actions.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1255,7 +1255,6 @@ def __init__(
12551255
table="event_push_actions",
12561256
columns=["highlight", "stream_ordering"],
12571257
where_clause="highlight=0",
1258-
psql_only=True,
12591258
)
12601259

12611260
async def get_push_actions_for_user(
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* Copyright 2022 The Matrix.org Foundation C.I.C
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
-- SQLite needs to rebuild indices which use partial indices on Postgres, but
17+
-- previously did not use them on SQLite.
18+
19+
-- Drop each index that was added with register_background_index_update AND specified
20+
-- a where_clause (that existed before this delta).
21+
22+
-- From events_bg_updates.py
23+
DROP INDEX IF EXISTS event_contains_url_index;
24+
-- There is also a redactions_censored_redacts index, but that gets dropped.
25+
DROP INDEX IF EXISTS redactions_have_censored_ts;
26+
-- There is also a PostgreSQL only index (event_contains_url_index2)
27+
-- which gets renamed to event_contains_url_index.
28+
29+
-- From roommember.py
30+
DROP INDEX IF EXISTS room_memberships_user_room_forgotten;
31+
32+
-- From presence.py
33+
DROP INDEX IF EXISTS presence_stream_state_not_offline_idx;
34+
35+
-- From media_repository.py
36+
DROP INDEX IF EXISTS local_media_repository_url_idx;
37+
38+
-- From event_push_actions.py
39+
DROP INDEX IF EXISTS event_push_actions_highlights_index;
40+
-- There's also a event_push_actions_stream_highlight_index which was previously
41+
-- PostgreSQL-only.
42+
43+
-- From state.py
44+
DROP INDEX IF EXISTS current_state_events_member_index;
45+
46+
-- Re-insert the background jobs to re-create the indices.
47+
INSERT INTO background_updates (ordering, update_name, progress_json, depends_on) VALUES
48+
(7209, 'event_contains_url_index', '{}', NULL),
49+
(7209, 'redactions_have_censored_ts_idx', '{}', NULL),
50+
(7209, 'room_membership_forgotten_idx', '{}', NULL),
51+
(7209, 'presence_stream_not_offline_index', '{}', NULL),
52+
(7209, 'local_media_repository_url_idx', '{}', NULL),
53+
(7209, 'event_push_actions_highlights_index', '{}', NULL),
54+
(7209, 'event_push_actions_stream_highlight_index', '{}', NULL),
55+
(7209, 'current_state_members_idx', '{}', NULL)
56+
ON CONFLICT (update_name) DO NOTHING;

0 commit comments

Comments
 (0)