Skip to content
Snippets Groups Projects
Unverified Commit f2049a8d authored by Richard van der Hoff's avatar Richard van der Hoff Committed by GitHub
Browse files

Fix a potentially-huge sql query (#7274)

We could end up looking up tens of thousands of events, which could cause large
amounts of data to be logged to the postgres log.
parent f1097e77
No related branches found
No related tags found
No related merge requests found
Fix a sql query introduced in Synapse 1.12.0 which could cause large amounts of logging to the postgres slow-query log.
...@@ -173,19 +173,28 @@ class EventFederationWorkerStore(EventsWorkerStore, SignatureWorkerStore, SQLBas ...@@ -173,19 +173,28 @@ class EventFederationWorkerStore(EventsWorkerStore, SignatureWorkerStore, SQLBas
for event_id in initial_events for event_id in initial_events
} }
# The sorted list of events whose auth chains we should walk.
search = [] # type: List[Tuple[int, str]]
# We need to get the depth of the initial events for sorting purposes. # We need to get the depth of the initial events for sorting purposes.
sql = """ sql = """
SELECT depth, event_id FROM events SELECT depth, event_id FROM events
WHERE %s WHERE %s
ORDER BY depth ASC
""" """
clause, args = make_in_list_sql_clause( # the list can be huge, so let's avoid looking them all up in one massive
txn.database_engine, "event_id", initial_events # query.
) for batch in batch_iter(initial_events, 1000):
txn.execute(sql % (clause,), args) clause, args = make_in_list_sql_clause(
txn.database_engine, "event_id", batch
)
txn.execute(sql % (clause,), args)
# The sorted list of events whose auth chains we should walk. # I think building a temporary list with fetchall is more efficient than
search = txn.fetchall() # type: List[Tuple[int, str]] # just `search.extend(txn)`, but this is unconfirmed
search.extend(txn.fetchall())
# sort by depth
search.sort()
# Map from event to its auth events # Map from event to its auth events
event_to_auth_events = {} # type: Dict[str, Set[str]] event_to_auth_events = {} # type: Dict[str, Set[str]]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment