Updated on 2024-05-21 GMT+08:00

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 is 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, sequences in GaussDB cannot be used to obtain sequence without gaps.
    • If the nextval function is pushed to DNs, each DN will automatically connect to the GTM and requests the next value. For example, in the INSERT INTO t1 SELECT xxx statement, a column in table t1 needs to call the nextval function. If maximum number of connections on the GTM is 8192, this type of pushed statements occupies too many GTM connections. Therefore, the number of concurrent connections for these statements is limited to 7000 divided by the number of cluster DNs. The other 1192 connections are reserved for other statements.

    Return type: numeric

    The nextval function can be called in either of the following ways: (In example 2, the ORA syntax is supported. Currently, the sequence name cannot contain a dot.)

    Example 1:

    1
    2
    3
    4
    5
    gaussdb=# SELECT nextval('seqDemo'); 
     nextval
    ---------
           2
    (1 row)
    

    Example 2:

    1
    2
    3
    4
    5
    gaussdb=# SELECT seqDemo.nextval; 
     nextval
    ---------
           2
    (1 row)
    
  • 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. By default, this function is disabled. To enable it, set enable_beta_features to true. After enable_beta_features is set to true, the nextval() function will not be pushed down.

    Return type: numeric

    The currval function can be called in either of the following ways: (In example 2, the ORA syntax is supported. Currently, the sequence name cannot contain a dot.)

    Example 1:

    1
    2
    3
    4
    5
    gaussdb=# SELECT currval('seq1'); 
     currval
    ---------
           2
    (1 row)
    

    Example 2:

    1
    2
    3
    4
    5
    gaussdb=# SELECT seq1.currval seq1; 
     currval
    ---------
           2
    (1 row)
    
  • 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.

    By default, this function is disabled. To enable it, set enable_beta_features or lastval_supported to true. After this function is enabled, nextval() will not be pushed down.

    Return type: numeric

    Example:

    1
    2
    3
    4
    5
    gaussdb=# SELECT lastval(); 
     lastval
    ---------
           2
    (1 row)
    
  • setval(regclass, bigint)

    Sets the current value of a sequence.

    Return type: numeric

    Example:

    1
    2
    3
    4
    5
    gaussdb=# SELECT setval('seqDemo',1);
     setval
    --------
          1
    (1 row)
    
  • setval(regclass, numeric, Boolean)

    Sets the current value of a sequence and the is_called sign.

    Return type: numeric

    Example:

    1
    2
    3
    4
    5
    gaussdb=# SELECT setval('seqDemo',1,true);
     setval
    --------
          1
    (1 row)
    

    The current session and GTM 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 perform setval with caution.

    Because the sequence is non-transactional, changes made by setval will not be canceled when a transaction rolled back.

  • pg_sequence_last_value(sequence_oid oid, OUT cache_value int16, OUT last_value int16)

    Description: Obtains the parameters of a specified sequence, including the cache value and current value.

    Return type: int16, int16