> For the complete documentation index, see [llms.txt](https://docs.postgresql.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.postgresql.tw/the-sql-language/functions/functions-sequence.md).

# 9.17. Sequence Manipulation Functions \#

This section describes functions for operating on *sequence objects*, also called sequence generators or just sequences. Sequence objects are special single-row tables created with [CREATE SEQUENCE](/reference/sql-commands/sql-createsequence.md). Sequence objects are commonly used to generate unique identifiers for rows of a table. The sequence functions, listed in [Table 9.55](#FUNCTIONS-SEQUENCE-TABLE), provide simple, multiuser-safe methods for obtaining successive sequence values from sequence objects.

**Table 9.55. Sequence Functions**

<table data-header-hidden><thead><tr><th></th></tr></thead><tbody><tr><td><p>Function</p><p>Description</p></td></tr><tr><td><p><code>nextval</code> ( <code>regclass</code> ) → <code>bigint</code></p><p>Advances the sequence object to its next value and returns that value. This is done atomically: even if multiple sessions execute <code>nextval</code> concurrently, each will safely receive a distinct sequence value. If the sequence object has been created with default parameters, successive <code>nextval</code> calls will return successive values beginning with 1. Other behaviors can be obtained by using appropriate parameters in the <a href="/pages/-LIPmg41oP7CQLli71gT">CREATE SEQUENCE</a> command.</p><p>This function requires <code>USAGE</code> or <code>UPDATE</code> privilege on the sequence.</p></td></tr><tr><td><p><code>setval</code> ( <code>regclass</code>, <code>bigint</code> [, <code>boolean</code> ] ) → <code>bigint</code></p><p>Sets the sequence object's current value, and optionally its <code>is_called</code> flag. The two-parameter form sets the sequence's <code>last_value</code> field to the specified value and sets its <code>is_called</code> field to <code>true</code>, meaning that the next <code>nextval</code> will advance the sequence before returning a value. The value that will be reported by <code>currval</code> is also set to the specified value. In the three-parameter form, <code>is_called</code> can be set to either <code>true</code> or <code>false</code>. <code>true</code> has the same effect as the two-parameter form. If it is set to <code>false</code>, the next <code>nextval</code> will return exactly the specified value, and sequence advancement commences with the following <code>nextval</code>. Furthermore, the value reported by <code>currval</code> is not changed in this case. For example,</p><pre><code>SELECT setval('myseq', 42);           Next nextval will return 43
SELECT setval('myseq', 42, true);     Same as above
SELECT setval('myseq', 42, false);    Next nextval will return 42
</code></pre><p>The result returned by <code>setval</code> is just the value of its second argument.</p><p>This function requires <code>UPDATE</code> privilege on the sequence.</p></td></tr><tr><td><p><code>currval</code> ( <code>regclass</code> ) → <code>bigint</code></p><p>Returns the value most recently obtained by <code>nextval</code> for this sequence in the current session. (An error is reported if <code>nextval</code> 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 executed <code>nextval</code> since the current session did.</p><p>This function requires <code>USAGE</code> or <code>SELECT</code> privilege on the sequence.</p></td></tr><tr><td><p><code>lastval</code> () → <code>bigint</code></p><p>Returns the value most recently returned by <code>nextval</code> in the current session. This function is identical to <code>currval</code>, except that instead of taking the sequence name as an argument it refers to whichever sequence <code>nextval</code> was most recently applied to in the current session. It is an error to call <code>lastval</code> if <code>nextval</code> has not yet been called in the current session.</p><p>This function requires <code>USAGE</code> or <code>SELECT</code> privilege on the last used sequence.</p></td></tr></tbody></table>

<br>

## Caution

To avoid blocking concurrent transactions that obtain numbers from the same sequence, the value obtained by `nextval` 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 an `INSERT` with an `ON CONFLICT` clause will compute the to-be-inserted tuple, including doing any required `nextval` calls, before detecting any conflict that would cause it to follow the `ON CONFLICT` rule instead. Thus, PostgreSQL sequence objects *cannot be used to obtain “gapless” sequences*.

Likewise, sequence state changes made by `setval` 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 a `nextval` or `setval` 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 the `nextval` call has been committed before doing so.

The sequence to be operated on by a sequence function is specified by a `regclass` argument, which is simply 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. See [Section 8.19](/the-sql-language/datatype/datatype-oid.md) for details.

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/functions-sequence.html)（英文原文，待翻譯）


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.postgresql.tw/the-sql-language/functions/functions-sequence.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
