From 779d48f4de9cf0728ec8a3e99edda8d125a30eff Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Sat, 5 Nov 2022 18:21:07 +0000 Subject: [PATCH 1/2] Fix background update table-scanning `events` When this background update did its last batch, it would try to update all the events that had been inserted since the bgupdate started, which could cause a table-scan. Make sure we limit the update correctly --- changelog.d/14374.bugfix | 1 + .../storage/databases/main/events_bg_updates.py | 16 ++++++++-------- 2 files changed, 9 insertions(+), 8 deletions(-) create mode 100644 changelog.d/14374.bugfix diff --git a/changelog.d/14374.bugfix b/changelog.d/14374.bugfix new file mode 100644 index 000000000000..8226f5b65118 --- /dev/null +++ b/changelog.d/14374.bugfix @@ -0,0 +1 @@ +Fix a background database update which could cause poor database performance. diff --git a/synapse/storage/databases/main/events_bg_updates.py b/synapse/storage/databases/main/events_bg_updates.py index 6e8aeed7b4b0..9e31798ab198 100644 --- a/synapse/storage/databases/main/events_bg_updates.py +++ b/synapse/storage/databases/main/events_bg_updates.py @@ -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( @@ -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 From 27c456b0159299c95589e611164fdabafee72dad Mon Sep 17 00:00:00 2001 From: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> Date: Mon, 7 Nov 2022 13:43:43 +0000 Subject: [PATCH 2/2] Update changelog.d/14374.bugfix --- changelog.d/14374.bugfix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.d/14374.bugfix b/changelog.d/14374.bugfix index 8226f5b65118..8366cfbf8a2b 100644 --- a/changelog.d/14374.bugfix +++ b/changelog.d/14374.bugfix @@ -1 +1 @@ -Fix a background database update which could cause poor database performance. +Fix a background database update, introduced in Synapse 1.64.0, which could cause poor database performance.