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

Fix background update table-scanning events #14374

Merged
merged 2 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/14374.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a background database update which could cause poor database performance.
richvdh marked this conversation as resolved.
Show resolved Hide resolved
16 changes: 8 additions & 8 deletions synapse/storage/databases/main/events_bg_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -1435,16 +1435,16 @@ def _populate_txn(txn: LoggingTransaction) -> bool:
),
)

endpoint = None
row = txn.fetchone()
if row:
endpoint = row[0]
else:
# if the query didn't return a row, we must be almost done. We just
# need to go up to the recorded max_stream_ordering.
endpoint = max_stream_ordering_inclusive

where_clause = "stream_ordering > ?"
args = [min_stream_ordering_exclusive]
if endpoint:
where_clause += " AND stream_ordering <= ?"
args.append(endpoint)
where_clause = "stream_ordering > ? AND stream_ordering <= ?"
args = [min_stream_ordering_exclusive, endpoint]

# now do the updates.
txn.execute(
Expand All @@ -1458,13 +1458,13 @@ def _populate_txn(txn: LoggingTransaction) -> bool:
)

logger.info(
"populated new `events` columns up to %s/%i: updated %i rows",
"populated new `events` columns up to %i/%i: updated %i rows",
endpoint,
max_stream_ordering_inclusive,
txn.rowcount,
)

if endpoint is None:
if endpoint >= max_stream_ordering_inclusive:
# we're done
return True

Expand Down