setval()

Synopsis

Use the setval(sequence_name, value, is_called) function to set and return the value for the specified sequence. UPDATE privilege on the sequence is required to call this function.

Calling the function with two parameters defaults is_called to true, meaning that the nextval will advance the sequence prior to returning the value, and currval will also return the specified value.

When called with is_called set to false, nextval will return the specified value and the value reported by currval will not be changed.

setval() returns just the value of its second argument.

Semantics

sequence_name

Specify the name of the sequence.

value

Specify the value of the sequence.

is_called

Set is_called to true or false.

Examples

Create a sequence

yugabyte=# CREATE SEQUENCE s;
CREATE SEQUENCE

Use setval with is_called set to true:

yugabyte=# SELECT setval('s', 21);
yugabyte=# SELECT setval('s', 21, true);  -- the same command as above
yugabyte=# SELECT nextval('s');
 nextval
---------
      22
(1 row)

Use setval with is_called set to false:

yugabyte=# SELECT setval('s', 21, false);
yugabyte=# SELECT nextval('s');
 nextval
---------
      21
(1 row)

Note

setval changes are immediately visible in other transactions and are not rolled back if the transaction is rolled back.

See also