PolarDB-X sequences generate globally unique, sequentially incremental numeric values of the MySQL BIGINT type (signed 64-bit integers). Use them as primary key column values or unique index key column values.
PolarDB-X supports two ways to use sequences:
Explicit sequence: Create and manage a sequence with DDL statements, then call
SELECT seq.nextvalto retrieve values (whereseqis the sequence name).Implicit sequence: Add the
AUTO_INCREMENTattribute to a primary key column. PolarDB-X creates and maintains the underlying sequence automatically. See AUTO_INCREMENT.
Choose a sequence type
PolarDB-X offers three sequence types. Before diving into each type, use this table to choose the right one for your workload.
| New sequence | Group sequence | Time sequence | |
|---|---|---|---|
| Globally unique | Yes | Yes | Yes |
| Consecutive | Yes | No | No |
| Monotonically increasing | Yes | No | Macro level only |
| Monotonically increasing in the same session | Yes | Yes | Yes |
| Supported data types | All integer types | All integer types | BIGINT only |
| Readability | High | High | Low |
| Unitization capability | No | Yes | No |
| Performance trade-off | Depends on Global Meta Service (GMS); uses more GMS resources | High performance; no single point of failure | Excellent performance; no persistent storage of values |
When to use each type:
New sequence — Use in AUTO mode databases when running kernel version V5.4.14 or later. This is the default type when no type is specified.
Group sequence — Use in DRDS mode databases (group sequences are used by default in DRDS mode), or for multi-unit applications that need globally unique values across multiple instances or databases. Also the migration path from PolarDB-X 1.0 group sequences.
Time sequence — Use only when migrating applications from PolarDB-X 1.0 that depend on time-based sequence values.
New sequences depend on GMS and consume more GMS resources than other types. If your workload cannot tolerate GMS dependency, use a group sequence instead.
Key concepts
Consecutive — Each value is exactly n + 1 after the previous value n. If any value is skipped, the sequence is nonconsecutive.
Monotonically increasing — Each value is strictly greater than the previous value.
Monotonically increasing at the macro level, non-monotonically increasing at the micro level — Values trend upward overall but are not strictly ordered at fine granularity. For example: 1, 3, 2, 4, 5, 7, 6, 8, ...
Unitization capability — A group sequence can span multiple instances or databases, partitioned into units. Each unit occupies a non-overlapping subset of the sequence space, guaranteeing globally unique values across all units.
New sequences
A new sequence generates values that are globally unique, consecutive, and monotonically increasing. Values are natural numbers starting from 1 by default.
Example (start value: 100000):
100000, 100001, 100002, 100003, 100004, ..., 199999, 200000, 200001, 200002, 200003, ...Version requirements:
Kernel version V5.4.14 or later: when no sequence type is specified in an AUTO mode database, a new sequence is created by default.
Kernel version V5.4.17 or later: the INCREMENT BY, MAXVALUE, and CYCLE / NOCYCLE parameters are supported.
For limits, see New sequences.
Group sequences
A group sequence generates globally unique natural numeric values. Values are not required to be consecutive or monotonically increasing.
By default (no UNIT COUNT or INDEX parameter specified), a group sequence has a single unit and a default start value of 100001. To create a multi-unit group sequence, set UNIT COUNT to a value greater than 1.
How multi-unit sequences work:
The number of units determines the total sequence space.
Each unit is identified by an INDEX parameter and owns a non-overlapping subset of the sequence space.
No two units share the same INDEX value, so values generated by different units never collide.
Why values can be nonconsecutive: PolarDB-X uses multiple nodes to generate group sequence values. The system fetches a segment of values from the database at a time. In scenarios such as network disconnections, not all values in a segment are used, and those unused values are discarded. This is the trade-off for high availability and no single point of failure.
Example (specified start value: 100000; actual start value may differ):
200001, 200002, 200003, 200004, 100001, 100002, 100003, 200005, 200006, ...In this example, the actual start value is 200001, not 100000, because group sequences do not guarantee starting from the specified value. Values between 200004 and 100001, and between 100003 and 200005, are unused gaps.
For limits, see Group sequences.
Time sequences
A time sequence value encodes three components: a timestamp, a node ID, and a serial number. Values are globally unique and increase monotonically at the macro level. Updates do not require reading from a database — PolarDB-X stores only the sequence name and type, not the current value, so time sequences deliver excellent performance.
Example values:
776668092129345536, 776668098018148352, 776668111578333184, 776668114812141568, ...Constraints:
Values are nonconsecutive.
The START WITH, INCREMENT BY, MAXVALUE, CYCLE, and NOCYCLE parameters have no effect on time sequences.
If a time sequence is associated with an auto-increment column, that column must be of type BIGINT.