Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

complement errors relating to lazy uniqueness check #11690

Merged
merged 3 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 7 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 @@ -262,9 +262,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` 报错不一定是当前执行的 SQL 语句所发生的错误。因此,在一个事务操作多个表,且这些表有同名索引时,请注意 `1062` 报错信息中提示的是哪个表的哪个索引发生了错误。

## 主键约束

与 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