Skip to content

Commit

Permalink
Merge pull request matrix-org#5720 from matrix-org/erikj/transactions…
Browse files Browse the repository at this point in the history
…_upsert

Use upsert when updating destination retry interval
  • Loading branch information
erikjohnston authored Jul 19, 2019
2 parents d7bd965 + ced4fda commit 7704873
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog.d/5720.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve database query performance when recording retry intervals for remote hosts.
20 changes: 20 additions & 0 deletions synapse/storage/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,26 @@ def set_destination_retry_timings(self, destination, retry_last_ts, retry_interv
def _set_destination_retry_timings(
self, txn, destination, retry_last_ts, retry_interval
):

if self.database_engine.can_native_upsert:
# Upsert retry time interval if retry_interval is zero (i.e. we're
# resetting it) or greater than the existing retry interval.

sql = """
INSERT INTO destinations (destination, retry_last_ts, retry_interval)
VALUES (?, ?, ?)
ON CONFLICT (destination) DO UPDATE SET
retry_last_ts = EXCLUDED.retry_last_ts,
retry_interval = EXCLUDED.retry_interval
WHERE
EXCLUDED.retry_interval = 0
OR destinations.retry_interval < EXCLUDED.retry_interval
"""

txn.execute(sql, (destination, retry_last_ts, retry_interval))

return

self.database_engine.lock_table(txn, "destinations")

# We need to be careful here as the data may have changed from under us
Expand Down

0 comments on commit 7704873

Please sign in to comment.