|PostgreSQL Reference Manual - Volume 1 - SQL Language Reference|
by The PostgreSQL Global Development Group
Paperback (6"x9"), 716 pages
RRP £32.00 ($49.95)
Sales of this book support the PostgreSQL project! Get a printed copy>>>
7.12 Sequence Manipulation Functions
This section describes PostgreSQL's functions
for operating on sequence objects.
Sequence objects (also called sequence generators or
just sequences) are special single-row tables created with
CREATE SEQUENCE. A sequence object is usually used to
generate unique identifiers for rows of a table. The sequence functions,
listed in Table 7-34,
provide simple, multiuser-safe methods for obtaining successive
sequence values from sequence objects.
|Function||Return Type|| Description
|| || Return value most recently obtained with
|| || Advance sequence and return new value
|| || Set sequence's current value
|| || Set sequence's current value and |
The sequence to be operated on by a sequence-function call is specified by
regclass argument, which is just the OID of the sequence in the
pg_class system catalog. You do not have to look up the
OID by hand, however, since the
regclass data type's input
converter will do the work for you. Just write the sequence name enclosed
in single quotes, so that it looks like a literal constant. To
achieve some compatibility with the handling of ordinary
SQL names, the string will be converted to lowercase
unless it contains double quotes around the sequence name. Thus
nextval('foo') operates on sequence foo nextval('FOO') operates on sequence foo nextval('"Foo"') operates on sequence Foo
The sequence name can be schema-qualified if necessary:
nextval('myschema.foo') operates on myschema.foo nextval('"myschema".foo') same as above nextval('foo') searches search path for foo
See section 6.12 Object Identifier Types for more information about
Note: Before PostgreSQL 8.1, the arguments of the sequence functions were of type
regclass, and the above-described conversion from a text string to an OID value would happen at run time during each call. For backwards compatibility, this facility still exists, but internally it is now handled as an implicit coercion from
regclassbefore the function is invoked.
When you write the argument of a sequence function as an unadorned literal string, it becomes a constant of type
regclass. Since this is really just an OID, it will track the originally identified sequence despite later renaming, schema reassignment, etc. This “early binding” behavior is usually desirable for sequence references in column defaults and views. But sometimes you will want “late binding” where the sequence reference is resolved at run time. To get late-binding behavior, force the constant to be stored as a
textconstant instead of
regclass:nextval('foo'::text) foo is looked up at runtime
Note that late binding was the only behavior supported in PostgreSQL releases before 8.1, so you may need to do this to preserve the semantics of old applications.
Of course, the argument of a sequence function can be an expression as well as a constant. If it is a text expression then the implicit coercion will result in a run-time lookup.
The available sequence functions are:
Advance the sequence object to its next value and return that
value. This is done atomically: even if multiple sessions
nextvalconcurrently, each will safely receive a distinct sequence value.
Return the value most recently obtained by
nextvalfor this sequence in the current session. (An error is reported if
nextvalhas never been called for this sequence in this session.) Notice that because this is returning a session-local value, it gives a predictable answer whether or not other sessions have executed
nextvalsince the current session did.
Return the value most recently returned by
nextvalin the current session. This function is identical to
currval, except that instead of taking the sequence name as an argument it fetches the value of the last sequence that
nextvalwas used on in the current session. It is an error to call
nextvalhas not yet been called in the current session.
Reset the sequence object's counter value. The two-parameter
form sets the sequence's
last_valuefield to the specified value and sets its
true, meaning that the next
nextvalwill advance the sequence before returning a value. In the three-parameter form,
is_calledmay be set either
false. If it's set to
false, the next
nextvalwill return exactly the specified value, and sequence advancement commences with the following
nextval. For example,
SELECT setval('foo', 42); Next nextval will return 43 SELECT setval('foo', 42, true); Same as above SELECT setval('foo', 42, false); Next nextval will return 42The result returned by
setvalis just the value of its second argument.
If a sequence object has been created with default parameters,
nextval calls on it will return successive values
beginning with 1. Other behaviors can be obtained by using
special parameters in the
CREATE SEQUENCE command;
see its command reference page for more information.
Important: To avoid blocking of concurrent transactions that obtain numbers from the same sequence, a
nextvaloperation is never rolled back; that is, once a value has been fetched it is considered used, even if the transaction that did the
nextvallater aborts. This means that aborted transactions may leave unused “holes” in the sequence of assigned values.
setvaloperations are never rolled back, either.
|ISBN 0954612027||PostgreSQL Reference Manual - Volume 1 - SQL Language Reference||See the print edition|