> 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/19/the-sql-language/functions/functions-sequence.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# 9.17. 序列操作函式 \#

### 9.17. 序列操作函式 [#](#functions-sequence) <a href="#functions-sequence" id="functions-sequence"></a>

本節說明用於操作*序列物件*（sequence object）的函式；序列物件也稱為序列產生器，或簡稱序列。序列物件是以 [CREATE SEQUENCE](https://docs.postgresql.tw/19/reference/sql-commands/sql-createsequence.md) 建立的特殊單列資料表。序列物件通常用來為資料表的資料列產生唯一識別碼。[表 9.55](#functions-sequence-table) 所列的序列函式，提供了從序列物件取得連續序列值的簡單、多使用者安全的方法。

#### 表 9.55. 序列函式 [#](#functions-sequence-table) <a href="#functions-sequence-table" id="functions-sequence-table"></a>

<table data-header-hidden><thead><tr><th></th></tr></thead><tbody><tr><td><p>函式</p><p>說明</p></td></tr><tr><td><p><code>nextval</code> ( <code>regclass</code> ) → <code>bigint</code></p><p>將序列物件推進到下一個值，並回傳該值。這是以原子方式完成的：即使多個工作階段同時執行 <code>nextval</code>，每個工作階段也都會安全地取得不同的序列值。如果序列物件是以預設參數建立的，連續的 <code>nextval</code> 呼叫會回傳從 1 開始的連續值。在 <a href="https://docs.postgresql.tw/19/reference/sql-commands/sql-createsequence.md">CREATE SEQUENCE</a> 命令中使用適當的參數，可以得到其他行為。</p><p>這個函式需要序列上的 <code>USAGE</code> 或 <code>UPDATE</code> 權限。</p></td></tr><tr><td><p><code>setval</code> ( <code>regclass</code>, <code>bigint</code> [, <code>boolean</code> ] ) → <code>bigint</code></p><p>設定序列物件的目前值，並可選擇性地設定其 <code>is_called</code> 旗標。雙參數形式會將序列的 <code>last_value</code> 欄位設為指定的值，並將其 <code>is_called</code> 欄位設為 <code>true</code>，這表示下一次 <code>nextval</code> 會先推進序列，再回傳值。<code>currval</code> 所回報的值也會被設為指定的值。在三參數形式中，<code>is_called</code> 可以設為 <code>true</code> 或 <code>false</code>。<code>true</code> 的效果與雙參數形式相同。如果設為 <code>false</code>，下一次 <code>nextval</code> 會正好回傳指定的值，而序列會從再下一次 <code>nextval</code> 開始推進。此外，在這種情況下，<code>currval</code> 所回報的值不會改變。例如，</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><code>setval</code> 回傳的結果就是它第二個引數的值。</p><p>這個函式需要序列上的 <code>UPDATE</code> 權限。</p></td></tr><tr><td><p><code>currval</code> ( <code>regclass</code> ) → <code>bigint</code></p><p>回傳目前工作階段中，<code>nextval</code> 最近一次為這個序列取得的值。（如果在這個工作階段中從未為這個序列呼叫過 <code>nextval</code>，就會回報錯誤。）由於回傳的是工作階段本機的值，無論其他工作階段在目前工作階段之後是否執行過 <code>nextval</code>，它都會提供可預測的答案。</p><p>這個函式需要序列上的 <code>USAGE</code> 或 <code>SELECT</code> 權限。</p></td></tr><tr><td><p><code>lastval</code> () → <code>bigint</code></p><p>回傳目前工作階段中 <code>nextval</code> 最近一次回傳的值。這個函式與 <code>currval</code> 相同，只是它不以序列名稱作為引數，而是參照目前工作階段中 <code>nextval</code> 最近一次套用的那個序列。呼叫 <code>lastval</code> 時，如果目前工作階段中還沒有呼叫過 <code>nextval</code>，就會發生錯誤。</p><p>這個函式需要最後使用之序列上的 <code>USAGE</code> 或 <code>SELECT</code> 權限。</p></td></tr></tbody></table>

<br>

#### 警示

為了避免阻擋從同一個序列取得數字的並行交易，如果呼叫的交易之後中止，由 `nextval` 取得的值不會被收回重複使用。這表示交易中止或資料庫當機，可能會使指派出去的值序列中出現間隙。即使沒有交易中止，這種情況也可能發生。例如，帶有 `ON CONFLICT` 子句的 `INSERT`，會在偵測到任何會使它改為遵循 `ON CONFLICT` 規則的衝突之前，先計算要插入的 tuple，包括進行任何必要的 `nextval` 呼叫。因此，PostgreSQL 的序列物件*無法用來取得「無間隙」的序列*。

同樣地，由 `setval` 所做的序列狀態變更，會立即對其他交易可見，而且即使呼叫的交易回復，也不會被撤銷。

如果資料庫叢集在包含 `nextval` 或 `setval` 呼叫的交易提交之前當機，序列狀態的變更可能還沒有寫入持久性儲存體，因此在叢集重新啟動之後，無法確定序列會處於原本的狀態還是更新後的狀態。對於在資料庫內部使用序列而言，這並無害處，因為未提交交易的其他效果同樣也不會可見。不過，如果你想將序列值用於資料庫外部的持久性用途，請確定 `nextval` 呼叫已經提交之後再這麼做。

序列函式所要操作的序列，是由一個 `regclass` 引數指定的，它就是該序列在 `pg_class` 系統目錄中的 OID。不過，你不需要手動查找 OID，因為 `regclass` 資料型別的輸入轉換器會替你完成這項工作。詳情請參閱[第 8.19 節](https://docs.postgresql.tw/19/the-sql-language/datatype/datatype-oid.md)。

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/functions-sequence.html)（原文版本：18.6；核對日期：2026-09-11）


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
