PostgreSQL 9.4.1 Documentation | |||
---|---|---|---|
Prev | Up | Chapter 9. Functions and Operators | Next |
9.16. Sequence Manipulation Functions
This section describes functions for operating onsequence objects, also called sequence generators or just sequences. Sequence objects are special single-row tables created withCREATE SEQUENCE. Sequence objects are commonly used to generate unique identifiers for rows of a table. The sequence functions, listed inTable 9-44, provide simple, multiuser-safe methods for obtaining successive sequence values from sequence objects.
Table 9-44. Sequence Functions
Function | Return Type | Description |
---|---|---|
currval(regclass) | bigint | Return value most recently obtained withnextval for specified sequence |
lastval() | bigint | Return value most recently obtained withnextval for any sequence |
nextval(regclass) | bigint | Advance sequence and return new value |
setval(regclass,bigint) | bigint | Set sequence's current value |
setval(regclass,bigint,boolean) | bigint | Set sequence's current value andis_called flag |
The sequence to be operated on by a sequence function is specified by aregclass argument, which is simply the OID of the sequence in thepg_class system catalog. You do not have to look up the OID by hand, however, since theregclass 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. For compatibility with the handling of ordinarySQL names, the string will be converted to lower case unless it contains double quotes around the sequence name. Thus: The sequence name can be schema-qualified if necessary: SeeSection 8.18 for more information aboutregclass. Note: BeforePostgreSQL 8.1, the arguments of the sequence functions were of typetext, notregclass, and the above-described conversion from a text string to an OID value would happen at run time during each call. For backward compatibility, this facility still exists, but internally it is now handled as an implicit coercion fromtext toregclass before the function is invoked. When you write the argument of a sequence function as an unadorned literal string, it becomes a constant of typeregclass. 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 might want"late binding" where the sequence reference is resolved at run time. To get late-binding behavior, force the constant to be stored as atext constant instead ofregclass: Note that late binding was the only behavior supported inPostgreSQL releases before 8.1, so you might 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 execute If a sequence object has been created with default parameters, successive Important: To avoid blocking concurrent transactions that obtain numbers from the same sequence, a Return the value most recently obtained by Return the value most recently returned by Reset the sequence object's counter value. The two-parameter form sets the sequence'slast_value field to the specified value and sets itsis_called field totrue, meaning that the next The result returned by Important: Because sequences are non-transactional, changes made bynextval('foo')operates on sequencefoonextval('FOO')operates on sequencefoonextval('"Foo"')operates on sequenceFoo
nextval('myschema.foo')operates onmyschema.foonextval('"myschema".foo')same as abovenextval('foo')searches search path forfoo
nextval('foo'::text)foo is looked up at runtime
nextval
nextval
concurrently, each will safely receive a distinct sequence value.nextval
calls will return successive values beginning with 1. Other behaviors can be obtained by using special parameters in theCREATE SEQUENCE command; see its command reference page for more information.nextval
operation is never rolled back; that is, once a value has been fetched it is considered used, even if the transaction that did thenextval
later aborts. This means that aborted transactions might leave unused"holes" in the sequence of assigned values.currval
nextval
for this sequence in the current session. (An error is reported ifnextval
has never been called for this sequence in this session.) Because this is returning a session-local value, it gives a predictable answer whether or not other sessions have executednextval
since the current session did.lastval
nextval
in the current session. This function is identical tocurrval
, except that instead of taking the sequence name as an argument it fetches the value of the last sequence used bynextval
in the current session. It is an error to calllastval
ifnextval
has not yet been called in the current session.setval
nextval
will advance the sequence before returning a value. The value reported bycurrval
is also set to the specified value. In the three-parameter form,is_called can be set to eithertrue orfalse.true has the same effect as the two-parameter form. If it is set tofalse, the nextnextval
will return exactly the specified value, and sequence advancement commences with the followingnextval
. Furthermore, the value reported bycurrval
is not changed in this case. For example,SELECT setval('foo', 42);Next
nextval
will return 43SELECT setval('foo', 42, true);Same as aboveSELECT setval('foo', 42, false);Nextnextval
will return 42setval
is just the value of its second argument.setval
are not undone if the transaction rolls back.