Speed up inserting into stream_positions (#18672)

By ensuring we don't do a no-op `UPDATE`, as this causes new tuples to
be written in postgres.
This commit is contained in:
Erik Johnston 2025-07-09 11:48:06 +01:00 committed by GitHub
parent f8a7872ddb
commit bf0370162f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 6 additions and 9 deletions

1
changelog.d/18672.misc Normal file
View File

@ -0,0 +1 @@
Minor speed up of insertion into `stream_positions` table.

View File

@ -794,20 +794,16 @@ class MultiWriterIdGenerator(AbstractStreamIdGenerator):
# We upsert the value, ensuring on conflict that we always increase the
# value (or decrease if stream goes backwards).
if isinstance(self._db.engine, PostgresEngine):
agg = "GREATEST" if self._positive else "LEAST"
else:
agg = "MAX" if self._positive else "MIN"
cmp = "<" if self._positive else ">"
sql = """
sql = f"""
INSERT INTO stream_positions (stream_name, instance_name, stream_id)
VALUES (?, ?, ?)
ON CONFLICT (stream_name, instance_name)
DO UPDATE SET
stream_id = %(agg)s(stream_positions.stream_id, EXCLUDED.stream_id)
""" % {
"agg": agg,
}
stream_id = EXCLUDED.stream_id
WHERE stream_positions.stream_id {cmp} EXCLUDED.stream_id
"""
pos = self.get_current_token_for_writer(self._instance_name)
txn.execute(sql, (self._stream_name, self._instance_name, pos))