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

Commit 3e87d79

Browse files
authored
Fix schema delta for servers that have not backfilled (#8396)
Fixes #8395.
1 parent c77c4a2 commit 3e87d79

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

changelog.d/8396.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add experimental support for sharding event persister.

synapse/storage/databases/main/schema/delta/58/14events_instance_name.sql.postgres

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ SELECT setval('events_stream_seq', (
2121

2222
CREATE SEQUENCE IF NOT EXISTS events_backfill_stream_seq;
2323

24+
-- If the server has never backfilled a room then doing `-MIN(...)` will give
25+
-- a negative result, hence why we do `GREATEST(...)`
2426
SELECT setval('events_backfill_stream_seq', (
25-
SELECT COALESCE(-MIN(stream_ordering), 1) FROM events
27+
SELECT GREATEST(COALESCE(-MIN(stream_ordering), 1), 1) FROM events
2628
));

synapse/storage/util/id_generators.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,12 @@ def _load_current_ids(
287287
min_stream_id = min(self._current_positions.values(), default=None)
288288

289289
if min_stream_id is None:
290+
# We add a GREATEST here to ensure that the result is always
291+
# positive. (This can be a problem for e.g. backfill streams where
292+
# the server has never backfilled).
290293
sql = """
291-
SELECT COALESCE(%(agg)s(%(id)s), 1) FROM %(table)s
294+
SELECT GREATEST(COALESCE(%(agg)s(%(id)s), 1), 1)
295+
FROM %(table)s
292296
""" % {
293297
"id": id_column,
294298
"table": table,

0 commit comments

Comments
 (0)