11<!--
2- $Header: /cvsroot/pgsql/doc/src/sgml/ref/create_sequence.sgml,v 1.20 2001/09/13 15:55:24 petere Exp $
2+ $Header: /cvsroot/pgsql/doc/src/sgml/ref/create_sequence.sgml,v 1.21 2001/11/17 22:20:34 tgl Exp $
33Postgres documentation
44-->
55
@@ -15,7 +15,7 @@ Postgres documentation
1515 CREATE SEQUENCE
1616 </refname>
1717 <refpurpose>
18- define a new sequence
18+ define a new sequence generator
1919 </refpurpose>
2020 </refnamediv>
2121 <refsynopsisdiv>
@@ -42,8 +42,8 @@ CREATE [ TEMPORARY | TEMP ] SEQUENCE <replaceable class="parameter">seqname</rep
4242 <term>TEMPORARY or TEMP</term>
4343 <listitem>
4444 <para>
45- If specified, the sequence is created only for this session, and is
46- automatically dropped on session exit.
45+ If specified, the sequenceobject is created only for this session,
46+ and is automatically dropped on session exit.
4747Existing permanent sequences with the same name are not visible
4848(in this session) while the temporary sequence exists.
4949 </para>
@@ -141,6 +141,8 @@ CREATE [ TEMPORARY | TEMP ] SEQUENCE <replaceable class="parameter">seqname</rep
141141<replaceable class="parameter">minvalue</replaceable> or
142142<replaceable class="parameter">maxvalue</replaceable>,
143143respectively.
144+ Without CYCLE, after the limit is reached <function>nextval</> calls
145+ will return an error.
144146 </para>
145147 </listitem>
146148 </varlistentry>
@@ -222,81 +224,57 @@ ERROR: DefineSequence: MINVALUE (<replaceable class="parameter">min</replaceabl
222224 </title>
223225 <para>
224226 <command>CREATE SEQUENCE</command> will enter a new sequence number generator
225- into the currentdata base . This involves creating and initializing a
227+ into the currentdatabase . This involves creating and initializing a
226228 new single-row
227229 table with the name <replaceable class="parameter">seqname</replaceable>.
228230 The generator will be owned by the user issuing the command.
229231 </para>
230232
231233 <para>
232- After a sequence is created, you may use the function
233- <function>nextval('<replaceable class="parameter">seqname</replaceable>')</function>
234- to get a new number from the sequence.
235- The function
236- <function>currval('<replaceable class="parameter">seqname</replaceable>')</function>
237- may be used to determine the number returned by the last call to
238- <function>nextval('<replaceable class="parameter">seqname</replaceable>')</function>
239- for the specified sequence in the current session.
240- The function
241- <function>setval('<replaceable class="parameter">seqname</replaceable>',
242- <replaceable class="parameter">newvalue</replaceable>)</function>
243- may be used to set the current value of the specified sequence.
244- The next call to
245- <function>nextval('<replaceable class="parameter">seqname</replaceable>')</function>
246- will return the given value plus the sequence increment.
234+ After a sequence is created, you use the functions
235+ <function>nextval</function>,
236+ <function>currval</function> and
237+ <function>setval</function>
238+ to operate on the sequence. These functions are documented in
239+ the <citetitle>User's Guide</citetitle>.
247240 </para>
248241
249242 <para>
250- Use a query like
243+ Although you cannot update a sequence directly, you can use a query like
251244
252245 <programlisting>
253246SELECT * FROM <replaceable>seqname</replaceable>;
254247 </programlisting>
255248
256- to examine the parameters of a sequence.
257-
258- As an alternative to fetching the
259- parameters from the original definition as above, you can use
260-
261- <programlisting>
262- SELECT last_value FROM <replaceable>seqname</replaceable>;
263- </programlisting>
264-
265- to obtain the last value allocated by any backend.
266- </para>
267-
268- <para>
269- To avoid blocking of concurrent transactions
270- that obtain numbers from the same sequence, a nextval operation
271- is never rolled back; that is, once a value has been fetched it is
272- considered used, even if the transaction that did the nextval later
273- aborts. This means that aborted transactions may leave unused <quote>holes</quote>
274- in the sequence of assigned values. setval operations are never
275- rolled back, either.
249+ to examine the parameters and current state of a sequence. In particular,
250+ the <literal>last_value</> field of the sequence shows the last value
251+ allocated by any backend process. (Of course, this value may be obsolete
252+ by the time it's printed, if other processes are actively doing
253+ <function>nextval</> calls.)
276254 </para>
277255
278256 <caution>
279257 <para>
280- Unexpected results may be obtained if a cache setting greater than one
258+ Unexpected results may be obtained if a<replaceable class="parameter"> cache</replaceable> setting greater than one
281259 is used for a sequence object that will be used concurrently by multiple
282260 backends. Each backend will allocate and cache successive sequence values
283261 during one access to the sequence object and increase the sequence
284- object's last_value accordingly. Then, the next cache-1 uses of nextval
262+ object's<literal> last_value</> accordingly. Then, the next<replaceable class="parameter"> cache</replaceable> -1 uses of<function> nextval</>
285263 within that backend simply return the preallocated values without touching
286- the shared object. So, numbers allocated but not usedin the current session
287- will be lost. Furthermore, although multiple backends are guaranteed to
264+ the shared object. So,any numbers allocated but not usedwithin a session
265+ will be lost when that session ends . Furthermore, although multiple backends are guaranteed to
288266 allocate distinct sequence values, the values may be generated out of
289- sequence when all the backends are considered. (For example, with a cache
267+ sequence when all the backends are considered. (For example, with a<replaceable class="parameter"> cache</replaceable>
290268 setting of 10, backend A might reserve values 1..10 and return nextval=1,
291269 then
292270 backend B might reserve values 11..20 and return nextval=11 before backend
293- A has generated nextval=2.) Thus, with a cache setting of one it is safe
294- to assume that nextval values are generated sequentially; with a cache
295- setting greater than one you should only assume that the nextval values
271+ A has generated nextval=2.) Thus, with a<replaceable class="parameter"> cache</replaceable> setting of one it is safe
272+ to assume that<function> nextval</> values are generated sequentially; with a<replaceable class="parameter"> cache</replaceable>
273+ setting greater than one you should only assume that the<function> nextval</> values
296274 are all distinct, not that they are generated purely sequentially.
297- Also, last_value will reflect the latest value reserved by any backend,
298- whether or not it has yet been returned by nextval.
299- Another consideration is that a setval executed on such a sequence
275+ Also,<literal> last_value</> will reflect the latest value reserved by any backend,
276+ whether or not it has yet been returned by<function> nextval</> .
277+ Another consideration is that a<function> setval</> executed on such a sequence
300278 will not be noticed by other backends until they have used up any
301279 preallocated values they have cached.
302280 </para>
@@ -313,7 +291,8 @@ SELECT last_value FROM <replaceable>seqname</replaceable>;
313291 Use <command>DROP SEQUENCE</command> to remove a sequence.
314292 </para>
315293 <para>
316- Each backend uses its own cache to store preallocated numbers.
294+ When <replaceable class="parameter">cache</replaceable> is greater than
295+ one, each backend uses its own cache to store preallocated numbers.
317296 Numbers that are cached but not used in the current session will be
318297 lost, resulting in <quote>holes</quote> in the sequence.
319298 </para>
@@ -333,7 +312,7 @@ CREATE SEQUENCE serial START 101;
333312 <para>
334313 Select the next number from this sequence:
335314 <programlisting>
336- SELECTNEXTVAL ('serial');
315+ SELECTnextval ('serial');
337316
338317nextval
339318-------
@@ -343,19 +322,16 @@ nextval
343322 <para>
344323 Use this sequence in an INSERT:
345324 <programlisting>
346- INSERT INTO distributors VALUES (NEXTVAL ('serial'),'nothing');
325+ INSERT INTO distributors VALUES (nextval ('serial'), 'nothing');
347326 </programlisting>
348327 </para>
349328
350329 <para>
351- Set the sequence value after a COPY FROM:
330+ Update the sequence value after a COPY FROM:
352331 <programlisting>
353- CREATE FUNCTION distributors_id_max() RETURNS INT4
354- AS 'SELECT max(id) FROM distributors'
355- LANGUAGE 'sql';
356332BEGIN;
357333 COPY distributors FROM 'input_file';
358- SELECT setval('serial',distributors_id_max()) ;
334+ SELECT setval('serial',max(id)) FROM distributors ;
359335END;
360336 </programlisting>
361337 </para>