Panda Index: a unique key index for deadlock prevention
The PolarDB-X storage engine provides Panda Index, a next-generation multi-version unique key index. To address the performance bottlenecks of traditional MySQL non-clustered indexes, Panda Index uses its native Multi-Version Concurrency Control (MVCC) capability to avoid the overhead from cross-index lookups. It also optimizes global range constraint checks to a row-level lock granularity, which reduces lock scope escalation.
Prerequisites
Your instance must meet the following version requirements:
-
Instance edition: Standard Edition or Enterprise Edition.
-
Engine version: MySQL 8.0.
-
Data node version: xcluster8.4.20-20250527 or later, which corresponds to the version released on May 27, 2025 or later.
NoteInstance version naming rules, see Version notes.
Instance version viewing method, see View the instance version.
Billing
Panda Index is free of charge. However, Panda Index adds 28 bytes of overhead to each unique index record. For most business tables, this results in a minor storage increase, typically less than 5%, and a corresponding increase in storage costs.
Limitations
-
Instance version: Because Panda Index involves changes to the underlying storage structure, an instance that uses Panda Index cannot be downgraded to an earlier version that does not support Panda Index.
-
Handling existing indexes: For tables with existing unique keys (UK), the keys are not automatically converted to a Panda Index after you enable Panda Index. You can rebuild the indexes to enable Panda Index capabilities.
-
Create a new index:
ALTER TABLE ... ADD UNIQUE INDEX idx_new ...; -
Rename the indexes:
ALTER TABLE ... RENAME INDEX idx TO idx_old, RENAME INDEX idx_new TO idx; -
Drop the original index:
ALTER TABLE ... DROP INDEX idx_old;
-
-
Isolation level restrictions: Panda Index is primarily designed to mitigate gap lock issues in the Read Committed (RC) isolation level. In the Repeatable Read (RR) isolation level, the system must still use next-key locks (a combination of a record lock and a gap lock) to ensure repeatable reads. Therefore, Panda Index cannot prevent gap locks at this level.
-
Table and index type restrictions: Panda Index is not supported for temporary tables, system tables, compressed tables, or multi-valued indexes.
Use Panda Index
1. Enable the Panda Index feature
In the PolarDB-X console, go to the Storage Layer tab on the page for your target cluster. Set the opt_index_format_panda_enabled parameter to ON. This change takes effect immediately and does not require an instance restart.
-
For new instances purchased after 00:00:00, June 4, 2025 (Beijing Time), the Panda Index feature is enabled by default.
-
If you set the
opt_index_format_panda_enabledparameter toON, PolarDB-X creates a Panda Index by default when you create a table or a unique key index. -
If you set the
opt_index_format_panda_enabledparameter toOFF, PolarDB-X creates a standard unique key index compatible with community edition MySQL by default when you create a table or a unique key index.
2. Verify the Panda Index effect
You can verify the effect in a simple concurrency scenario.
Prepare data
-- Create a table and a unique key index.
CREATE TABLE t1(
id int,
c1 int,
PRIMARY KEY(id),
UNIQUE KEY uk1(c1)
) SINGLE /* This example uses a single table in Enterprise Edition. This is not required for Standard Edition instances. */;
-- Insert data.
INSERT INTO t1 VALUES (1,1);
INSERT INTO t1 VALUES (100,100);
Run a concurrency test
In one transaction (Session 1), delete a record and then insert a new record to simulate a common update pattern.
BEGIN;
DELETE FROM t1 where id=1;
INSERT INTO t1 values (2,1);
At the same time, in another transaction (Session 2), insert a non-conflicting record into the data gap.
INSERT INTO t1 values (3,2);
At this point, a standard unique key and a Panda Index behave differently.
-
Standard unique key: The insert operation in Session 2 is blocked by the gap lock held by Session 1 and eventually times out.
mysql> INSERT INTO t1 values (3,2); -- The error message contains the following: Lock wait timeout exceeded; try restarting transactionOn a Standard Edition instance, you can observe that the lock information contains a gap lock on the unique key
uk1.SELECT lock_data, lock_mode FROM performance_schema.data_locks WHERE index_name ='uk1'; +-----------+---------------+ | lock_data | lock_mode | +-----------+---------------+ | 1, 1 | X,REC_NOT_GAP | | 1, 1 | S,GAP | | 100, 100 | S,GAP | | 1, 2 | S,GAP | +-----------+---------------+ -
Panda Index: The insert operation in Session 2 succeeds immediately because Panda Index does not create an unnecessary gap lock.
mysql> INSERT INTO t1 values (3,2); Query OK, 1 row affected (0.00 sec)On a Standard Edition instance, you can observe that the lock information does not contain a gap lock on the unique key
uk1.SELECT lock_data, lock_mode FROM performance_schema.data_locks WHERE index_name ='uk1'; +-----------+---------------+ | lock_data | lock_mode | +-----------+---------------+ | 1, 2 | X,REC_NOT_GAP | +-----------+---------------+