{"id":29700,"date":"2023-02-25T12:20:30","date_gmt":"2023-02-25T02:20:30","guid":{"rendered":"https:\/\/database.guide\/?p=29700"},"modified":"2023-02-28T13:56:44","modified_gmt":"2023-02-28T03:56:44","slug":"create-a-sequence-in-postgresql","status":"publish","type":"post","link":"https:\/\/database.guide\/create-a-sequence-in-postgresql\/","title":{"rendered":"Create a Sequence in PostgreSQL"},"content":{"rendered":"\n<p>PostgreSQL allows us to create sequence objects, otherwise known as &#8220;sequence generators&#8221;, or simply &#8220;sequences&#8221;. As the name suggests, a sequence object is used to generate sequence numbers. <\/p>\n\n\n\n<p>We can use sequences to generate incrementing numbers or decrementing numbers.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<p>The syntax for creating a sequence generator goes like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE &#91; { TEMPORARY | TEMP } | UNLOGGED ] SEQUENCE &#91; IF NOT EXISTS ] name\n    &#91; AS data_type ]\n    &#91; INCREMENT &#91; BY ] increment ]\n    &#91; MINVALUE minvalue | NO MINVALUE ] &#91; MAXVALUE maxvalue | NO MAXVALUE ]\n    &#91; START &#91; WITH ] start ] &#91; CACHE cache ] &#91; &#91; NO ] CYCLE ]\n    &#91; OWNED BY { table_name.column_name | NONE } ]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Here&#8217;s an example of creating a sequence in PostgreSQL:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE SEQUENCE Sequence1;<\/code><\/pre>\n\n\n\n<p>This is all that&#8217;s required to create a sequence object. We specify <code>CREATE SEQUENCE<\/code>, followed by the name we want to give to the sequence.<\/p>\n\n\n\n<p>That creates a sequence with the default options. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Specify Options for the Sequence<\/h2>\n\n\n\n<p>We can also specify our own custom options for the sequence. <\/p>\n\n\n\n<p>Here&#8217;s an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE SEQUENCE Sequence2\n    AS integer\n    INCREMENT 10\n    MINVALUE 100\n    MAXVALUE 250000\n    START 101\n    CACHE 1\n    NO CYCLE;<\/code><\/pre>\n\n\n\n<p>Here&#8217;s what those sequence options do:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><code>INCREMENT<\/code><\/td><td>Specifies which value is added to the current sequence value to create a new value. A positive value makes it an ascending sequence, a negative one a descending sequence. The default value is <code>1<\/code>.<\/td><\/tr><tr><td><code>MINVALUE<\/code><\/td><td>Specifies the minimum value a sequence can generate. If this clause is not supplied or&nbsp;<code>NO MINVALUE<\/code>&nbsp;is specified, then default values are used. The default for an ascending sequence is <code>1<\/code>. The default for a descending sequence is the minimum value of the data type.<\/td><\/tr><tr><td><code>MAXVALUE<\/code><\/td><td>Specifies the maximum value for the sequence. If this clause is not supplied or&nbsp;<code>NO MAXVALUE<\/code>&nbsp;is specified, then default values are used. The default for an ascending sequence is the maximum value of the data type. The default for a descending sequence is <code>-1<\/code>.<\/td><\/tr><tr><td><code>START<\/code><\/td><td>Specifies the value to start the sequence at. The default value for ascending sequences is the <code>MINVALUE<\/code> amount, and the default for descending sequences is the <code>MAXVALUE<\/code> amount.<\/td><\/tr><tr><td><code>CACHE<\/code><\/td><td>Specifies how many sequence numbers are to be preallocated and stored in memory for faster access. The default value is <code>1<\/code>, which is also the minimum value.<\/td><\/tr><tr><td><code>CYCLE<\/code><br><code>NO CYCLE<\/code><\/td><td>We can use <code>CYCLE<\/code> or <code>NO CYCLE<\/code> to specify whether or not the sequence will start again when it reaches its <code>MINVALUE<\/code> or <code>MAXVALUE<\/code> amount (depending on whether it&#8217;s an ascending sequence or descending one). If <code>NO CYCLE<\/code> is used, an error will occur once the sequence tries to generate a value that exceeds its <code>MINVALUE<\/code>\/<code>MAXVALUE<\/code>.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Data Type<\/h2>\n\n\n\n<p>In the previous example I specified the data type as <code>integer<\/code>. We have the option of <code>smallint<\/code>, <code>integer<\/code>, and <code>bigint<\/code>.<\/p>\n\n\n\n<p>The default data type is <code>bigint<\/code>. In other words, if we don&#8217;t specify the data type, the sequence will use <code>bigint<\/code>. This has implications for the default <code>MAXVALUE<\/code> and <code>MINVALUE<\/code>. The default maximum value for an ascending sequence is the maximum value of the data type.&nbsp;The default minimum value for a descending sequence is the minimum value of the data type.<\/p>\n\n\n\n<p>Let&#8217;s create some more sequences:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE SEQUENCE Sequence3 AS integer\n    INCREMENT 1;\nCREATE SEQUENCE Sequence4 AS integer\n    INCREMENT -1;<\/code><\/pre>\n\n\n\n<p>In this case, both sequences use the <code>integer<\/code> type, but they use a different increment. Specifically, <code>Sequence3<\/code> uses a positive increment, while <code>Sequence4<\/code> uses a negative increment.<\/p>\n\n\n\n<p>Next we&#8217;ll see how this affects the <code>MINVALUE<\/code> and <code>MAXVALUE<\/code> for each sequence.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Check the Sequences<\/h2>\n\n\n\n<p>We can run the following query to get information about our sequences:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    pc.relname,\n    ps.seqstart,\n    ps.seqincrement,\n    ps.seqmax,\n    ps.seqmin,\n    ps.seqcache,\n    ps.seqcycle\nFROM pg_class pc\nJOIN pg_sequence ps \nON pc.oid = ps.seqrelid\nWHERE pc.relname LIKE 'sequence%';<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">  relname  | seqstart | seqincrement |       seqmax        |   seqmin    | seqcache | seqcycle \n-----------+----------+--------------+---------------------+-------------+----------+----------\n sequence1 |        1 |            1 | 9223372036854775807 |           1 |        1 | f\n sequence2 |      101 |           10 |              250000 |         100 |        1 | f\n sequence3 |        1 |            1 |          2147483647 |           1 |        1 | f\n sequence4 |       -1 |           -1 |                  -1 | -2147483648 |        1 | f<\/pre>\n\n\n\n<p>We can see the differences between each of the sequences. In particular, we can see how <code>sequence3<\/code> and <code>sequence4<\/code> differ, based on the increments that we gave them in the previous example (i.e. one has a positive increment, the other has a negative increment).<\/p>\n\n\n\n<p>Regarding <code>sequence1<\/code>, the <code>seqmax<\/code> (maximum value) reflects the <code>bigint<\/code> type. That&#8217;s because we didn&#8217;t specify a data type for that sequence, and so it used the default type (which is <code>bigint<\/code>). For <code>sequence2<\/code>, we specified our own <code>MAXVALUE<\/code> of <code>250000<\/code>, and so this is reflected in the <code>seqmax<\/code> column. We can also see that some of its other properties reflect the values that we gave the sequence when we created it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PostgreSQL allows us to create sequence objects, otherwise known as &#8220;sequence generators&#8221;, or simply &#8220;sequences&#8221;. As the name suggests, a sequence object is used to generate sequence numbers. We can use sequences to generate incrementing numbers or decrementing numbers.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[10,164],"class_list":["post-29700","post","type-post","status-publish","format-standard","hentry","category-postgresql","tag-how-to","tag-sequences"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/29700","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=29700"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/29700\/revisions"}],"predecessor-version":[{"id":29834,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/29700\/revisions\/29834"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=29700"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=29700"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=29700"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}