SEQUENCE Functions
The sequence functions provide a simple method to ensure security of multiple users for users to obtain sequence values from sequence objects.
- nextval(regclass)
Description: Specifies an increasing sequence and returns a new value.
To avoid blocking of concurrent transactions that obtain numbers from the same sequence, a nextval operation is never rolled back; that is, once a value has been fetched it is considered used, even if the transaction that did the nextval later aborts. This means that aborted transactions may leave unused "holes" in the sequence of assigned values. Therefore, GaussDB sequences cannot be used to obtain sequence without gaps.
The nextval function can be executed only on the primary node. It is not supported on standby nodes.
Return type: numeric
The nextval function can be called in either of the following ways: (In example 2, the sequence name cannot contain a dot.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
openGauss=# CREATE SEQUENCE seqDemo; -- Example 1: openGauss=# SELECT nextval('seqDemo'); nextval --------- 1 (1 row) -- Example 2: openGauss=# SELECT seqDemo.nextval; nextval --------- 2 (1 row) openGauss=# DROP SEQUENCE seqDemo;
- currval(regclass)
Description: Returns the last value returned by nextval in the current session. If nextval has not been called for the specified sequence in the current session, an error is reported when currval is called.
Return type: numeric
The currval function can be called in either of the following ways: (In example 2, the sequence name cannot contain a dot.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
openGauss=# CREATE SEQUENCE seq1; openGauss=# SELECT nextval('seq1'); -- Example 1: openGauss=# SELECT currval('seq1'); currval --------- 1 (1 row) -- Example 2: openGauss=# SELECT seq1.currval; currval --------- 1 (1 row) openGauss=# DROP SEQUENCE seq1;
- lastval()
Description: Returns the last value returned by nextval in the current session. This function is equivalent to currval, except that it does not use sequence names as parameters. It fetches the sequence used by nextval last time in the current session. If nextval has not been called in the current session, an error is reported when lastval is called.
Return type: numeric
Example:
1 2 3 4 5 6 7 8
openGauss=# CREATE SEQUENCE seq1; openGauss=# SELECT nextval('seq1'); openGauss=# SELECT lastval(); lastval --------- 1 (1 row) openGauss=# DROP SEQUENCE seq1;
- setval(regclass, numeric)
Description: Sets the current value of a sequence.
Return type: numeric
Example:
1 2 3 4 5 6 7 8
openGauss=# CREATE SEQUENCE seqDemo; openGauss=# SELECT nextval('seqDemo'); openGauss=# SELECT setval('seqDemo',5); setval -------- 5 (1 row) openGauss=# DROP SEQUENCE seqDemo;
- setval(regclass, numeric, Boolean)
Description: Sets the current value of a sequence and the is_called sign.
Return type: numeric
Example:
1 2 3 4 5 6 7 8
openGauss=# CREATE SEQUENCE seqDemo; openGauss=# SELECT nextval('seqDemo'); openGauss=# SELECT setval('seqDemo',5,true); setval -------- 5 (1 row) openGauss=# DROP SEQUENCE seqDemo;
The current session will take effect immediately after setval is performed. If other sessions have buffered sequence values, setval will take effect only after the values are used up. Therefore, to prevent sequence value conflicts, you are advised to use setval with caution.
Because the sequence is non-transactional, the change caused by setval will not be undone by transaction rollback.
The nextval function can be executed only on the primary node. It is not supported on standby nodes.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot