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

Avoid updating the same rows multiple times with simple_update_many_txn. #16609

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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/16609.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a long-standing bug where some queries updated the same row twice. Introduced in Synapse 1.57.0.
5 changes: 1 addition & 4 deletions synapse/storage/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -2047,10 +2047,7 @@ def simple_update_many_txn(

# List of tuples of (value values, then key values)
# (This matches the order needed for the query)
args = [tuple(x) + tuple(y) for x, y in zip(value_values, key_values)]

for ks, vs in zip(key_values, value_values):
args.append(tuple(vs) + tuple(ks))
args = [tuple(vv) + tuple(kv) for vv, kv in zip(value_values, key_values)]

# 'col1 = ?, col2 = ?, ...'
set_clause = ", ".join(f"{n} = ?" for n in value_names)
Expand Down
4 changes: 2 additions & 2 deletions tests/storage/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,12 +371,12 @@ def test_update_many(self) -> Generator["defer.Deferred[object]", object, None]:
self.mock_execute_batch.assert_called_once_with(
self.mock_txn,
"UPDATE tablename SET col3 = ? WHERE col1 = ? AND col2 = ?",
[("val3", "val1", "val2"), ("val3", "val1", "val2")],
[("val3", "val1", "val2")],
)
else:
self.mock_txn.executemany.assert_called_once_with(
"UPDATE tablename SET col3 = ? WHERE col1 = ? AND col2 = ?",
[("val3", "val1", "val2"), ("val3", "val1", "val2")],
[("val3", "val1", "val2")],
)

# key_values and value_values must be the same length.
Expand Down
Loading