-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "receipts_linearized_uniqueness" #16138
Description
Description
We encountered a database corruption problem some weeks ago, and had to make many fixes in the database by hand to reach again a consistent state that would allow synapse to run. We were fortunately successful enough, and our instance is now running nicely once more and without any remaining corruption.
However, after this intervention, we noticed our homeserver started having lots of errors labeled sycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "receipts_linearized_uniqueness". This is probably due to this table having obtained new rows it shouldn't have during the corruption, and these lines remained in place as their presence does not directly violates the contraint − in fact, it's adding new rows that Synapse refuses as it would break the constraint.
While obviously Synapse is not responsible of external database problems, I thought it was relevant to open this issue as I find it strange that it is not able to recover from such a situation, eg. by deleting old rows before inserting new ones, in order to preserve the uniqueness constraint?
Steps to reproduce
- Encounter an unbfortunate database problem and end up with a slightly inconsistent
receipts_linearizedtable - Start synapse and check logs
Homeserver
Private
Synapse Version
1.90.0
Installation Method
Docker (matrixdotorg/synapse)
Database
PostgreSQL v15
Workers
Single process
Platform
Debian 11, no virtualization, synapse and postgre running in Docker containers
Configuration
No response
Relevant log output
2023-08-20 19:14:40,671 - synapse.federation.federation_server - 1481 - ERROR - PUT-990851- Failed to handle edu 'm.receipt'
--
Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/synapse/federation/federation_server.py", line 1477, in on_edu
await handler(origin, content)
File "/usr/local/lib/python3.11/site-packages/synapse/handlers/receipts.py", line 112, in _received_remote_receipt
await self._handle_new_receipts(receipts)
File "/usr/local/lib/python3.11/site-packages/synapse/handlers/receipts.py", line 120, in _handle_new_receipts
res = await self.store.insert_receipt(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/synapse/storage/databases/main/receipts.py", line 769, in insert_receipt
event_ts = await self.db_pool.runInteraction(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/synapse/storage/database.py", line 930, in runInteraction
return await delay_cancellation(_runInteraction())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/twisted/internet/defer.py", line 1693, in _inlineCallbacks
result = context.run(
^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/twisted/python/failure.py", line 518, in throwExceptionIntoGenerator
return g.throw(self.type, self.value, self.tb)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/synapse/storage/database.py", line 896, in _runInteraction
result = await self.runWithConnection(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/synapse/storage/database.py", line 1025, in runWithConnection
return await make_deferred_yieldable(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/twisted/python/threadpool.py", line 244, in inContext
result = inContext.theWork() # type: ignore[attr-defined]
^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/twisted/python/threadpool.py", line 260, in <lambda>
inContext.theWork = lambda: context.call( # type: ignore[attr-defined]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/twisted/python/context.py", line 117, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/twisted/python/context.py", line 82, in callWithContext
return func(*args, **kw)
^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/twisted/enterprise/adbapi.py", line 282, in _runWithConnection
result = func(conn, *args, **kw)
^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/synapse/storage/database.py", line 1018, in inner_func
return func(db_conn, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/synapse/storage/database.py", line 758, in new_transaction
r = func(cursor, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/synapse/storage/databases/main/receipts.py", line 684, in _insert_linearized_receipt_txn
self.db_pool.simple_upsert_txn(
File "/usr/local/lib/python3.11/site-packages/synapse/storage/database.py", line 1291, in simple_upsert_txn
return self.simple_upsert_txn_native_upsert(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/synapse/storage/database.py", line 1431, in simple_upsert_txn_native_upsert
txn.execute(sql, list(allvalues.values()))
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)
^^^^^^^^^^^^^^^^^^^^^^^^^^
psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "receipts_linearized_uniqueness"
DETAIL: Key (room_id, receipt_type, user_id)=(<redacted room id>, m.read, <redacted user id>) already exists.Anything else that would be useful to know?
No response