Skip to content

Commit 205d9e4

Browse files
H-Shayerikjohnston
andauthored
Improve redact_on_ban performance (#18851)
Co-authored-by: Erik Johnston <[email protected]>
1 parent 40edb10 commit 205d9e4

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

changelog.d/18851.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve database performance of [MSC4293](https://github.com/matrix-org/matrix-spec-proposals/pull/4293) - Redact on Kick/Ban.

synapse/storage/databases/main/events_worker.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
DatabasePool,
8282
LoggingDatabaseConnection,
8383
LoggingTransaction,
84+
make_tuple_in_list_sql_clause,
8485
)
8586
from synapse.storage.types import Cursor
8687
from synapse.storage.util.id_generators import (
@@ -1617,21 +1618,28 @@ def _fetch_event_rows(
16171618
# likely that some of these events may be for the same room/user combo, in
16181619
# which case we don't need to do redundant queries
16191620
to_check_set = set(to_check)
1620-
for room_and_user in to_check_set:
1621-
room_redactions_sql = "SELECT redacting_event_id, redact_end_ordering FROM room_ban_redactions WHERE room_id = ? and user_id = ?"
1622-
txn.execute(room_redactions_sql, room_and_user)
1623-
1624-
res = txn.fetchone()
1625-
# we have a redaction for a room, user_id combo - apply it to matching events
1626-
if not res:
1627-
continue
1621+
room_redaction_sql = "SELECT room_id, user_id, redacting_event_id, redact_end_ordering FROM room_ban_redactions WHERE "
1622+
(
1623+
in_list_clause,
1624+
room_redaction_args,
1625+
) = make_tuple_in_list_sql_clause(
1626+
self.database_engine, ("room_id", "user_id"), to_check_set
1627+
)
1628+
txn.execute(room_redaction_sql + in_list_clause, room_redaction_args)
1629+
for (
1630+
returned_room_id,
1631+
returned_user_id,
1632+
redacting_event_id,
1633+
redact_end_ordering,
1634+
) in txn:
16281635
for e_row in events:
16291636
e_json = json.loads(e_row.json)
16301637
room_id = e_json.get("room_id")
16311638
user_id = e_json.get("sender")
1639+
room_and_user = (returned_room_id, returned_user_id)
1640+
# check if we have a redaction match for this room, user combination
16321641
if room_and_user != (room_id, user_id):
16331642
continue
1634-
redacting_event_id, redact_end_ordering = res
16351643
if redact_end_ordering:
16361644
# Avoid redacting any events arriving *after* the membership event which
16371645
# ends an active redaction - note that this will always redact

0 commit comments

Comments
 (0)