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

Commit

Permalink
Rewrite highest priority rule txn to use FOR UPDATE in one query
Browse files Browse the repository at this point in the history
  • Loading branch information
Fizzadar committed Nov 5, 2023
1 parent 4dbee68 commit 2ec17da
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions synapse/storage/databases/main/push_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,23 +465,18 @@ 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):
# Postgres doesn't do FOR SHARE 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 SHARE
"""
sql += " 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]
Expand Down

0 comments on commit 2ec17da

Please sign in to comment.