Movatterモバイル変換


[0]ホーム

URL:


July 17, 2025: | PostgreSQL 18 Beta 2 Released!
Supported Versions:Current (17) /16 /15 /14 /13
Development Versions:18 /devel
Unsupported versions:12 /11 /10 /9.6 /9.5 /9.4 /9.3 /9.2 /9.1 /9.0 /8.4 /8.3 /8.2 /8.1 /8.0 /7.4 /7.3 /7.2
This documentation is for an unsupported version of PostgreSQL.
You may want to view the same page for thecurrent version, or one of the other supported versions listed above instead.
PostgreSQL 7.3.21 Documentation
PrevChapter 6. Functions and OperatorsNext

6.11. Sequence-Manipulation Functions

This section describesPostgreSQL's functions for operating onsequence objects. Sequence objects (also called sequence generators or just sequences) are special single-row tables created withCREATE SEQUENCE. A sequence object is usually used to generate unique identifiers for rows of a table. The sequence functions, listed inTable 6-26, provide simple, multiuser-safe methods for obtaining successive sequence values from sequence objects.

Table 6-26. Sequence Functions

FunctionReturnsDescription
nextval(text)bigintAdvance sequence and return new value
currval(text)bigintReturn value most recently obtained withnextval
setval(text,bigint)bigintSet sequence's current value
setval(text,bigint,boolean)bigintSet sequence's current value andis_called flag

For largely historical reasons, the sequence to be operated on by a sequence-function call is specified by a text-string argument. To achieve some compatibility with the handling of ordinary SQL names, the sequence functions convert their argument to lower case unless the string is double-quoted. Thus

nextval('foo')operates on sequencefoonextval('FOO')operates on sequencefoonextval('"Foo"')operates on sequenceFoo

The sequence name can be schema-qualified if necessary:

nextval('myschema.foo')operates onmyschema.foonextval('"myschema".foo')same as abovenextval('foo')searches search path forfoo

Of course, the text argument can be the result of an expression, not only a simple literal, which is occasionally useful.

The available sequence functions are:

nextval

Advance the sequence object to its next value and return that value. This is done atomically: even if multiple sessions executenextval concurrently, each will safely receive a distinct sequence value.

currval

Return the value most recently obtained bynextval for this sequence in the current session. (An error is reported ifnextval has never been called for this sequence in this session.) Notice that because this is returning a session-local value, it gives a predictable answer even if other sessions are executingnextval meanwhile.

setval

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 nextnextval will advance the sequence before returning a value. In the three-parameter form,is_called may be set eithertrue orfalse. If it's set tofalse, the nextnextval will return exactly the specified value, and sequence advancement commences with the followingnextval. For example,

SELECT setval('foo', 42);Nextnextval() will return 43SELECT setval('foo', 42, true);Same as aboveSELECT setval('foo', 42, false);Nextnextval() will return 42

The result returned bysetval is just the value of its second argument.

Important: To avoid blocking of concurrent transactions that obtain numbers from the same sequence, anextval 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 may leave unused"holes" in the sequence of assigned values.setval operations are never rolled back, either.

If a sequence object has been created with default parameters,nextval() calls on it will return successive values beginning with one. Other behaviors can be obtained by using special parameters in theCREATE SEQUENCE command; see its command reference page for more information.


PrevHomeNext
Network Address Type FunctionsUpConditional Expressions

[8]ページ先頭

©2009-2025 Movatter.jp