The original implementation of conflict detection in the block_target table was faulty, with a race condition that we are hitting in production, causing multiple block_target rows for some users. The simplest way to fix this is probably to add a unique index to the block_target table.
bt_user and bt_address are nullable.The query sequence was UPDATE/SELECT/INSERT. The update query acquires a shared lock and so is not serialized with another update query. However, the sequence 1.update, 2.update, A null value in either causes the index to compare non-equal1.insert, permitting duplicates:2.insert fails safely with a deadlock error.
```
MariaDB [mw]> create table t (a int,The insert acquires an exclusive lock. b intIf the first thread does both an update and insert before the second thread does its update, unique key (a,b));
Query OK,the second update will wait for the first thread to commit. 0 rows affected (0.008 sec)
MariaDB [mw]> insert into t values (1The sequence is then 1.update, 1), (11.insert, 1);
ERROR 1062 (23000): Duplicate entry '1-1' for key 'a'
MariaDB [mw]> insert into t values (12.update(wait), null), (11.commit, null);
Query OK2.update(complete), 2 rows affected (0.008 sec)
Records: 2 Duplicates: 0 Warnings: 0
```
So a simple multi-column index doesn't work2.select, 2.insert. The 2.update does return affectedRows=1 however the insert was conditional on the result of the select.
**DBMS-layer solutions:**
| | MariaDB | MySQL | PostgreSQL | SQLite |
|---|---|---|---|---|
| Index on generated column | ✓ | ✓ | ✓ | 3.31+
| Index on expression | x | 8.0.13+ | ✓ | 3.9+
| NULLS NOT DISTINCT | x | x | 15+ | x
**App-layer solutions:**
* Stop nulling the fieldsUnfortunately, the select was non-locking. I used nulls mostly because it is convenient to use COALESCE() in a few places.
* Leave it without a unique index and use GET_LOCK to synchronize externallyA locking select always returns the updated row, however a non-locking select occasionally returns an empty result set despite the thread having waited for the new row to be committed.
**UPSERT with nulls**The [[https://gerrit.wikimedia.org/r/c/mediawiki/core/+/1134635|committed fix]] uses GET_LOCK() to synchronise externally, and this does help to avoid deadlock errors. That commit also added FOR UPDATE to the select, which would have been enough by itself to avoid duplicate rows.
Our emulated UPSERT implementation,Adding a unique index to the block_target table and using upsert is not feasible since an upsert on a table with multiple unique indexes is unsafe for replication. used on PostgreSQL and SQLiteAlso, needs to find conflictingnullable fields make it difficult to force the rows to collide, and so would need to be aware ofsince any null value causes an aggregate indexes on expressions. like (bt_user, Native upsert is available since PostgreSQL 9.5 and SQLite 3.24 as discussed in 2021 at https://gerrit.wikimedia.org/r/c/mediawiki/core/+/574101/comments/d859b0e9_5007a8b5bt_auto, bt_address) to compare non-equal.