2
.\" Title: CREATE SEQUENCE
3
.\" Author: The PostgreSQL Global Development Group
4
.\" Generator: DocBook XSL Stylesheets v1.75.1 <http://docbook.sf.net/>
6
.\" Manual: PostgreSQL 9.1beta1 Documentation
7
.\" Source: PostgreSQL 9.1beta1
10
.TH "CREATE SEQUENCE" "7" "2011-04-27" "PostgreSQL 9.1beta1" "PostgreSQL 9.1beta1 Documentation"
11
.\" -----------------------------------------------------------------
12
.\" * set default formatting
13
.\" -----------------------------------------------------------------
14
.\" disable hyphenation
16
.\" disable justification (adjust text to left margin only)
18
.\" -----------------------------------------------------------------
19
.\" * MAIN CONTENT STARTS HERE *
20
.\" -----------------------------------------------------------------
22
CREATE_SEQUENCE \- define a new sequence generator
27
CREATE [ TEMPORARY | TEMP ] SEQUENCE \fIname\fR [ INCREMENT [ BY ] \fIincrement\fR ]
28
[ MINVALUE \fIminvalue\fR | NO MINVALUE ] [ MAXVALUE \fImaxvalue\fR | NO MAXVALUE ]
29
[ START [ WITH ] \fIstart\fR ] [ CACHE \fIcache\fR ] [ [ NO ] CYCLE ]
30
[ OWNED BY { \fItable\fR\&.\fIcolumn\fR | NONE } ]
35
creates a new sequence number generator\&. This involves creating and initializing a new special single\-row table with the name
36
\fIname\fR\&. The generator will be owned by the user issuing the command\&.
38
If a schema name is given then the sequence is created in the specified schema\&. Otherwise it is created in the current schema\&. Temporary sequences exist in a special schema, so a schema name cannot be given when creating a temporary sequence\&. The sequence name must be distinct from the name of any other sequence, table, index, view, or foreign table in the same schema\&.
40
After a sequence is created, you use the functions
44
to operate on the sequence\&. These functions are documented in
45
Section 9.15, \(lqSequence Manipulation Functions\(rq, in the documentation\&.
47
Although you cannot update a sequence directly, you can use a query like:
53
SELECT * FROM \fIname\fR;
59
to examine the parameters and current state of a sequence\&. In particular, the
61
field of the sequence shows the last value allocated by any session\&. (Of course, this value might be obsolete by the time it\(aqs printed, if other sessions are actively doing
68
If specified, the sequence object is created only for this session, and is automatically dropped on session exit\&. Existing permanent sequences with the same name are not visible (in this session) while the temporary sequence exists, unless they are referenced with schema\-qualified names\&.
73
The name (optionally schema\-qualified) of the sequence to be created\&.
79
INCREMENT BY \fIincrement\fR
80
specifies which value is added to the current sequence value to create a new value\&. A positive value will make an ascending sequence, a negative one a descending sequence\&. The default value is 1\&.
83
\fIminvalue\fR, NO MINVALUE
86
MINVALUE \fIminvalue\fR
87
determines the minimum value a sequence can generate\&. If this clause is not supplied or
89
is specified, then defaults will be used\&. The defaults are 1 and \-263\-1 for ascending and descending sequences, respectively\&.
92
\fImaxvalue\fR, NO MAXVALUE
95
MAXVALUE \fImaxvalue\fR
96
determines the maximum value for the sequence\&. If this clause is not supplied or
98
is specified, then default values will be used\&. The defaults are 263\-1 and \-1 for ascending and descending sequences, respectively\&.
104
START WITH \fIstart\fR
105
allows the sequence to begin anywhere\&. The default starting value is
107
for ascending sequences and
109
for descending ones\&.
116
specifies how many sequence numbers are to be preallocated and stored in memory for faster access\&. The minimum value is 1 (only one value can be generated at a time, i\&.e\&., no cache), and this is also the default\&.
123
option allows the sequence to wrap around when the
127
has been reached by an ascending or descending sequence respectively\&. If the limit is reached, the next number generated will be the
130
\fImaxvalue\fR, respectively\&.
134
is specified, any calls to
136
after the sequence has reached its maximum value will return an error\&. If neither
145
OWNED BY \fItable\fR\&.\fIcolumn\fR, OWNED BY NONE
149
option causes the sequence to be associated with a specific table column, such that if that column (or its whole table) is dropped, the sequence will be automatically dropped as well\&. The specified table must have the same owner and be in the same schema as the sequence\&.
150
OWNED BY NONE, the default, specifies that there is no such association\&.
156
to remove a sequence\&.
158
Sequences are based on
160
arithmetic, so the range cannot exceed the range of an eight\-byte integer (\-9223372036854775808 to 9223372036854775807)\&. On some older platforms, there might be no compiler support for eight\-byte integers, in which case sequences use regular
162
arithmetic (range \-2147483648 to +2147483647)\&.
164
Unexpected results might be obtained if a
166
setting greater than one is used for a sequence object that will be used concurrently by multiple sessions\&. Each session will allocate and cache successive sequence values during one access to the sequence object and increase the sequence object\(aqs
168
accordingly\&. Then, the next
169
\fIcache\fR\-1 uses of
171
within that session simply return the preallocated values without touching the sequence object\&. So, any numbers allocated but not used within a session will be lost when that session ends, resulting in
175
Furthermore, although multiple sessions are guaranteed to allocate distinct sequence values, the values might be generated out of sequence when all the sessions are considered\&. For example, with a
177
setting of 10, session A might reserve values 1\&.\&.10 and return
178
\fBnextval\fR=1, then session B might reserve values 11\&.\&.20 and return
179
\fBnextval\fR=11 before session A has generated
180
nextval=2\&. Thus, with a
182
setting of one it is safe to assume that
184
values are generated sequentially; with a
186
setting greater than one you should only assume that the
188
values are all distinct, not that they are generated purely sequentially\&. Also,
190
will reflect the latest value reserved by any session, whether or not it has yet been returned by
193
Another consideration is that a
195
executed on such a sequence will not be noticed by other sessions until they have used up any preallocated values they have cached\&.
198
Create an ascending sequence called
199
serial, starting at 101:
205
CREATE SEQUENCE serial START 101;
211
Select the next number from this sequence:
217
SELECT nextval(\(aqserial\(aq);
227
Select the next number from this sequence:
233
SELECT nextval(\(aqserial\(aq);
243
Use this sequence in an
251
INSERT INTO distributors VALUES (nextval(\(aqserial\(aq), \(aqnothing\(aq);
257
Update the sequence value after a
265
COPY distributors FROM \(aqinput_file\(aq;
266
SELECT setval(\(aqserial\(aq, max(id)) FROM distributors;
277
standard, with the following exceptions:
289
expression is not supported\&.
300
Obtaining the next value is done using the
302
function instead of the standard\(aqs
322
ALTER SEQUENCE (\fBALTER_SEQUENCE\fR(7)), DROP SEQUENCE (\fBDROP_SEQUENCE\fR(7))