@@ -278,9 +278,6 @@ def __init__(
278278 # no active writes in progress.
279279 self ._max_position_of_local_instance = self ._max_seen_allocated_stream_id
280280
281- # This goes and fills out the above state from the database.
282- self ._load_current_ids (db_conn , tables , sequence_name )
283-
284281 self ._sequence_gen = build_sequence_generator (
285282 db_conn = db_conn ,
286283 database_engine = db .engine ,
@@ -305,6 +302,13 @@ def __init__(
305302 positive = positive ,
306303 )
307304
305+ # This goes and fills out the above state from the database.
306+ # This may read on the PostgreSQL sequence, and
307+ # SequenceGenerator.check_consistency might have fixed up the sequence, which
308+ # means the SequenceGenerator needs to be setup before we read the value from
309+ # the sequence.
310+ self ._load_current_ids (db_conn , tables , sequence_name )
311+
308312 self ._max_seen_allocated_stream_id = max (
309313 self ._current_positions .values (), default = 1
310314 )
@@ -373,9 +377,7 @@ def _load_current_ids(
373377 cur .execute (f"SELECT last_value FROM { sequence_name } " )
374378 row = cur .fetchone ()
375379 assert row is not None
376- self ._current_positions [self ._instance_name ] = (
377- row [0 ] * self ._return_factor
378- )
380+ self ._current_positions [self ._instance_name ] = row [0 ]
379381
380382 # We set the `_persisted_upto_position` to be the minimum of all current
381383 # positions. If empty we use the max stream ID from the DB table.
0 commit comments