

#Postgresql serial start value code
If you execute the statement again, you will get the next value from the sequence: SELECT nextval( 'mysequence') Code language: SQL (Structured Query Language) ( sql ) 2) Creating a descending sequence example To get the next value from the sequence to you use the nextval() function: SELECT nextval( 'mysequence') Ĭode language: SQL (Structured Query Language) ( sql ) INCREMENT 5 START 100 Code language: SQL (Structured Query Language) ( sql ) This statement uses the CREATE SEQUENCE statement to create a new ascending sequence starting from 100 with an increment of 5: CREATE SEQUENCE mysequence 1) Creating an ascending sequence example Let’s take some examples of creating sequences to get a better understanding. Note that when you use the SERIAL pseudo-type for a column of a table, behind the scenes, PostgreSQL automatically creates a sequence associated with the column. The OWNED BY clause allows you to associate the table column with the sequence so that when you drop the column or table, PostgreSQL will automatically drop the associated sequence. The NO CYCLE is the default if you don’t explicitly specify CYCLE or NO CYCLE. If you use NO CYCLE, when the limit is reached, attempting to get the next value will result in an error. The next number will be the minimum value for the ascending sequence and maximum value for the descending sequence. The CYCLE allows you to restart the value if the limit is reached.

One value can be generated at a time.īy default, the sequence generates one value at a time i.e., no cache. The CACHE determines how many sequence numbers are preallocated and stored in memory for faster access. The default starting value is minvalue for ascending sequences and maxvalue for descending ones. The START clause specifies the starting value of the sequence. In case of a descending sequence, the default maximum value is -1 and the default minimum value is the minimum value of the data type of the sequence. If you use NO MINVALUEand NO MAXVALUE, the sequence will use the default value.įor an ascending sequence, the default maximum value is the maximum value of the data type of the sequence and the default minimum value is 1. ĭefine the minimum value and maximum value of the sequence. The increment specifies which value to be added to the current sequence value to create new value.Ī positive number will make an ascending sequence while a negative number will form a descending sequence. The data type of the sequence which determines the sequence’s minimum and maximum values. The default data type is BIGINT if you skip it. The valid data type is SMALLINT, INT, and BIGINT. The orders of numbers in the sequence are important.
#Postgresql serial start value how to
Just switch your database table to use BIGSERIAL.Summary: in this tutorial, you will learn about the PostgreSQL sequences and how to use a sequence object to generate a sequence of numbers.īy definition, a sequence is an ordered list of integers. This makes them very efficient and concurrent.

The server only needs to keep track of one value (the prior max) to uniquely generating ids, it can cache them across multiple connections to speed up sequence generation, and by allowing gaps it never needs to transactionally lock the sequence object when it's generating sequences. Sequences are implemented like this to be efficient. There's nothing wrong with that and if you're simply using them as unique ids then having gaps should cause no issue. If the transaction that fetched the id is rolled back then the id will be "lost" and your sequence will have holes in it (eg. This incrementation can happen regardless of whether the id is actually permanently saved. The server keeps track of the previous value (the prior max) and each time a value is requested the value is incremented. The behavior of how it handles overflowing past 2 31 isn't mentioned in the docs but it clearly states that you shouldn't use it if you expect to have values greater than that. The type names smallserial and serial2 also work the same way, except that they create a smallint column. bigserial should be used if you anticipate the use of more than 2 31 identifiers over the lifetime of the table. The type names bigserial and serial8 work the same way, except that they create a bigint column.

The type names serial and serial4 are equivalent: both create integer columns. Here's the snippet from the PostgreSQL docs: The SERIAL type is a signed 32-bit integer and is designed for values less than 2 31 ( NOTE: that's 2 31, not 2 32 as they're signed integers). TLDR: If you're going to have more than 2 31 possible values change the column type to BIGSERIAL.
