Repository: matrix-org/synapse

Issue #8619: Port script cannot be run twice on the same DB
Created: 2020-10-21T17:09:45Z
The port script should be able to run multiple times on the same DB, to allow incremental porting.
This fails with:
(env) ~/temp/synapse >>> synapse_port_db --sqlite-database matrix.db.snapshot --postgres-config homeserver-postgres.yaml                                                                  [130]
Preparing sqlite3...
2020-10-21 18:04:54,927 - synapse.storage.prepare_database - 102 - INFO - ['main', 'state']: Checking existing schema version
2020-10-21 18:04:54,928 - synapse.storage.prepare_database - 107 - INFO - ['main', 'state']: Existing schema is 58 (+20 deltas)
2020-10-21 18:04:54,929 - synapse.storage.databases.main - 601 - INFO - Checking database for consistency with configuration...
2020-10-21 18:04:54,929 - synapse.storage.prepare_database - 379 - INFO - Applying schema deltas for v58
2020-10-21 18:04:54,929 - synapse.storage.prepare_database - 503 - INFO - Schema now up to date
2020-10-21 18:04:54,931 - synapse.storage.util.id_generators - 57 - INFO - initialising stream generator for events(stream_ordering)
2020-10-21 18:04:54,931 - synapse.storage.util.id_generators - 57 - INFO - initialising stream generator for events(stream_ordering)
2020-10-21 18:04:54,931 - synapse.storage.util.id_generators - 57 - INFO - initialising stream generator for ex_outlier_stream(event_stream_ordering)
Preparing psycopg2...
2020-10-21 18:04:54,946 - synapse.storage.prepare_database - 102 - INFO - ['main', 'state']: Checking existing schema version
2020-10-21 18:04:54,948 - synapse.storage.prepare_database - 107 - INFO - ['main', 'state']: Existing schema is 58 (+22 deltas)
2020-10-21 18:04:54,948 - synapse.storage.databases.main - 601 - INFO - Checking database for consistency with configuration...
2020-10-21 18:04:54,949 - synapse.storage.prepare_database - 379 - INFO - Applying schema deltas for v58
2020-10-21 18:04:54,949 - synapse.storage.prepare_database - 503 - INFO - Schema now up to date
--- Logging error ---
Traceback (most recent call last):
  File "/usr/lib/python3.8/logging/__init__.py", line 1081, in emit
    msg = self.format(record)
  File "/usr/lib/python3.8/logging/__init__.py", line 925, in format
    return fmt.format(record)
  File "/usr/lib/python3.8/logging/__init__.py", line 664, in format
    record.message = record.getMessage()
  File "/usr/lib/python3.8/logging/__init__.py", line 369, in getMessage
    msg = msg % self.args
TypeError: not enough arguments for format string
Call stack:
  File "/home/twilight/temp/synapse/env/bin/synapse_port_db", line 1097, in <module>
    start()
  File "/home/twilight/temp/synapse/env/bin/synapse_port_db", line 1092, in start
    reactor.run()
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/twisted/internet/base.py", line 1283, in run
    self.mainLoop()
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/twisted/internet/base.py", line 1292, in mainLoop
    self.runUntilCurrent()
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/metrics/__init__.py", line 538, in f
    ret = func(*args, **kwargs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/twisted/internet/base.py", line 886, in runUntilCurrent
    f(*a, **kw)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/twisted/internet/defer.py", line 460, in callback
    self._startRunCallbacks(result)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/twisted/internet/defer.py", line 568, in _startRunCallbacks
    self._runCallbacks()
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/twisted/internet/defer.py", line 654, in _runCallbacks
    current.result = callback(current.result, *args, **kw)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/twisted/internet/defer.py", line 1475, in gotResult
    _inlineCallbacks(r, g, status)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/twisted/internet/defer.py", line 1418, in _inlineCallbacks
    result = g.send(result)
  File "/home/twilight/temp/synapse/env/bin/synapse_port_db", line 547, in run
    self.postgres_store = self.build_db_store(
  File "/home/twilight/temp/synapse/env/bin/synapse_port_db", line 497, in build_db_store
    store = Store(DatabasePool(hs, db_config, engine), db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/client_ips.py", line 34, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/deviceinbox.py", line 286, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/devices.py", line 704, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/events_bg_updates.py", line 32, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/media_repository.py", line 27, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/registration.py", line 784, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/registration.py", line 39, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/room.py", line 866, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/roommember.py", line 826, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/search.py", line 92, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/state/bg_updates.py", line 184, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/state.py", line 323, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/roommember.py", line 57, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/events_worker.py", line 83, in __init__
    self._stream_id_gen = MultiWriterIdGenerator(
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/util/id_generators.py", line 263, in __init__
    self._sequence_gen.check_consistency(
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/util/sequence.py", line 118, in check_consistency
    logger.warning(
Message: 'Postgres sequence %s is behind table %s: %d < %d'
Arguments: (1, 41)
2020-10-21 18:04:54,960 - synapse_port_db - 639 - ERROR - 
Traceback (most recent call last):
  File "/home/twilight/temp/synapse/env/bin/synapse_port_db", line 547, in run
    self.postgres_store = self.build_db_store(
  File "/home/twilight/temp/synapse/env/bin/synapse_port_db", line 497, in build_db_store
    store = Store(DatabasePool(hs, db_config, engine), db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/client_ips.py", line 34, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/deviceinbox.py", line 286, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/devices.py", line 704, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/events_bg_updates.py", line 32, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/media_repository.py", line 27, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/registration.py", line 784, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/registration.py", line 39, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/room.py", line 866, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/roommember.py", line 826, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/search.py", line 92, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/state/bg_updates.py", line 184, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/state.py", line 323, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/roommember.py", line 57, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/events_worker.py", line 83, in __init__
    self._stream_id_gen = MultiWriterIdGenerator(
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/util/id_generators.py", line 263, in __init__
    self._sequence_gen.check_consistency(
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/util/sequence.py", line 123, in check_consistency
    raise IncorrectDatabaseSetup(
synapse.storage.engines._base.IncorrectDatabaseSetup: 
Postgres sequence 'events_stream_seq' is inconsistent with associated
table 'events'. This can happen if Synapse has been downgraded and
then upgraded again, or due to a bad migration.

To fix this error, shut down Synapse (including any and all workers)
and run the following SQL:

    SELECT setval('events_stream_seq', (
        SELECT GREATEST(MAX(stream_ordering), 0) FROM events
    ));

See docs/postgres.md for more information.

Traceback (most recent call last):
  File "/home/twilight/temp/synapse/env/bin/synapse_port_db", line 547, in run
    self.postgres_store = self.build_db_store(
  File "/home/twilight/temp/synapse/env/bin/synapse_port_db", line 497, in build_db_store
    store = Store(DatabasePool(hs, db_config, engine), db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/client_ips.py", line 34, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/deviceinbox.py", line 286, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/devices.py", line 704, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/events_bg_updates.py", line 32, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/media_repository.py", line 27, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/registration.py", line 784, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/registration.py", line 39, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/room.py", line 866, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/roommember.py", line 826, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/search.py", line 92, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/state/bg_updates.py", line 184, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/state.py", line 323, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/roommember.py", line 57, in __init__
    super().__init__(database, db_conn, hs)
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/events_worker.py", line 83, in __init__
    self._stream_id_gen = MultiWriterIdGenerator(
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/util/id_generators.py", line 263, in __init__
    self._sequence_gen.check_consistency(
  File "/home/twilight/temp/synapse/env/lib/python3.8/site-packages/synapse/storage/util/sequence.py", line 123, in check_consistency
    raise IncorrectDatabaseSetup(
synapse.storage.engines._base.IncorrectDatabaseSetup: 
Postgres sequence 'events_stream_seq' is inconsistent with associated
table 'events'. This can happen if Synapse has been downgraded and
then upgraded again, or due to a bad migration.

To fix this error, shut down Synapse (including any and all workers)
and run the following SQL:

    SELECT setval('events_stream_seq', (
        SELECT GREATEST(MAX(stream_ordering), 0) FROM events
    ));

See docs/postgres.md for more information.

Traceback (most recent call last):
  File "/home/twilight/temp/synapse/env/bin/synapse_port_db", line 1104, in <module>
    sys.stderr.write(end_error)
TypeError: write() argument must be str, not IncorrectDatabaseSetup

Duplicate issue #16040: synapse_port_db script can't run twice on same sqlite database
Created: 2023-07-31T23:41:14Z
Description
I tried to follow the instructions on moving from sqlite to postgres and run the synapse_port_db script on homeserver.db twice, the second time on a homeserver.db a few hours later (the rsync -Pav --append command was over in seconds).
The first run of the port script completes properly, even if it takes about 520 min (so, close to 9 hours). In fairness, the sqlite db it about 42GB.
Trying to run the script again ends abruptly with:
synapse_port_db --curses --sqlite-database /mnt/btrfs-vol/matrix/ho
meserver.db --postgres-config /home/debian/matrix-synapse/homeserver-postgres.yaml
Traceback (most recent call last):
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/_scripts/synapse_port_db.py", line 719, in run
    self.postgres_store = self.build_db_store(
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/_scripts/synapse_port_db.py", line 635, in build_db_store
    store = Store(DatabasePool(hs, db_config, engine), db_conn, hs)  # type: ignore[arg-type]
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/event_push_actions.py", line 1753, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/event_push_actions.py", line 256, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/client_ips.py", line 75, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/deviceinbox.py", line 905, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/devices.py", line 1469, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/events_bg_updates.py", line 100, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/media_repository.py", line 70, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/registration.py", line 1985, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/registration.py", line 153, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/room.py", line 1643, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/roommember.py", line 1426, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/search.py", line 136, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/state/bg_updates.py", line 280, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/state.py", line 503, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/user_directory.py", line 96, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/end_to_end_keys.py", line 85, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/e2e_room_keys.py", line 54, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/stats.py", line 120, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/account_data.py", line 64, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/filtering.py", line 43, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/profile.py", line 38, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/push_rule.py", line 339, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/push_rule.py", line 115, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/appservice.py", line 98, in __init__
    self._as_txn_seq_gen = build_sequence_generator(
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/util/sequence.py", line 283, in build_sequence_generator
    seq.check_consistency(
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/util/sequence.py", line 180, in check_consistency
    raise IncorrectDatabaseSetup(
synapse.storage.engines._base.IncorrectDatabaseSetup:
Postgres sequence 'application_services_txn_id_seq' is inconsistent with associated
table 'application_services_txns'. This can happen if Synapse has been downgraded and
then upgraded again, or due to a bad migration.

To fix this error, shut down Synapse (including any and all workers)
and run the following SQL:

    SELECT setval('application_services_txn_id_seq', (
        SELECT GREATEST(MAX(txn_id), 0) FROM application_services_txns
    ));

See docs/postgres.md for more information.


Postgres sequence 'application_services_txn_id_seq' is inconsistent with associated
table 'application_services_txns'. This can happen if Synapse has been downgraded and
then upgraded again, or due to a bad migration.

To fix this error, shut down Synapse (including any and all workers)
and run the following SQL:

    SELECT setval('application_services_txn_id_seq', (
        SELECT GREATEST(MAX(txn_id), 0) FROM application_services_txns
    ));

See docs/postgres.md for more information

Running the suggested SQL on the synapse db from psql does nothing, as far as i can see.
I've now attempted this about 4 or 5 times with the same result.
Steps to reproduce

stop synapse
rsync homeserver.db to another server with enough space
start synapse
on other server: run synapse_port_db
at migration time: stop synapse
rsync homeserver.db to other server (using --apend-verify or --append)
on other server: run synapse_port_db
postgres db appears messed up and port script crashes

Homeserver
other homeserver
Synapse Version
1.88.0+bullseye1
Installation Method
Debian packages from packages.matrix.org
Database
SQLite
Workers
Single process
Platform
Debian Bookworm 12.1 on main server
Debian Bullseye 11.7 on server running port script
Configuration
Presence is on. It's a small server (<10 users)
Relevant log output
synapse_port_db --curses --sqlite-database /mnt/btrfs-vol/matrix/ho
meserver.db --postgres-config /home/debian/matrix-synapse/homeserver-postgres.yaml
Traceback (most recent call last):
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/_scripts/synapse_port_db.py", line 719, in run
    self.postgres_store = self.build_db_store(
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/_scripts/synapse_port_db.py", line 635, in build_db_store
    store = Store(DatabasePool(hs, db_config, engine), db_conn, hs)  # type: ignore[arg-type]
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/event_push_actions.py", line 1753, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/event_push_actions.py", line 256, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/client_ips.py", line 75, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/deviceinbox.py", line 905, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/devices.py", line 1469, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/events_bg_updates.py", line 100, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/media_repository.py", line 70, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/registration.py", line 1985, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/registration.py", line 153, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/room.py", line 1643, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/roommember.py", line 1426, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/search.py", line 136, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/state/bg_updates.py", line 280, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/state.py", line 503, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/user_directory.py", line 96, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/end_to_end_keys.py", line 85, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/e2e_room_keys.py", line 54, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/stats.py", line 120, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/account_data.py", line 64, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/filtering.py", line 43, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/profile.py", line 38, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/push_rule.py", line 339, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/push_rule.py", line 115, in __init__
    super().__init__(database, db_conn, hs)
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/databases/main/appservice.py", line 98, in __init__
    self._as_txn_seq_gen = build_sequence_generator(
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/util/sequence.py", line 283, in build_sequence_generator
    seq.check_consistency(
  File "/opt/venvs/matrix-synapse/lib/python3.9/site-packages/synapse/storage/util/sequence.py", line 180, in check_consistency
    raise IncorrectDatabaseSetup(
synapse.storage.engines._base.IncorrectDatabaseSetup:
Postgres sequence 'application_services_txn_id_seq' is inconsistent with associated
table 'application_services_txns'. This can happen if Synapse has been downgraded and
then upgraded again, or due to a bad migration.

To fix this error, shut down Synapse (including any and all workers)
and run the following SQL:

    SELECT setval('application_services_txn_id_seq', (
        SELECT GREATEST(MAX(txn_id), 0) FROM application_services_txns
    ));

See docs/postgres.md for more information.


Postgres sequence 'application_services_txn_id_seq' is inconsistent with associated
table 'application_services_txns'. This can happen if Synapse has been downgraded and
then upgraded again, or due to a bad migration.

To fix this error, shut down Synapse (including any and all workers)
and run the following SQL:

    SELECT setval('application_services_txn_id_seq', (
        SELECT GREATEST(MAX(txn_id), 0) FROM application_services_txns
    ));

See docs/postgres.md for more information
Anything else that would be useful to know?
The two server configuration is only to run the port script on a second server that has enough space, the main one doesn't have enough space to fit both the sqlite db and the same db ported to postgres.
The second server never actually runs synapse, only the port script.

Closing comment (2023-07-31T23:42:21Z): Possible duplicate of #8619, #9979.