Explicit sequences

Updated at:
Copy as MD

PolarDB-X 1.0 supports four sequence types for generating unique numeric IDs in distributed environments: group sequences, unit group sequences, time-based sequences, and simple sequences.

Choose a sequence type

Sequence typeGuaranteed unique?Consecutive values?Best for
Group sequenceYesNoGeneral-purpose distributed primary keys
Unit group sequenceYes (across units)NoSharded deployments where each shard needs its own range
Time-based sequenceYesNoTime-ordered IDs (requires BIGINT column)
Simple sequenceYesYesWhen consecutive values are required
Group and unit group sequences may produce non-consecutive values. The START WITH value is a lower bound — the actual starting value is always greater than the specified value.
PolarDB-X 1.0 uses seq.NEXTVAL syntax (Oracle/PostgreSQL style) rather than standard MySQL AUTO_INCREMENT. If you are migrating from MySQL, use the sequence's NEXTVAL in your INSERT statements or set the table's AUTO_INCREMENT column to let PolarDB-X 1.0 manage it automatically.

Create a sequence

Group sequence

A group sequence generates globally unique values across a distributed cluster. If you create a sequence without specifying a type, PolarDB-X 1.0 creates a group sequence by default.

Syntax:

CREATE [GROUP] SEQUENCE <name>
  [START WITH <numeric value>]

Parameters:

ParameterDescriptionDefault
START WITHLower bound for the start value. The actual start value is always greater than this value.100001

Examples:

-- Both statements create a group sequence
mysql> CREATE SEQUENCE seq1;
mysql> CREATE GROUP SEQUENCE seq1;

Unit group sequence

A unit group sequence partitions a global sequence across multiple instances or databases. Each unit produces values in a non-overlapping range, ensuring global uniqueness when all units use the same sequence name and unit count.

A group sequence is a special case of a unit group sequence with UNIT COUNT 1 and INDEX 0.

Syntax:

CREATE [GROUP] SEQUENCE <name>
  [START WITH <numeric value>]
  [UNIT COUNT <numeric value> INDEX <numeric value>]

Parameters:

ParameterDescriptionDefault
START WITHLower bound for the start value. Actual start value depends on the unit index and unit count.100001
UNIT COUNTTotal number of units sharing this sequence.1
INDEXIndex of this unit. Range: 0 to UNIT COUNT - 1.0

Example: Create a globally unique sequence across three instances.

Create one unit group sequence per instance, using the same name and unit count but a different index:

-- Instance 1 or Database 1
mysql> CREATE GROUP SEQUENCE seq2 UNIT COUNT 3 INDEX 0;

-- Instance 2 or Database 2
mysql> CREATE GROUP SEQUENCE seq2 UNIT COUNT 3 INDEX 1;

-- Instance 3 or Database 3
mysql> CREATE GROUP SEQUENCE seq2 UNIT COUNT 3 INDEX 2;

Time-based sequence

A time-based sequence generates values based on timestamps, producing time-ordered IDs.

Important

The column that stores time-based sequence values must be of the BIGINT data type.

Syntax:

CREATE TIME SEQUENCE <name>

Example:

mysql> CREATE TIME SEQUENCE seq3;

Simple sequence

A simple sequence generates consecutive values with configurable increments, bounds, and cycling behavior. Use simple sequences when your application requires strictly consecutive IDs.

Syntax:

CREATE SIMPLE SEQUENCE <name>
  [START WITH <numeric value>]
  [INCREMENT BY <numeric value>]
  [MAXVALUE <numeric value>]
  [CYCLE | NOCYCLE]

Parameters:

ParameterDescriptionDefault
START WITHThe start value of the sequence.1
INCREMENT BYThe step between consecutive values.1
MAXVALUEThe maximum value.9223372036854775807 (max signed BIGINT)
CYCLE / NOCYCLEWhether to restart from START WITH after reaching MAXVALUE.NOCYCLE

Example: Create a simple sequence starting at 1000, incrementing by 2, with a maximum of 99999999999, and no cycling:

mysql> CREATE SIMPLE SEQUENCE seq4 START WITH 1000 INCREMENT BY 2 MAXVALUE 99999999999 NOCYCLE;

Modify a sequence

Use ALTER SEQUENCE to change a sequence's parameters or convert it to a different type.

What you can modify:

Sequence typeModifiable parametersCan convert to
Group sequenceSTART WITHSimple, Time-based
Unit group sequenceSTART WITHCannot convert
Time-based sequenceSTART WITHGroup, Simple
Simple sequenceSTART WITH, INCREMENT BY, MAXVALUE, CYCLE/NOCYCLEGroup, Time-based

Conversion rules:

  • Use CHANGE TO <type> in the ALTER SEQUENCE statement to convert a sequence to another type.

  • When using CHANGE TO, START WITH is required to prevent duplicate values.

  • You cannot convert a unit group sequence to another type, or convert any sequence to a unit group sequence.

  • When converting to a time-based sequence, only START WITH applies — other parameters are not supported.

Before changing START WITH on any sequence, review existing sequence values and the current generation rate to avoid duplicate values. The new START WITH value takes effect immediately — the next generated value starts from that point.

Group sequence

Syntax:

ALTER SEQUENCE <name> [CHANGE TO SIMPLE | TIME]
  START WITH <numeric value>
  [INCREMENT BY <numeric value>]
  [MAXVALUE <numeric value>]
  [CYCLE | NOCYCLE]

Parameters:

