From 2410335507b9fdaffb889755d76a11b0bea66f60 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Fri, 19 Jul 2019 11:34:15 +0100 Subject: [PATCH 1/2] Use upsert when updating destination retry interval --- synapse/storage/transactions.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/synapse/storage/transactions.py b/synapse/storage/transactions.py index fd1861917..c585cf6cf 100644 --- a/synapse/storage/transactions.py +++ b/synapse/storage/transactions.py @@ -196,6 +196,26 @@ class TransactionStore(SQLBaseStore): 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 From ced4fdaa84a9addcafc87ba1af6202de90dd2685 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Fri, 19 Jul 2019 11:37:37 +0100 Subject: [PATCH 2/2] Newsfile --- changelog.d/5720.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/5720.misc diff --git a/changelog.d/5720.misc b/changelog.d/5720.misc new file mode 100644 index 000000000..590f64f19 --- /dev/null +++ b/changelog.d/5720.misc @@ -0,0 +1 @@ +Improve database query performance when recording retry intervals for remote hosts.