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

Commit eee6fcf

Browse files
authored
Use execute_batch instead of executemany in places (#9181)
`execute_batch` does fewer round trips in postgres than `executemany`, but does not give a correct `txn.rowcount` result after.
1 parent 1fa15b7 commit eee6fcf

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

changelog.d/9181.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Speed up batch insertion when using PostgreSQL.

synapse/storage/database.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,7 @@ def execute_batch(self, sql: str, args: Iterable[Iterable[Any]]) -> None:
267267

268268
self._do_execute(lambda *x: execute_batch(self.txn, *x), sql, args)
269269
else:
270-
for val in args:
271-
self.execute(sql, val)
270+
self.executemany(sql, args)
272271

273272
def execute_values(self, sql: str, *args: Any) -> List[Tuple]:
274273
"""Corresponds to psycopg2.extras.execute_values. Only available when
@@ -888,7 +887,7 @@ def simple_insert_many_txn(
888887
", ".join("?" for _ in keys[0]),
889888
)
890889

891-
txn.executemany(sql, vals)
890+
txn.execute_batch(sql, vals)
892891

893892
async def simple_upsert(
894893
self,

synapse/storage/databases/main/events.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ def _update_current_state_txn(
876876
WHERE room_id = ? AND type = ? AND state_key = ?
877877
)
878878
"""
879-
txn.executemany(
879+
txn.execute_batch(
880880
sql,
881881
(
882882
(
@@ -895,7 +895,7 @@ def _update_current_state_txn(
895895
)
896896
# Now we actually update the current_state_events table
897897

898-
txn.executemany(
898+
txn.execute_batch(
899899
"DELETE FROM current_state_events"
900900
" WHERE room_id = ? AND type = ? AND state_key = ?",
901901
(
@@ -907,7 +907,7 @@ def _update_current_state_txn(
907907
# We include the membership in the current state table, hence we do
908908
# a lookup when we insert. This assumes that all events have already
909909
# been inserted into room_memberships.
910-
txn.executemany(
910+
txn.execute_batch(
911911
"""INSERT INTO current_state_events
912912
(room_id, type, state_key, event_id, membership)
913913
VALUES (?, ?, ?, ?, (SELECT membership FROM room_memberships WHERE event_id = ?))
@@ -927,7 +927,7 @@ def _update_current_state_txn(
927927
# we have no record of the fact the user *was* a member of the
928928
# room but got, say, state reset out of it.
929929
if to_delete or to_insert:
930-
txn.executemany(
930+
txn.execute_batch(
931931
"DELETE FROM local_current_membership"
932932
" WHERE room_id = ? AND user_id = ?",
933933
(
@@ -938,7 +938,7 @@ def _update_current_state_txn(
938938
)
939939

940940
if to_insert:
941-
txn.executemany(
941+
txn.execute_batch(
942942
"""INSERT INTO local_current_membership
943943
(room_id, user_id, event_id, membership)
944944
VALUES (?, ?, ?, (SELECT membership FROM room_memberships WHERE event_id = ?))
@@ -1738,7 +1738,7 @@ def _set_push_actions_for_event_and_users_txn(
17381738
"""
17391739

17401740
if events_and_contexts:
1741-
txn.executemany(
1741+
txn.execute_batch(
17421742
sql,
17431743
(
17441744
(
@@ -1767,7 +1767,7 @@ def _set_push_actions_for_event_and_users_txn(
17671767

17681768
# Now we delete the staging area for *all* events that were being
17691769
# persisted.
1770-
txn.executemany(
1770+
txn.execute_batch(
17711771
"DELETE FROM event_push_actions_staging WHERE event_id = ?",
17721772
((event.event_id,) for event, _ in all_events_and_contexts),
17731773
)
@@ -1886,7 +1886,7 @@ def _update_backward_extremeties(self, txn, events):
18861886
" )"
18871887
)
18881888

1889-
txn.executemany(
1889+
txn.execute_batch(
18901890
query,
18911891
[
18921892
(e_id, ev.room_id, e_id, ev.room_id, e_id, ev.room_id, False)
@@ -1900,7 +1900,7 @@ def _update_backward_extremeties(self, txn, events):
19001900
"DELETE FROM event_backward_extremities"
19011901
" WHERE event_id = ? AND room_id = ?"
19021902
)
1903-
txn.executemany(
1903+
txn.execute_batch(
19041904
query,
19051905
[
19061906
(ev.event_id, ev.room_id)

0 commit comments

Comments
 (0)