ParameterDescriptionDefault
START WITHNew start value. Required when converting type; ignored if not specified otherwise.None
INCREMENT BYStep between values. Applies only when converting to a simple sequence.1
MAXVALUEMaximum value. Applies only when converting to a simple sequence.9223372036854775807
CYCLE / NOCYCLECycling behavior. Applies only when converting to a simple sequence.NOCYCLE

Unit group sequence

Syntax:

ALTER SEQUENCE <name>
  START WITH <numeric value>

Parameters:

ParameterDescriptionDefault
START WITHNew start value. Ignored if not specified.None
You cannot convert a unit group sequence to another type or modify parameters other than START WITH.

Time-based sequence

Syntax:

ALTER SEQUENCE <name> [CHANGE TO GROUP | SIMPLE]
  START WITH <numeric value>
  [INCREMENT BY <numeric value>]
  [MAXVALUE <numeric value>]
  [CYCLE | NOCYCLE]

Parameters:

ParameterDescriptionDefault
START WITHNew start value. Required when converting type; ignored if not specified otherwise.None
INCREMENT BYStep between values. Does not apply when converting to a group sequence.1
MAXVALUEMaximum value. Does not apply when converting to a group sequence.9223372036854775807
CYCLE / NOCYCLECycling behavior. Does not apply when converting to a group sequence.NOCYCLE

Simple sequence

Syntax:

ALTER SEQUENCE <name> [CHANGE TO GROUP | TIME]
  START WITH <numeric value>
  [INCREMENT BY <numeric value>]
  [MAXVALUE <numeric value>]
  [CYCLE | NOCYCLE]

Parameters:

ParameterDescriptionDefault
START WITHNew start value. Required when converting type; ignored if not specified otherwise.None
INCREMENT BYStep between values. Does not apply when converting to a group sequence.1
MAXVALUEMaximum value. Does not apply when converting to a group sequence.9223372036854775807
CYCLE / NOCYCLECycling behavior. Does not apply when converting to a group sequence.NOCYCLE

Examples

Modify a simple sequence — set START WITH to 3000, INCREMENT BY to 5, MAXVALUE to 1000000, and switch to CYCLE:

mysql> ALTER SEQUENCE seq4 START WITH 3000 INCREMENT BY 5 MAXVALUE 1000000 CYCLE;

Convert a group sequence to a simple sequence:

mysql> ALTER SEQUENCE seq1 CHANGE TO SIMPLE START WITH 1000000;

Query sequences

List all sequences

Run SHOW SEQUENCES to view all sequences and their current state:

mysql> SHOW SEQUENCES;

Example output:

+------+--------+------------+------------+------------+--------------+------------+-------------+-------+--------+
| NAME | VALUE  | UNIT_COUNT | UNIT_INDEX | INNER_STEP | INCREMENT_BY | START_WITH | MAX_VALUE   | CYCLE | TYPE   |
+------+--------+------------+------------+------------+--------------+------------+-------------+-------+--------+
| seq1 | 100000 | 1          | 0          | 100000     | N/A          | N/A        | N/A         | N/A   | GROUP  |
| seq2 | 400000 | 3          | 1          | 100000     | N/A          | N/A        | N/A         | N/A   | GROUP  |
| seq3 | N/A    | N/A        | N/A        | N/A        | N/A          | N/A        | N/A         | N/A   | TIME   |
| seq4 | 1006   | N/A        | N/A        | N/A        | 2            | 1000       | 99999999999 | N     | SIMPLE |
+------+--------+------------+------------+------------+--------------+------------+-------------+-------+--------+
4 rows in set (0.00 sec)

The TYPE column shows abbreviated sequence type names.

Get the next sequence value

Use <sequence_name>.NEXTVAL to retrieve the next value from a sequence.

Syntax:

[<schema_name>.]<sequence_name>.NEXTVAL

Get a single value:

mysql> SELECT sample_seq.nextval FROM dual;

Output:

+--------------------+
| SAMPLE_SEQ.NEXTVAL |
+--------------------+
|             101001 |
+--------------------+
1 row in set (0.04 sec)

Use in an INSERT statement:

mysql> INSERT INTO some_users (name, address, gmt_create, gmt_modified, intro)
       VALUES ('sun', sample_seq.nextval, now(), now(), 'aa');

sample_seq.nextval is evaluated as a value within the SQL statement. If the table was created with AUTO_INCREMENT, you do not need to specify the auto-increment column — PolarDB-X 1.0 manages it automatically.

Get multiple values at once:

SELECT [<schema_name>.]<sequence_name>.NEXTVAL FROM DUAL WHERE COUNT = <numeric value>

Example — retrieve 10 values:

mysql> SELECT sample_seq.nextval FROM dual WHERE count = 10;

Output:

+--------------------+
| SAMPLE_SEQ.NEXTVAL |
+--------------------+
|             101002 |
|             101003 |
|             101004 |
|             101005 |
|             101006 |
|             101007 |
|             101008 |
|             101009 |
|             101010 |
|             101011 |
+--------------------+
10 rows in set (0.04 sec)

Delete a sequence

Syntax:

DROP SEQUENCE <name>

Example:

mysql> DROP SEQUENCE seq3;

MySQL compatibility

The explicit sequence feature is a PolarDB-X 1.0 extension. Standard MySQL does not support CREATE SEQUENCE or the seq.NEXTVAL syntax. PolarDB-X 1.0 sequences are modeled on Oracle/PostgreSQL-style sequences and use .NEXTVAL to retrieve the next value, rather than MySQL's AUTO_INCREMENT.

If your application relies on standard MySQL AUTO_INCREMENT, you can continue using it — PolarDB-X 1.0 manages AUTO_INCREMENT columns automatically and you do not need to reference a sequence explicitly in your INSERT statements.