Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
9.17. Sequence Manipulation Functions
Prev UpChapter 9. Functions and OperatorsHome Next

9.17. 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.53, provide simple, multiuser-safe methods for obtaining successive sequence values from sequence objects.

Table 9.53. Sequence Functions

Function

Description

nextval (regclass ) →bigint

Advances the sequence object to its next value and returns that value. This is done atomically: even if multiple sessions executenextval concurrently, each will safely receive a distinct sequence value. If the sequence object has been created with default parameters, successivenextval calls will return successive values beginning with 1. Other behaviors can be obtained by using appropriate parameters in theCREATE SEQUENCE command.

This function requiresUSAGE orUPDATE privilege on the sequence.

setval (regclass,bigint [,boolean] ) →bigint

Sets the sequence object's current value, and optionally itsis_called flag. 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. The value that will be 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('myseq', 42);Nextnextval will return 43SELECT setval('myseq', 42, true);Same as aboveSELECT setval('myseq', 42, false);Nextnextval will return 42

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

This function requiresUPDATE privilege on the sequence.

currval (regclass ) →bigint

Returns 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.) 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.

This function requiresUSAGE orSELECT privilege on the sequence.

lastval () →bigint

Returns the value most recently returned bynextval in the current session. This function is identical tocurrval, except that instead of taking the sequence name as an argument it refers to whichever sequencenextval was most recently applied to in the current session. It is an error to calllastval ifnextval has not yet been called in the current session.

This function requiresUSAGE orSELECT privilege on the last used sequence.


Caution

To avoid blocking concurrent transactions that obtain numbers from the same sequence, the value obtained bynextval is not reclaimed for re-use if the calling transaction later aborts. This means that transaction aborts or database crashes can result in gaps in the sequence of assigned values. That can happen without a transaction abort, too. For example anINSERT with anON CONFLICT clause will compute the to-be-inserted tuple, including doing any requirednextval calls, before detecting any conflict that would cause it to follow theON CONFLICT rule instead. Thus,PostgreSQL sequence objectscannot be used to obtaingapless sequences.

Likewise, sequence state changes made bysetval are immediately visible to other transactions, and are not undone if the calling transaction rolls back.

If the database cluster crashes before committing a transaction containing anextval orsetval call, the sequence state change might not have made its way to persistent storage, so that it is uncertain whether the sequence will have its original or updated state after the cluster restarts. This is harmless for usage of the sequence within the database, since other effects of uncommitted transactions will not be visible either. However, if you wish to use a sequence value for persistent outside-the-database purposes, make sure that thenextval call has been committed before doing so.

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. SeeSection 8.19 for details.


Prev Up Next
9.16. JSON Functions and Operators Home 9.18. Conditional Expressions
pdfepub
Go to PostgreSQL 17
By continuing to browse this website, you agree to the use of cookies. Go toPrivacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp