Skip to content

Commit

Permalink
complement errors relating to lazy uniqueness check (pingcap#10928)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekexium authored Nov 4, 2022
1 parent 962bb98 commit b0ea142
Show file tree
Hide file tree
Showing 5 changed files with 17 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 @@ -542,15 +542,15 @@ INSERT INTO t VALUES ('a');
```
```sql
ERROR 1062 (23000): Duplicate entry 'a' for key 'PRIMARY' # TiDB is compatible with the case-insensitive collation of MySQL.
ERROR 1062 (23000): Duplicate entry 'a' for key 't.PRIMARY' # TiDB is compatible with the case-insensitive collation of MySQL.
```
```sql
INSERT INTO t VALUES ('a ');
```
```sql
ERROR 1062 (23000): Duplicate entry 'a ' for key 'PRIMARY' # TiDB modifies the `PADDING` behavior to be compatible with MySQL.
ERROR 1062 (23000): Duplicate entry 'a ' for key 't.PRIMARY' # TiDB modifies the `PADDING` behavior to be compatible with MySQL.
```
> **Note:**
Expand Down
12 changes: 7 additions & 5 deletions constraints.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ COMMIT;
```

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

In the preceding optimistic example, the unique check was deferred until the transaction is committed. This resulted in a duplicate key error, because the value `bill` was already present.
Expand Down Expand Up @@ -154,7 +154,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'
```

The first `INSERT` statement caused a duplicate key error. This causes additional network communication overhead and may reduce the throughput of insert operations.
Expand All @@ -177,7 +177,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'
```

To achieve better performance of pessimistic transactions, you can set the [`tidb_constraint_check_in_place_pessimistic`](/system-variables.md#tidb_constraint_check_in_place_pessimistic-new-in-v630) variable to `OFF`, which allows TiDB to defer the unique constraint check of a unique index (to the next time when this index requires a lock or to the time when the transaction is committed) and skip the corresponding pessimistic lock. When using this variable, pay attention to the following:
Expand Down Expand Up @@ -215,7 +215,7 @@ To achieve better performance of pessimistic transactions, you can set the [`tid
```

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

- When this variable is disabled, committing a pessimistic transaction that needs to write data might return a `Write conflict` error. When this error occurs, TiDB rolls back the current transaction.
Expand Down Expand Up @@ -267,9 +267,11 @@ To achieve better performance of pessimistic transactions, you can set the [`tid
```

```
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'
```

- When this variable is disabled, the `1062 Duplicate entry` error might be not from the current SQL statement. Therefore, when a transaction operates on multiple tables that have indexes with the same name, you need to check the `1062` error message to find which index the error is actually from.

## PRIMARY KEY

Like MySQL, primary key constraints contain unique constraints, that is, creating a primary key constraint is equivalent to having a unique constraint. In addition, other primary key constraints of TiDB are also similar to those of MySQL.
Expand Down
4 changes: 2 additions & 2 deletions log-redaction.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,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'
```

The error log for the `INSERT` statement above is printed as follows:

```
[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'"]
```

From the error log above, you can see that all sensitive information is shielded using `?` after `tidb_redact_log` is enabled. In this way, data security risks are avoided.
Expand Down
8 changes: 4 additions & 4 deletions system-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@ MPP is a distributed computing framework provided by the TiFlash engine, which a
tidb> insert into t values (1);
Query OK, 1 row affected
tidb> commit; -- Check only when a transaction is committed.
ERROR 1062 : Duplicate entry '1' for key 'PRIMARY'
ERROR 1062 : Duplicate entry '1' for key 't.PRIMARY'
```
- When setting `tidb_constraint_check_in_place` to `ON` and using optimistic transactions:
Expand All @@ -902,7 +902,7 @@ MPP is a distributed computing framework provided by the TiFlash engine, which a
tidb> set @@tidb_constraint_check_in_place=ON;
tidb> begin optimistic;
tidb> insert into t values (1);
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">New in v6.3.0</span>
Expand Down Expand Up @@ -943,7 +943,7 @@ MPP is a distributed computing framework provided by the TiFlash engine, which a
```
```
ERROR 1062 : Duplicate entry '1' for key 'PRIMARY'
ERROR 1062 : Duplicate entry '1' for key 't.PRIMARY'
```
- When setting `tidb_constraint_check_in_place_pessimistic` to `ON` and using pessimistic transactions:
Expand All @@ -955,7 +955,7 @@ MPP is a distributed computing framework provided by the TiFlash engine, which a
```
```
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">New in 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; -- It is successfully committed in MySQL; TiDB returns an error and the transaction rolls back.
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
ERROR 1062 (23000): Duplicate entry '1' for key 't1.PRIMARY'
mysql> SELECT * FROM t1; -- MySQL returns 1 2; TiDB returns 1.
+----+
| id |
Expand Down Expand Up @@ -268,7 +268,7 @@ Query OK, 1 row affected (0.02 sec)
mysql> INSERT INTO tset VALUES (2); -- Statement does not take effect because "test" is misspelled as "tset".
ERROR 1146 (42S02): Table 'test.tset' doesn't exist
mysql> INSERT INTO test VALUES (1),(2); -- Entire statement does not take effect because it violates a PRIMARY KEY constraint
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 b0ea142

Please sign in to comment.