2 $Header: /cvsroot/pgsql/doc/src/sgml/ref/create_sequence.sgml,v 1.15 2000/12/08 20:06:58 tgl Exp $
6 <refentry id="SQL-CREATESEQUENCE">
8 <refentrytitle id="sql-createsequence-title">
11 <refmiscinfo>SQL - Language Statements</refmiscinfo>
18 Creates a new sequence number generator
23 <date>1999-07-20</date>
26 CREATE SEQUENCE <replaceable class="parameter">seqname</replaceable> [ INCREMENT <replaceable class="parameter">increment</replaceable> ]
27 [ MINVALUE <replaceable class="parameter">minvalue</replaceable> ] [ MAXVALUE <replaceable class="parameter">maxvalue</replaceable> ]
28 [ START <replaceable class="parameter">start</replaceable> ] [ CACHE <replaceable class="parameter">cache</replaceable> ] [ CYCLE ]
31 <refsect2 id="R2-SQL-CREATESEQUENCE-1">
33 <date>1998-09-11</date>
42 <term><replaceable class="parameter">seqname</replaceable></term>
45 The name of a sequence to be created.
51 <term><replaceable class="parameter">increment</replaceable></term>
55 <option>INCREMENT <replaceable class="parameter">increment</replaceable></option>
56 clause is optional. A positive value will make an
57 ascending sequence, a negative one a descending sequence.
58 The default value is one (1).
64 <term><replaceable class="parameter">minvalue</replaceable></term>
67 The optional clause <option>MINVALUE
68 <replaceable class="parameter">minvalue</replaceable></option>
69 determines the minimum value
70 a sequence can generate. The defaults are 1 and -2147483647 for
71 ascending and descending sequences, respectively.
77 <term><replaceable class="parameter">maxvalue</replaceable></term>
80 The optional clause <option>MAXVALUE
81 <replaceable class="parameter">maxvalue</replaceable></option>
82 determines the maximum
83 value for the sequence. The defaults are 2147483647 and -1 for
84 ascending and descending sequences, respectively.
90 <term><replaceable class="parameter">start</replaceable></term>
93 The optional <option>START
94 <replaceable class="parameter">start</replaceable>
95 clause</option> enables the sequence to begin anywhere.
96 The default starting value is
97 <replaceable class="parameter">minvalue</replaceable>
98 for ascending sequences and
99 <replaceable class="parameter">maxvalue</replaceable>
106 <term><replaceable class="parameter">cache</replaceable></term>
109 The <option>CACHE <replaceable class="parameter">cache</replaceable></option> option
110 enables sequence numbers to be preallocated
111 and stored in memory for faster access. The minimum
112 value is 1 (only one value can be generated at a time, i.e., no cache)
113 and this is also the default.
122 The optional CYCLE keyword may be used to enable the sequence
123 to wrap around when the
124 <replaceable class="parameter">maxvalue</replaceable> or
125 <replaceable class="parameter">minvalue</replaceable> has been
127 an ascending or descending sequence respectively. If the limit is
128 reached, the next number generated will be the
129 <replaceable class="parameter">minvalue</replaceable> or
130 <replaceable class="parameter">maxvalue</replaceable>,
139 <refsect2 id="R2-SQL-CREATESEQUENCE-2">
141 <date>1998-09-11</date>
150 <term><computeroutput>
152 </computeroutput></term>
155 Message returned if the command is successful.
160 <term><computeroutput>
161 ERROR: Relation '<replaceable class="parameter">seqname</replaceable>' already exists
162 </computeroutput></term>
165 If the sequence specified already exists.
170 <term><computeroutput>
171 ERROR: DefineSequence: MINVALUE (<replaceable class="parameter">start</replaceable>) can't be >= MAXVALUE (<replaceable class="parameter">max</replaceable>)
172 </computeroutput></term>
175 If the specified starting value is out of range.
180 <term><computeroutput>
181 ERROR: DefineSequence: START value (<replaceable class="parameter">start</replaceable>) can't be < MINVALUE (<replaceable class="parameter">min</replaceable>)
182 </computeroutput></term>
185 If the specified starting value is out of range.
190 <term><computeroutput>
191 ERROR: DefineSequence: MINVALUE (<replaceable class="parameter">min</replaceable>) can't be >= MAXVALUE (<replaceable class="parameter">max</replaceable>)
192 </computeroutput></term>
195 If the minimum and maximum values are inconsistent.
204 <refsect1 id="R1-SQL-CREATESEQUENCE-1">
206 <date>1998-09-11</date>
212 <command>CREATE SEQUENCE</command> will enter a new sequence number generator
213 into the current data base. This involves creating and initializing a
215 table with the name <replaceable class="parameter">seqname</replaceable>.
216 The generator will be owned by the user issuing the command.
220 After a sequence is created, you may use the function
221 <function>nextval('<replaceable class="parameter">seqname</replaceable>')</function>
222 to get a new number from the sequence.
224 <function>currval('<replaceable class="parameter">seqname</replaceable>')</function>
225 may be used to determine the number returned by the last call to
226 <function>nextval('<replaceable class="parameter">seqname</replaceable>')</function>
227 for the specified sequence in the current session.
229 <function>setval('<replaceable class="parameter">seqname</replaceable>',
230 <replaceable class="parameter">newvalue</replaceable>)</function>
231 may be used to set the current value of the specified sequence.
233 <function>nextval('<replaceable class="parameter">seqname</replaceable>')</function>
234 will return the given value plus the sequence increment.
241 SELECT * FROM <replaceable>seqname</replaceable>;
244 to examine the parameters of a sequence.
246 As an alternative to fetching the
247 parameters from the original definition as above, you can use
250 SELECT last_value FROM <replaceable>seqname</replaceable>;
253 to obtain the last value allocated by any backend.
257 To avoid blocking of concurrent transactions
258 that obtain numbers from the same sequence, a nextval operation
259 is never rolled back; that is, once a value has been fetched it is
260 considered used, even if the transaction that did the nextval later
261 aborts. This means that aborted transactions may leave unused "holes"
262 in the sequence of assigned values. setval operations are never
268 Unexpected results may be obtained if a cache setting greater than one
269 is used for a sequence object that will be used concurrently by multiple
270 backends. Each backend will allocate and cache successive sequence values
271 during one access to the sequence object and increase the sequence
272 object's last_value accordingly. Then, the next cache-1 uses of nextval
273 within that backend simply return the preallocated values without touching
274 the shared object. So, numbers allocated but not used in the current session
275 will be lost. Furthermore, although multiple backends are guaranteed to
276 allocate distinct sequence values, the values may be generated out of
277 sequence when all the backends are considered. (For example, with a cache
278 setting of 10, backend A might reserve values 1..10 and return nextval=1,
280 backend B might reserve values 11..20 and return nextval=11 before backend
281 A has generated nextval=2.) Thus, with a cache setting of one it is safe
282 to assume that nextval values are generated sequentially; with a cache
283 setting greater than one you should only assume that the nextval values
284 are all distinct, not that they are generated purely sequentially.
285 Also, last_value will reflect the latest value reserved by any backend,
286 whether or not it has yet been returned by nextval.
287 Another consideration is that a setval executed on such a sequence
288 will not be noticed by other backends until they have used up any
289 preallocated values they have cached.
293 <refsect2 id="R2-SQL-CREATESEQUENCE-3">
295 <date>1998-09-11</date>
301 Use <command>DROP SEQUENCE</command> to remove a sequence.
304 Each backend uses its own cache to store preallocated numbers.
305 Numbers that are cached but not used in the current session will be
306 lost, resulting in "holes" in the sequence.
311 <refsect1 id="R1-SQL-CREATESEQUENCE-2">
316 Create an ascending sequence called <literal>serial</literal>, starting at 101:
319 CREATE SEQUENCE serial START 101;
322 Select the next number from this sequence:
324 SELECT NEXTVAL ('serial');
332 Use this sequence in an INSERT:
334 INSERT INTO distributors VALUES (NEXTVAL('serial'),'nothing');
339 Set the sequence value after a COPY FROM:
341 CREATE FUNCTION distributors_id_max() RETURNS INT4
342 AS 'SELECT max(id) FROM distributors'
345 COPY distributors FROM 'input_file';
346 SELECT setval('serial', distributors_id_max());
352 <refsect1 id="R1-SQL-CREATESEQUENCE-3">
357 <refsect2 id="R2-SQL-CREATESEQUENCE-4">
359 <date>1998-09-11</date>
366 <command>CREATE SEQUENCE</command> is a <productname>Postgres</productname>
368 There is no <command>CREATE SEQUENCE</command> statement
369 in <acronym>SQL92</acronym>.
375 <!-- Keep this comment at the end of the file
380 sgml-minimize-attributes:nil
381 sgml-always-quote-attributes:t
384 sgml-parent-document:nil
385 sgml-default-dtd-file:"../reference.ced"
386 sgml-exposed-tags:nil
387 sgml-local-catalogs:"/usr/lib/sgml/catalog"
388 sgml-local-ecat-files:nil