SoFunction
Updated on 2025-03-03

PostgreSQL creates self-increasing sequences, query sequences and use sequence code examples

Sequence functions:

function Return type describe
nextval(regclass) bigint Increment the sequence object to its next value and return that value. This action is automatically completed. Even if multiple sessions run nextval concurrently, each process will safely receive a unique sequence value.
currval(regclass) bigint Returns the value of the sequence caught by nextval in the current session. (If nextval has never been called on this sequence in this session, an error will be reported.) Please note that because this function returns a session-wide value and can also give a predictable result, it can be used to determine whether other sessions have executed nextval.
lastval() bigint Returns the value of the sequence caught by nextval in the current session. (If nextval has never been called on the sequence in this session, an error will be reported.) Please note that this function returns a
Returns the value returned by the last nextval in the current session. This function is equivalent to currval, except that it does not use the sequence name as a parameter, it grabs the sequence used by nextval in the current session for the most recent time. If nextval has not been called yet, calling lastval will report an error.
setval(regclass, bigint) bigint Resets the counter value of the sequence object. Set the last_value field of the sequence to the specified value and set its is_called field to true, indicating that the next nextval will increment the sequence before returning the value.
setval(regclass, bigint, boolean) bigint Resets the counter value of the sequence object. The function is equivalent to the setval function above, except that is_called can be set to true or false. If it is set to false, the next nextval will return the value, and the subsequent nextval will start incrementing the sequence.

CREATE SEQUENCE Create a Sequence

CREATE SEQUENCE "user_id_seq"
    START WITH 1        --Start value
    INCREMENT BY 1      --Added number each time
    NO MINVALUE         --No minimum value
    NO MAXVALUE         --No maximum value
    CACHE 1;            --Number of cached sequences

ALTER SEQUENCE Modify the sequence:

ALTER SEQUENCE "user_id_seq"
    START WITH 1        --Start value
    INCREMENT BY 1      --Added number each time
    NO MINVALUE         --No minimum value
    NO MAXVALUE         --No maximum value
    CACHE 1;            --Number of cached sequences

Nextval query the next sequence value:

SELECT nextval('user_info_id_seq')

currval query the current sequence value:

SELECT currval('user_info_id_seq')

Batch setting table self-increasing sequence value maximum:

SELECT
	'SELECT SETVAL(''' || SUBSTRING ( sequence_name FROM '^(.*)[_]id_seq$' ) || '_id_seq'', (SELECT (MAX(id) + 1) FROM ' || SUBSTRING ( sequence_name FROM '^(.*)[_]id_seq$' ) || ') , true);' AS SQL 
FROM
	information_schema.sequences

Summarize

This is the article about PostgreSQL creating self-increasing sequences, query sequences and using sequences. For more related contents of PGSQL creating self-increasing sequences, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!