From 210923327fa77797342993cd48346aded594c709 Mon Sep 17 00:00:00 2001 From: Guacamolie Date: Wed, 19 Jul 2023 18:35:30 +0200 Subject: [PATCH] teach synapse_port_db about new sequences This makes sure we that the port script sets up the "application_services_txns" sequence (#12209). and the "un_partial_stated_event_stream_sequence" sequence (#14545), which fixes errors like the one below from appearing when trying to connect to the postgresql database after running the script: 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. Signed-off-by: Guacamolie --- changelog.d/15965.bugfix | 1 + synapse/_scripts/synapse_port_db.py | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 changelog.d/15965.bugfix diff --git a/changelog.d/15965.bugfix b/changelog.d/15965.bugfix new file mode 100644 index 000000000000..979ad0b4079b --- /dev/null +++ b/changelog.d/15965.bugfix @@ -0,0 +1 @@ +Fix `synapse_port_db` not configuring the `application_services_txns` and `un_partial_stated_event_stream_sequence` sequences. Contributed by @vanguacamolie. diff --git a/synapse/_scripts/synapse_port_db.py b/synapse/_scripts/synapse_port_db.py index 7c4aa0afa269..6446f0f6cf9c 100755 --- a/synapse/_scripts/synapse_port_db.py +++ b/synapse/_scripts/synapse_port_db.py @@ -769,6 +769,10 @@ def alter_table(txn: LoggingTransaction) -> None: await self._setup_state_group_id_seq() await self._setup_user_id_seq() await self._setup_events_stream_seqs() + await self._setup_sequence( + "un_partial_stated_event_stream_sequence", + ("un_partial_stated_event_stream",), + ) await self._setup_sequence( "device_inbox_sequence", ("device_inbox", "device_federation_outbox") ) @@ -779,6 +783,7 @@ def alter_table(txn: LoggingTransaction) -> None: await self._setup_sequence("receipts_sequence", ("receipts_linearized",)) await self._setup_sequence("presence_stream_sequence", ("presence_stream",)) await self._setup_auth_chain_sequence() + await self._setup_application_services_sequence() # Step 3. Get tables. self.progress.set_state("Fetching tables") @@ -1133,6 +1138,27 @@ def r(txn: LoggingTransaction) -> None: r, ) + async def _setup_application_services_sequence(self) -> None: + curr_tnx_id: Optional[ + int + ] = await self.sqlite_store.db_pool.simple_select_one_onecol( + table="application_services_txns", + keyvalues={}, + retcol="COALESCE(max(txn_id), 0)", + ) + + def r(txn: LoggingTransaction) -> None: + txn.execute( + "ALTER SEQUENCE application_services_txn_id_seq RESTART WITH %s", + (curr_tnx_id + 1,), + ) + + if curr_tnx_id is not None: + await self.postgres_store.db_pool.runInteraction( + "_setup_application_services_sequence", + r, + ) + ############################################## # The following is simply UI stuff