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

SQLITE3 error "UNIQUE constraint failed: temp_user_filters.user_id" after update to 1.86.0 #15809

Closed
brunofontes opened this issue Jun 20, 2023 · 30 comments · Fixed by #15817
Closed
Labels
A-Database DB stuff like queries, migrations, new/remove columns, indexes, unexpected entries in the db T-Defect Bugs, crashes, hangs, security vulnerabilities, or other reported issues.

Comments

@brunofontes
Copy link

brunofontes commented Jun 20, 2023

Description

Hi,

I am using a Ubuntu server and had installed the matrix-synapse-py3. The update to version 1.86.0 appeared Today and I updated it as normal (via apt).

After the update the server was not getting back.

I've tried to rollback to version 1.71.0+focal1, but it complains the database has a newer version.

Steps to reproduce

  • sudo apt upgrade

After that, server did not started.

Homeserver

matrix.brunofontes.net

Synapse Version

1.86.0

Installation Method

Debian packages from packages.matrix.org

Database

SQLite

Workers

Single process

Platform

Linux mx 5.4.0-152-generic #169-Ubuntu SMP x86_64

Configuration

No response

Relevant log output

2023-06-20 19:44:04,875 - root - 348 - WARNING - main - ***** STARTING SERVER *****                                                                                                     
2023-06-20 19:44:04,875 - root - 349 - WARNING - main - Server /opt/venvs/matrix-synapse/lib/python3.8/site-packages/synapse/app/homeserver.py version 1.86.0                           
2023-06-20 19:44:04,875 - root - 354 - INFO - main - Server hostname: matrix.brunofontes.net                                                                                            
2023-06-20 19:44:04,876 - root - 355 - INFO - main - Instance name: master                                                                                                              
2023-06-20 19:44:04,876 - root - 356 - INFO - main - Twisted reactor: EPollReactor                                                                                                      
2023-06-20 19:44:04,876 - synapse.app.homeserver - 349 - INFO - main - Setting up server                                                                                                
2023-06-20 19:44:04,876 - synapse.server - 337 - INFO - main - Setting up.                                                                                                              
2023-06-20 19:44:04,880 - synapse.storage.databases - 66 - INFO - main - [database config 'master']: Checking database server                                                           
2023-06-20 19:44:04,880 - synapse.storage.databases - 69 - INFO - main - [database config 'master']: Preparing for databases ['main', 'state']                                          
2023-06-20 19:44:04,881 - synapse.storage.prepare_database - 120 - INFO - main - ['main', 'state']: Checking existing schema version                                                    
2023-06-20 19:44:04,882 - synapse.storage.prepare_database - 124 - INFO - main - ['main', 'state']: Existing schema is 77 (+5 deltas)                                                   
2023-06-20 19:44:04,882 - synapse.storage.databases.main - 288 - INFO - main - Checking database for consistency with configuration...                                                  
2023-06-20 19:44:04,882 - synapse.storage.prepare_database - 418 - INFO - main - Applying schema deltas for v77                                                                         
2023-06-20 19:44:04,883 - synapse.storage.prepare_database - 418 - INFO - main - Applying schema deltas for v78                                                                         
2023-06-20 19:44:04,883 - synapse.storage.prepare_database - 513 - INFO - main - Running 78/01_validate_and_update_profiles.py:run_upgrade                                              
2023-06-20 19:44:04,899 - synapse.storage.prepare_database - 513 - INFO - main - Running 78/02_validate_and_update_user_filters.py:run_upgrade                                          
2023-06-20 19:44:04,901 - synapse.app._base - 215 - ERROR - main - Exception during startup                                                                                             
Traceback (most recent call last):                                                                                                                                                      
  File "/opt/venvs/matrix-synapse/lib/python3.8/site-packages/synapse/app/homeserver.py", line 352, in setup hs.setup()
  File "/opt/venvs/matrix-synapse/lib/python3.8/site-packages/synapse/server.py", line 339, in setup self.datastores = Databases(self.DATASTORE_CLASS, self)
  File "/opt/venvs/matrix-synapse/lib/python3.8/site-packages/synapse/storage/databases/__init__.py", line 74, in __init__ prepare_database(
  File "/opt/venvs/matrix-synapse/lib/python3.8/site-packages/synapse/storage/prepare_database.py", line 141, in prepare_database _upgrade_existing_database(
  File "/opt/venvs/matrix-synapse/lib/python3.8/site-packages/synapse/storage/prepare_database.py", line 514, in _upgrade_existing_database module.run_upgrade(cur, database_engine, config=config)
  File "/opt/venvs/matrix-synapse/lib/python3.8/site-packages/synapse/storage/schema/main/delta/78/02_validate_and_update_user_filters.py", line 85, in run_upgrade cur.execute(copy_sql, (f"{hostname}",))
  File "/opt/venvs/matrix-synapse/lib/python3.8/site-packages/synapse/storage/database.py", line 417, in execute self._do_execute(self.txn.execute, sql, parameters)
  File "/opt/venvs/matrix-synapse/lib/python3.8/site-packages/synapse/storage/database.py", line 469, in _do_execute return func(sql, *args, **kwargs)
    sqlite3.IntegrityError: UNIQUE constraint failed: temp_user_filters.user_id

Anything else that would be useful to know?

No response

@csnover
Copy link

csnover commented Jun 20, 2023

I also encountered this issue. It was introduced by PR #15649.

The workaround I used was to delete the UNIQUE (full_user_id), UNIQUE (user_id) constraints from the CREATE TABLE temp_user_filters statement in 02_validate_and_update_user_filters.py. The correct composite unique index on (user_id, filter_id) is added later. I guess that the extra keys that are causing the problem could be copy–paste errors since this PR also updates the temp_profiles table which does the same thing (and for that table those unique keys look like they are correct).

@benjistokman
Copy link

And how do I do that?

@SiskoUrso
Copy link

SiskoUrso commented Jun 21, 2023

having this same issue as well after the 1.86.0 update, I rolled back to v1.85.2 for now and its working.

@benjistokman
Copy link

That's working for me.

@ShadowTux
Copy link

I seem to encounter similar issues. I also rolled back for the moment and started working again.

@andreas-glaser
Copy link

Same issue here

@zerobulfa
Copy link

I also encountered this issue. It was introduced by PR #15649.

The workaround I used was to delete the UNIQUE (full_user_id), UNIQUE (user_id) constraints from the CREATE TABLE temp_user_filters statement in 02_validate_and_update_user_filters.py. The correct composite unique index on (user_id, filter_id) is added later. I guess that the extra keys that are causing the problem could be copy–paste errors since this PR also updates the temp_profiles table which does the same thing (and for that table those unique keys look like they are correct).

helps me also - I think it´s a good time to migrate to Postgres... thanks @csnover

@FlyveHest
Copy link

Had the same issue, and I am running the docker image.

Temporarily fixed by rolling back to tag matrixdotorg/synapse:v1.85.2

Editiing files internally in the docker image is not really a viable solution, so a fix for this would be nice.

@renaudallard
Copy link

Same issue when building the OpenBSD port. I will wait for a fix before creating a new OpenBSD package.

@Boldairdev
Copy link

Boldairdev commented Jun 21, 2023

And how do I do that?

nano (or vi)
/opt/venvs/matrix-synapse/lib/python3.8/site-packages/synapse/storage/schema/main/delta/78/02_validate_and_update_user_filters.py

find:

       CREATE TABLE temp_user_filters (
            full_user_id text NOT NULL,
            user_id text NOT NULL,
            filter_id bigint NOT NULL,
            filter_json bytea NOT NULL,
            UNIQUE (full_user_id), 
            UNIQUE (user_id)
    )

replace with:

     CREATE TABLE temp_user_filters (
           full_user_id text NOT NULL,
           user_id text NOT NULL,
           filter_id bigint NOT NULL,
           filter_json bytea NOT NULL
       )

(do not forget to remove the comma at the end of "filter_json bytea NOT NULL"..

do a systemctl restart matrix-synapse

that did it for me.

That being said, a proper fix would be nice :)

@githubber4ever
Copy link

Got the same issue since last upgrade to the latest version, had to roll-back to matrixdotorg/synapse:v1.85.2 docker image.

@apinter
Copy link

apinter commented Jun 21, 2023

Got the same error on 2 nodes using the container image. Might indeed need to look into migrating to psql. Rollback solved the issue tho.

@MadLittleMods MadLittleMods added T-Defect Bugs, crashes, hangs, security vulnerabilities, or other reported issues. A-Database DB stuff like queries, migrations, new/remove columns, indexes, unexpected entries in the db labels Jun 21, 2023
@neilschuerch
Copy link

Same issue here.
I will stay on v1.85.2 for now.
Will this be fixed in v1.56.1 or do we need to do the workaround?

@Eirikr70
Copy link

Same issue with docker. Same resolution rolling back to v1.85.2.

@renaudallard
Copy link

Same issue here. I will stay on v1.85.2 for now. Will this be fixed in v1.56.1 or do we need to do the workaround?

I assume they will solve it in the next version, otherwise sqlite support is broken.

@alyaeanyx
Copy link

alyaeanyx commented Jun 23, 2023

Same issue on NixOS 23.05, also fixed it by downgrading to 1.85.2.

@FlyveHest
Copy link

I assume they will solve it in the next version, otherwise sqlite support is broken.

There is an ongoing PR on this, so a fix is underway

@ThatCoffeeGuy
Copy link

ThatCoffeeGuy commented Jun 23, 2023

Same here. What was the testing method used before releasing this version? If there wasn't any, this could be a great time to implement testing. This was released in a completely broken state.

I understand it's a large bump from 1.71.0, but since it was the next release in apt, this exact scenario could have been tested.

@clokep
Copy link
Member

clokep commented Jun 23, 2023

Please do not run random SQL against your database. #15817 looks like it should fix this in the next version.

@reivilibre
Copy link
Contributor

I understand it's a large bump from 1.71.0, but since it was the next release in apt, this exact scenario could have been tested.

@ThatCoffeeGuy

Every version is released with Debian/apt packages, so in principle there was no need for this large bump. (With that said, we do aim to make it so that any upgrade will work, but it can be quite complex to do so.)

@alyaeanyx
Copy link

alyaeanyx commented Jun 23, 2023

In my case, the upgrade from 1.85.2 to 1.86.0 also caused this issue, so it doesn't seem like @ThatCoffeeGuy 's large bump is the cause of it in their case.

@mattcaron
Copy link

I can confirm that this is not limited to big jumps. My dpkg.log reports that I went from 1.84.0 to 1.85.0 on 06 June and from 1.85.0 to 1.86.0 today. The previous upgrade went without issue. This latest one blew up as described above.

@mat-l
Copy link

mat-l commented Jun 24, 2023

Same here on Docker v 1.86.0. v1.85.2 is working for me:

Starting synapse with args -m synapse.app.homeserver --config-path /data/homeserver.yaml

2023-06-24 12:38:49,933 - root - 348 - WARNING - main - ***** STARTING SERVER *****

2023-06-24 12:38:49,933 - root - 349 - WARNING - main - Server /usr/local/lib/python3.11/site-packages/synapse/app/homeserver.py version 1.86.0

2023-06-24 12:38:49,933 - root - 354 - INFO - main - Server hostname: chat.mnethome.de

2023-06-24 12:38:49,933 - root - 355 - INFO - main - Instance name: master

2023-06-24 12:38:49,933 - root - 356 - INFO - main - Twisted reactor: EPollReactor

2023-06-24 12:38:49,933 - synapse.app.homeserver - 349 - INFO - main - Setting up server

2023-06-24 12:38:49,933 - synapse.server - 337 - INFO - main - Setting up.

2023-06-24 12:38:49,935 - synapse.storage.databases - 66 - INFO - main - [database config 'master']: Checking database server

2023-06-24 12:38:49,935 - synapse.storage.databases - 69 - INFO - main - [database config 'master']: Preparing for databases ['main', 'state']

2023-06-24 12:38:49,935 - synapse.storage.prepare_database - 120 - INFO - main - ['main', 'state']: Checking existing schema version

2023-06-24 12:38:49,936 - synapse.storage.prepare_database - 124 - INFO - main - ['main', 'state']: Existing schema is 77 (+4 deltas)

2023-06-24 12:38:49,936 - synapse.storage.databases.main - 288 - INFO - main - Checking database for consistency with configuration...

2023-06-24 12:38:49,936 - synapse.storage.prepare_database - 418 - INFO - main - Applying schema deltas for v77

2023-06-24 12:38:49,936 - synapse.storage.prepare_database - 526 - INFO - main - Applying schema 77/14bg_indices_event_stream_ordering.sql

2023-06-24 12:38:49,994 - synapse.storage.prepare_database - 418 - INFO - main - Applying schema deltas for v78

2023-06-24 12:38:49,995 - synapse.storage.prepare_database - 513 - INFO - main - Running 78/01_validate_and_update_profiles.py:run_upgrade

2023-06-24 12:38:50,003 - synapse.storage.prepare_database - 513 - INFO - main - Running 78/02_validate_and_update_user_filters.py:run_upgrade

2023-06-24 12:38:50,004 - synapse.app._base - 215 - ERROR - main - Exception during startup

Traceback (most recent call last):

  File "/usr/local/lib/python3.11/site-packages/synapse/app/homeserver.py", line 352, in setup

    hs.setup()

  File "/usr/local/lib/python3.11/site-packages/synapse/server.py", line 339, in setup

    self.datastores = Databases(self.DATASTORE_CLASS, self)

                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "/usr/local/lib/python3.11/site-packages/synapse/storage/databases/__init__.py", line 74, in __init__

    prepare_database(

  File "/usr/local/lib/python3.11/site-packages/synapse/storage/prepare_database.py", line 141, in prepare_database

    _upgrade_existing_database(

  File "/usr/local/lib/python3.11/site-packages/synapse/storage/prepare_database.py", line 514, in _upgrade_existing_database

    module.run_upgrade(cur, database_engine, config=config)

  File "/usr/local/lib/python3.11/site-packages/synapse/storage/schema/main/delta/78/02_validate_and_update_user_filters.py", line 85, in run_upgrade

    cur.execute(copy_sql, (f"{hostname}",))

  File "/usr/local/lib/python3.11/site-packages/synapse/storage/database.py", line 417, in execute

    self._do_execute(self.txn.execute, sql, parameters)

  File "/usr/local/lib/python3.11/site-packages/synapse/storage/database.py", line 469, in _do_execute

    return func(sql, *args, **kwargs)

           ^^^^^^^^^^^^^^^^^^^^^^^^^^

sqlite3.IntegrityError: UNIQUE constraint failed: temp_user_filters.user_id

**********************************************************************************

 Error during initialisation:

     Traceback (most recent call last):

       File "/usr/local/lib/python3.11/site-packages/synapse/app/homeserver.py", line 352, in setup

         hs.setup()

       File "/usr/local/lib/python3.11/site-packages/synapse/server.py", line 339, in setup

         self.datastores = Databases(self.DATASTORE_CLASS, self)

                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

       File "/usr/local/lib/python3.11/site-packages/synapse/storage/databases/__init__.py", line 74, in __init__

         prepare_database(

       File "/usr/local/lib/python3.11/site-packages/synapse/storage/prepare_database.py", line 141, in prepare_database

         _upgrade_existing_database(

       File "/usr/local/lib/python3.11/site-packages/synapse/storage/prepare_database.py", line 514, in _upgrade_existing_database

         module.run_upgrade(cur, database_engine, config=config)

       File "/usr/local/lib/python3.11/site-packages/synapse/storage/schema/main/delta/78/02_validate_and_update_user_filters.py", line 85, in run_upgrade

         cur.execute(copy_sql, (f"{hostname}",))

       File "/usr/local/lib/python3.11/site-packages/synapse/storage/database.py", line 417, in execute

         self._do_execute(self.txn.execute, sql, parameters)

       File "/usr/local/lib/python3.11/site-packages/synapse/storage/database.py", line 469, in _do_execute

         return func(sql, *args, **kwargs)

                ^^^^^^^^^^^^^^^^^^^^^^^^^^

     sqlite3.IntegrityError: UNIQUE constraint failed: temp_user_filters.user_id

@GregOriol
Copy link

The solution from @Boldairdev did work.

However, by looking at #15817, it could be necessary to also run
CREATE UNIQUE INDEX IF NOT EXISTS user_filters_full_user_id_unique ON user_filters (full_user_id, filter_id);

@DMRobertson
Copy link
Contributor

DMRobertson commented Jun 25, 2023

Everyone who has encountered this: please wait for a release with a fix, and in the meantime rollback to a working version.

You should NOT manually make edits to the Synapse database at all. Doing so without knowing what you're doing is highly unsafe and we cannot support anyone who has done so without our guidance.

@kenmickles
Copy link

kenmickles commented Jun 26, 2023

Also ran into this on Ubuntu 20.04 with the apt package.

This is how I rolled back, in case any future Ubuntu users are confused by the process like I was:

  1. Download the debs.tar.xz file from the release page and extract it
  2. Copy matrix-synapse-py3_1.85.2+focal1_amd64.deb to your server
  3. Install it with dpkg -i matrix-synapse-py3_1.85.2+focal1_amd64.deb

@DMRobertson
Copy link
Contributor

This is how I rolled back

I would try follow the advice in https://askubuntu.com/a/138327/610524 to downgrade to the deb in the repository itself. (I'm not sure if the debs in debs.tar.xz have been signed by the matrix.org signing key?)

@kenmickles
Copy link

@DMRobertson I tried that first and it didn't work. Are the old versions still on the package server?

@DMRobertson
Copy link
Contributor

DMRobertson commented Jun 26, 2023

I'm not familiar with the intricacies of Debian packaging, but I believe so: https://packages.matrix.org/debian/pool/main/m/matrix-synapse-py3/

@blzbrg
Copy link

blzbrg commented Jun 26, 2023

I tried to rollback on debian using apt get and the = syntax to select a particular version, but I gave up and downloaded the debs by hand from that pool link. I don't understand apt packaging very well, but I perhaps the issue is that the old versions are missing from the Packages.

My debian is on bullseye, so I looked at https://packages.matrix.org/debian/dists/bullseye/main/binary-amd64/Packages and saw that the only two versions for matrix-synapse-py3 were 1.86.0+bullseye1 and 1.71.0+bullseye1. 1.85, 1.83, and other releases in-between were not there.

Edit: looks like this is tracked by this is #14082

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A-Database DB stuff like queries, migrations, new/remove columns, indexes, unexpected entries in the db T-Defect Bugs, crashes, hangs, security vulnerabilities, or other reported issues.
Projects
None yet
Development

Successfully merging a pull request may close this issue.