From d7cd57a8489b974ea0d09a15a9e49474260b6ec2 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Fri, 24 Jan 2025 11:27:20 +0000 Subject: [PATCH] Add row level lock --- synapse/storage/databases/state/epochs.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/synapse/storage/databases/state/epochs.py b/synapse/storage/databases/state/epochs.py index e47e4c31ae..30ba109c25 100644 --- a/synapse/storage/databases/state/epochs.py +++ b/synapse/storage/databases/state/epochs.py @@ -25,6 +25,7 @@ from synapse.storage.database import ( LoggingTransaction, make_in_list_sql_clause, ) +from synapse.storage.engines import PostgresEngine from synapse.util.stringutils import shortstr if TYPE_CHECKING: @@ -124,6 +125,15 @@ class StateEpochDataStore: LEFT JOIN state_groups_pending_deletion ON (id = state_group) WHERE {clause} """ + + if isinstance(self.db_pool.engine, PostgresEngine): + # On postgres we add a row level lock to the rows to ensure that we + # conflict with any concurrent DELETEs. `FOR KEY SHARE` lock will + # not conflict with other reads. + sql += """ + FOR KEY SHARE OF state_groups + """ + txn.execute(sql, values) state_group_to_epoch: Dict[int, Optional[int]] = {row[0]: row[1] for row in txn}