From 576605e0d0d78f4d70cbd853d286492fee6b5d57 Mon Sep 17 00:00:00 2001 From: Nick Mills-Barrett Date: Sun, 5 Nov 2023 11:48:31 +0000 Subject: [PATCH] Revert "Rewrite highest priority rule txn to use FOR UPDATE in one query" This reverts commit 2ec17da8985afb78ae619c72d7eaebccc38afbef. # Conflicts: # synapse/storage/databases/main/push_rule.py --- synapse/storage/databases/main/push_rule.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/synapse/storage/databases/main/push_rule.py b/synapse/storage/databases/main/push_rule.py index bb11a3da5f2d..a9ce6cf220a4 100644 --- a/synapse/storage/databases/main/push_rule.py +++ b/synapse/storage/databases/main/push_rule.py @@ -506,18 +506,23 @@ def _add_push_rule_highest_priority_txn( conditions_json: str, actions_json: str, ) -> None: - sql = """ - SELECT COUNT(*), MAX(priority) FROM push_rules - WHERE user_name = ? and priority_class = ? - """ - if isinstance(self.database_engine, PostgresEngine): - sql += " FOR UPDATE" + # Postgres doesn't do FOR UPDATE on aggregate functions, so select the rows first + # then re-select the count/max below. + sql = """ + SELECT * FROM push_rules + WHERE user_name = ? and priority_class = ? + FOR UPDATE + """ else: # Annoyingly SQLite doesn't support row level locking, so lock the whole table self.database_engine.lock_table(txn, "push_rules") # find the highest priority rule in that class + sql = ( + "SELECT COUNT(*), MAX(priority) FROM push_rules" + " WHERE user_name = ? and priority_class = ?" + ) txn.execute(sql, (user_id, priority_class)) res = txn.fetchall() (how_many, highest_prio) = res[0]