Skip to content

Commit

Permalink
complement errors relating to lazy uniqueness check
Browse files Browse the repository at this point in the history
Signed-off-by: ekexium <eke@fastmail.com>
  • Loading branch information
ekexium committed Oct 19, 2022
1 parent 89bbf4e commit fecfc0e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
4 changes: 2 additions & 2 deletions character-set-and-collation.md
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ INSERT INTO t VALUES ('a');
```

```sql
ERROR 1062 (23000): Duplicate entry 'a' for key 'PRIMARY'
ERROR 1062 (23000): Duplicate entry 'a' for key 't.PRIMARY'
```

TiDB 兼容了 MySQL 的 case insensitive collation。
Expand All @@ -538,7 +538,7 @@ INSERT INTO t VALUES ('a ');
```

```sql
ERROR 1062 (23000): Duplicate entry 'a ' for key 'PRIMARY'
ERROR 1062 (23000): Duplicate entry 'a ' for key 't.PRIMARY'
```

TiDB 修正了 `PADDING` 行为,与 MySQL 兼容。
Expand Down
14 changes: 9 additions & 5 deletions constraints.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ COMMIT;
```

```
ERROR 1062 (23000): Duplicate entry 'bill' for key 'username'
ERROR 1062 (23000): Duplicate entry 'bill' for key 'users.username'
```

在以上乐观事务的示例中,唯一约束的检查推迟到事务提交时才进行。由于 `bill` 值已经存在,这一行为导致了重复键错误。
Expand Down Expand Up @@ -149,7 +149,7 @@ INSERT INTO users (username) VALUES ('jane'), ('chris'), ('bill');
```

```
ERROR 1062 (23000): Duplicate entry 'bill' for key 'username'
ERROR 1062 (23000): Duplicate entry 'bill' for key 'users.username'
```

第一条 `INSERT` 语句导致了重复键错误。这会造成额外的网络通信开销,并可能降低插入操作的吞吐量。
Expand All @@ -172,7 +172,7 @@ INSERT INTO users (username) VALUES ('jane'), ('chris'), ('bill');
```

```
ERROR 1062 (23000): Duplicate entry 'bill' for key 'username'
ERROR 1062 (23000): Duplicate entry 'bill' for key 'users.username'
```

对于悲观事务,你可以设置变量 [`tidb_constraint_check_in_place_pessimistic`](/system-variables.md#tidb_constraint_check_in_place_pessimistic-从-v630-版本开始引入)`OFF` 来推迟唯一约束检查,到下一次对该唯一索引项加锁时或事务提交时再进行检查,同时也跳过对该悲观锁加锁,以获得更好的性能。此时需要注意:
Expand Down Expand Up @@ -210,7 +210,7 @@ ERROR 1062 (23000): Duplicate entry 'bill' for key 'username'
```

```
ERROR 1062 (23000): Duplicate entry 'bill' for key 'username'
ERROR 1062 (23000): Duplicate entry 'bill' for key 'users.username'
```

- 关闭该变量时,如果在事务中写入数据,执行 `COMMIT` 语句可能会返回 `Write conflict` 错误。返回该错误时,TiDB 会回滚当前事务。
Expand Down Expand Up @@ -246,6 +246,8 @@ ERROR 1062 (23000): Duplicate entry 'bill' for key 'username'
ERROR 9007 (HY000): Write conflict, txnStartTS=435688780611190794, conflictStartTS=435688783311536129, conflictCommitTS=435688783311536130, key={tableID=74, indexID=1, indexValues={bill, }} primary={tableID=74, indexID=1, indexValues={bill, }}, reason=LazyUniquenessCheck [try again later]
```

- 关闭该变量时,如果多个悲观事务之间有写冲突,悲观锁可能会被其它悲观事务提交时强制回滚,因此产生 `Pessimistic lock not found` 错误。发生该错误时,说明该业务不适合推迟悲观事务的唯一约束检查,应考虑业务上避免冲突,或在错误后重试事务。

- 关闭该变量会导致悲观事务中可能报出错误 `8147: LazyUniquenessCheckFailure`

> **注意:**
Expand All @@ -262,9 +264,11 @@ ERROR 1062 (23000): Duplicate entry 'bill' for key 'username'
```

```
ERROR 8147 (23000): transaction aborted because lazy uniqueness check is enabled and an error occurred: [kv:1062]Duplicate entry 'bill' for key 'username'
ERROR 8147 (23000): transaction aborted because lazy uniqueness check is enabled and an error occurred: [kv:1062]Duplicate entry 'bill' for key 'users.username'
```

- 关闭该变量时,1062 Duplicate entry 错误的语句所报的不一定是当前语句发生的错误,因此在一个事务操作多个表,且这些表有同名索引时,注意报错的是哪个表的哪个索引。

## 主键约束

与 MySQL 行为一样,主键约束包含了唯一约束,即创建了主键约束相当于拥有了唯一约束。此外,TiDB 其他的主键约束规则也与 MySQL 相似。例如:
Expand Down
4 changes: 2 additions & 2 deletions log-redaction.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ create table t (a int, unique key (a));
Query OK, 0 rows affected (0.00 sec)

insert into t values (1),(1);
ERROR 1062 (23000): Duplicate entry '1' for key 'a'
ERROR 1062 (23000): Duplicate entry '1' for key 't.a'
```

打印出的错误日志如下:

```
[2020/10/20 11:45:49.539 +08:00] [INFO] [conn.go:800] ["command dispatched failed"] [conn=5] [connInfo="id:5, addr:127.0.0.1:57222 status:10, collation:utf8_general_ci, user:root"] [command=Query] [status="inTxn:0, autocommit:1"] [sql="insert into t values ( ? ) , ( ? )"] [txn_mode=OPTIMISTIC] [err="[kv:1062]Duplicate entry '?' for key 'a'"]
[2020/10/20 11:45:49.539 +08:00] [INFO] [conn.go:800] ["command dispatched failed"] [conn=5] [connInfo="id:5, addr:127.0.0.1:57222 status:10, collation:utf8_general_ci, user:root"] [command=Query] [status="inTxn:0, autocommit:1"] [sql="insert into t values ( ? ) , ( ? )"] [txn_mode=OPTIMISTIC] [err="[kv:1062]Duplicate entry '?' for key 't.a'"]
```

从以上报错日志可以看到,开启 `tidb_redact_log` 后,报错信息里的敏感内容被隐藏掉了(目前是用问号替代)。TiDB 日志中会把敏感信息隐藏掉,以此规避数据安全风险。
Expand Down
8 changes: 4 additions & 4 deletions system-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ MPP 是 TiFlash 引擎提供的分布式计算框架,允许节点之间的数
```
```
ERROR 1062 : Duplicate entry '1' for key 'PRIMARY'
ERROR 1062 : Duplicate entry '1' for key 't.PRIMARY'
```
- 乐观事务模型下将 `tidb_constraint_check_in_place` 设置为 `ON`
Expand All @@ -748,7 +748,7 @@ MPP 是 TiFlash 引擎提供的分布式计算框架,允许节点之间的数
```
```
ERROR 1062 : Duplicate entry '1' for key 'PRIMARY'
ERROR 1062 : Duplicate entry '1' for key 't.PRIMARY'
```
### `tidb_constraint_check_in_place_pessimistic` <span class="version-mark">从 v6.3.0 版本开始引入</span>
Expand Down Expand Up @@ -783,7 +783,7 @@ MPP 是 TiFlash 引擎提供的分布式计算框架,允许节点之间的数
```
```
ERROR 1062 : Duplicate entry '1' for key 'PRIMARY'
ERROR 1062 : Duplicate entry '1' for key 't.PRIMARY'
```
- 悲观事务模型下将 `tidb_constraint_check_in_place_pessimistic` 设置为 `ON`
Expand All @@ -795,7 +795,7 @@ MPP 是 TiFlash 引擎提供的分布式计算框架,允许节点之间的数
```
```
ERROR 1062 : Duplicate entry '1' for key 'PRIMARY'
ERROR 1062 : Duplicate entry '1' for key 't.PRIMARY'
```
### `tidb_cost_model_version` <span class="version-mark">从 v6.2.0 版本开始引入</span>
Expand Down
4 changes: 2 additions & 2 deletions transaction-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ mysql> INSERT INTO t1 VALUES (2);
Query OK, 1 row affected (0.00 sec)

mysql> COMMIT; -- MySQL 提交成功;TiDB 返回错误,事务回滚。
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
ERROR 1062 (23000): Duplicate entry '1' for key 't1.PRIMARY'
mysql> SELECT * FROM t1; -- MySQL 返回 1 2;TiDB 返回 1。
+----+
| id |
Expand Down Expand Up @@ -268,7 +268,7 @@ Query OK, 1 row affected (0.02 sec)
mysql> INSERT INTO tset VALUES (2); -- tset 拼写错误,使该语句执行出错。
ERROR 1146 (42S02): Table 'test.tset' doesn't exist
mysql> INSERT INTO test VALUES (1),(2); -- 违反 PRIMARY KEY 约束,语句不生效。
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
ERROR 1062 (23000): Duplicate entry '1' for key 'test.PRIMARY'
mysql> INSERT INTO test VALUES (3);
Query OK, 1 row affected (0.00 sec)
Expand Down

0 comments on commit fecfc0e

Please sign in to comment.