From d42df2786e493aadbb2f258b80d5cc00a9d2ecf0 Mon Sep 17 00:00:00 2001 From: Eric Eastwood Date: Thu, 11 May 2023 23:39:43 -0500 Subject: [PATCH] Only select state_group when we have to --- synapse/storage/databases/state/bg_updates.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/synapse/storage/databases/state/bg_updates.py b/synapse/storage/databases/state/bg_updates.py index 7b62e21e7b..00830cf9f9 100644 --- a/synapse/storage/databases/state/bg_updates.py +++ b/synapse/storage/databases/state/bg_updates.py @@ -167,6 +167,8 @@ class StateGroupBackgroundUpdateStore(SQLBaseStore): # something that's simpler and compatible with both Database engines. select_clause_list.append( wrap_union_if_postgres( + # We only select `state_group` here for use in the `ORDER` + # clause later after the `UNION` f""" SELECT type, state_key, event_id, state_group FROM state_groups_state @@ -203,7 +205,7 @@ class StateGroupBackgroundUpdateStore(SQLBaseStore): if isinstance(self.database_engine, PostgresEngine): overall_select_clause = f""" SELECT DISTINCT ON (type, state_key) - type, state_key, event_id, state_group + type, state_key, event_id FROM state_groups_state WHERE state_group IN ( SELECT state_group FROM sgs @@ -215,7 +217,7 @@ class StateGroupBackgroundUpdateStore(SQLBaseStore): # some potential duplicate (type, state_key) pairs and then only use the # first of each kind we see. overall_select_clause = f""" - SELECT type, state_key, event_id, state_group + SELECT type, state_key, event_id FROM state_groups_state WHERE state_group IN ( SELECT state_group FROM sgs @@ -229,7 +231,9 @@ class StateGroupBackgroundUpdateStore(SQLBaseStore): txn.execute(sql % (overall_select_clause,), args) for row in txn: - typ, state_key, event_id, _ = row + # The `*_` rest syntax is to ignore the `state_group` column which we + # only select in the optimized case + typ, state_key, event_id, *_ = row key = (intern_string(typ), intern_string(state_key)) # Deal with the potential duplicate (type, state_key) pairs from the # SQLite specific query above. We only want to use the first row which