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

# 9.27.8. 交易 ID 與快照資訊函式

## 9.27.8. 交易 ID 與快照資訊函式 [#](#functions-info-snapshot) <a href="#functions-info-snapshot" id="functions-info-snapshot"></a>

[表 9.84](#functions-pg-snapshot) 所列的函式以可匯出的形式提供伺服器的交易資訊。這些函式的主要用途是判斷在兩個快照之間有哪些交易已經提交。

### 表 9.84. 交易 ID 與快照資訊函式 [#](#functions-pg-snapshot) <a href="#functions-pg-snapshot" id="functions-pg-snapshot"></a>

| <p>函式</p><p>說明</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><code>age</code> ( <code>xid</code> ) → <code>integer</code></p><p>回傳所提供的交易 ID 與目前交易計數器之間相差的交易數。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| <p><code>mxid\_age</code> ( <code>xid</code> ) → <code>integer</code></p><p>回傳所提供的 multixact ID 與目前 multixact 計數器之間相差的 multixact ID 數。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| <p><code>pg\_current\_xact\_id</code> () → <code>xid8</code></p><p>回傳目前交易的 ID。如果目前交易還沒有 ID（因為它尚未執行任何資料庫更新），就會為它指派一個新的 ID；詳情請參閱<a href="https://docs.postgresql.tw/19/internals/transactions/transaction-id.md">第 67.1 節</a>。如果是在子交易中執行，則會回傳最上層的交易 ID；詳情請參閱<a href="https://docs.postgresql.tw/19/internals/transactions/subxacts.md">第 67.3 節</a>。</p>                                                                                                                                                                                                                                                                                                                                                        |
| <p><code>pg\_current\_xact\_id\_if\_assigned</code> () → <code>xid8</code></p><p>回傳目前交易的 ID；如果尚未指派 ID，則回傳 <code>NULL</code>。（如果交易原本可能是唯讀的，最好使用這個版本，以避免不必要地消耗 XID。）如果是在子交易中執行，則會回傳最上層的交易 ID。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| <p><code>pg\_xact\_status</code> ( <code>xid8</code> ) → <code>text</code></p><p>回報近期某個交易的提交狀態。只要該交易夠近期，使得系統仍保留該交易的提交狀態，結果就會是 <code>in progress</code>、<code>committed</code> 或 <code>aborted</code> 其中之一。如果該交易已經久遠到系統中不再留有任何對它的參照，且提交狀態資訊已被捨棄，則結果為 <code>NULL</code>。應用程式可以使用這個函式，例如在 <code>COMMIT</code> 進行中而應用程式與資料庫伺服器之間的連線中斷後，用來判斷其交易究竟是已提交還是已中止。請注意，已預備的交易會被回報為 <code>in progress</code>；如果應用程式需要判斷某個交易 ID 是否屬於已預備的交易，就必須檢查 <a href="https://docs.postgresql.tw/19/internals/views/view-pg-prepared-xacts.md"><code>pg\_prepared\_xacts</code></a>。</p>                                                                                                                                                 |
| <p><code>pg\_current\_snapshot</code> () → <code>pg\_snapshot</code></p><p>回傳目前的<em>快照</em>，這是一種資料結構，顯示目前有哪些交易 ID 正在進行中。快照中只包含最上層的交易 ID，不會顯示子交易 ID；詳情請參閱<a href="https://docs.postgresql.tw/19/internals/transactions/subxacts.md">第 67.3 節</a>。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| <p><code>pg\_snapshot\_xip</code> ( <code>pg\_snapshot</code> ) → <code>setof xid8</code></p><p>回傳快照中所包含之進行中交易 ID 的集合。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| <p><code>pg\_snapshot\_xmax</code> ( <code>pg\_snapshot</code> ) → <code>xid8</code></p><p>回傳快照的 <code>xmax</code>。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| <p><code>pg\_snapshot\_xmin</code> ( <code>pg\_snapshot</code> ) → <code>xid8</code></p><p>回傳快照的 <code>xmin</code>。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| <p><code>pg\_visible\_in\_snapshot</code> ( <code>xid8</code>, <code>pg\_snapshot</code> ) → <code>boolean</code></p><p>依據這個快照，給定的交易 ID 是否<em>可見</em>（也就是說，它是否在快照取得之前就已經完成）？請注意，對於子交易 ID（subxid），這個函式不會給出正確的答案；詳情請參閱<a href="https://docs.postgresql.tw/19/internals/transactions/subxacts.md">第 67.3 節</a>。</p>                                                                                                                                                                                                                                                                                                                                                                                              |
| <p><code>pg\_get\_multixact\_members</code> ( <em><code>multixid</code></em> <code>xid</code> ) → <code>setof record</code> ( <em><code>xid</code></em> <code>xid</code>, <em><code>mode</code></em> <code>text</code> )</p><p>回傳指定 multixact ID 之每個成員的交易 ID 與鎖模式。鎖模式 <code>forupd</code>、<code>fornokeyupd</code>、<code>sh</code> 與 <code>keysh</code> 分別對應於資料列層級鎖 <code>FOR UPDATE</code>、<code>FOR NO KEY UPDATE</code>、<code>FOR SHARE</code> 與 <code>FOR KEY SHARE</code>，如<a href="https://docs.postgresql.tw/19/the-sql-language/mvcc/explicit-locking/locking-rows.md#locking-rows">第 13.3.2 節</a>所述。另外還有兩種 multixact 專用的模式：<code>nokeyupd</code>，用於未修改鍵欄位的更新；以及 <code>upd</code>，用於會修改鍵欄位的更新或刪除。</p> |

<br>

內部的交易 ID 型別 `xid` 為 32 位元寬，每經過 40 億個交易就會迴繞（wrap around）。不過，[表 9.84](#functions-pg-snapshot) 所列的函式，除了 `age`、`mxid_age` 與 `pg_get_multixact_members` 之外，都使用 64 位元的型別 `xid8`，它在安裝環境的整個生命週期內都不會迴繞，並且在需要時可以透過型別轉換轉為 `xid`；詳情請參閱[第 67.1 節](https://docs.postgresql.tw/19/internals/transactions/transaction-id.md)。資料型別 `pg_snapshot` 儲存在某個特定時間點的交易 ID 可見性資訊。其組成部分說明於[表 9.85](#functions-pg-snapshot-parts)。`pg_snapshot` 的文字表示形式為 `xmin:xmax:xip_list`。例如 `10:20:10,14,15` 表示 `xmin=10, xmax=20, xip_list=10, 14, 15`。

### 表 9.85. 快照組成部分 [#](#functions-pg-snapshot-parts) <a href="#functions-pg-snapshot-parts" id="functions-pg-snapshot-parts"></a>

| 名稱         | 說明                                                                                                                        |
| ---------- | ------------------------------------------------------------------------------------------------------------------------- |
| `xmin`     | 仍在進行中的最小交易 ID。所有小於 `xmin` 的交易 ID，不是已提交且可見，就是已回滾且無效。                                                                       |
| `xmax`     | 比已完成之最大交易 ID 大一的值。所有大於或等於 `xmax` 的交易 ID，在取得快照時都尚未完成，因此是不可見的。                                                              |
| `xip_list` | 取得快照時正在進行中的交易。若某個交易 ID 符合 `xmin <=`` `*`X`*` ``< xmax` 且不在這個清單中，表示它在取得快照時已經完成，因此依其提交狀態，不是可見就是無效。這個清單不包含子交易的交易 ID（subxid）。 |

<br>

在 PostgreSQL 13 之前的版本中沒有 `xid8` 型別，因此提供了這些函式的變體，它們使用 `bigint` 來表示 64 位元的 XID，並相對應地使用另一個獨立的快照資料型別 `txid_snapshot`。這些較舊的函式名稱中帶有 `txid`。為了向下相容，它們仍然受到支援，但可能會在未來的版本中移除。請參閱[表 9.86](#functions-txid-snapshot)。

### 表 9.86. 已棄用的交易 ID 與快照資訊函式 [#](#functions-txid-snapshot) <a href="#functions-txid-snapshot" id="functions-txid-snapshot"></a>

| <p>函式</p><p>說明</p>                                                                                                                                                              |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><code>txid\_current</code> () → <code>bigint</code></p><p>See <code>pg\_current\_xact\_id()</code>.</p>                                                                      |
| <p><code>txid\_current\_if\_assigned</code> () → <code>bigint</code></p><p>See <code>pg\_current\_xact\_id\_if\_assigned()</code>.</p>                                          |
| <p><code>txid\_current\_snapshot</code> () → <code>txid\_snapshot</code></p><p>See <code>pg\_current\_snapshot()</code>.</p>                                                    |
| <p><code>txid\_snapshot\_xip</code> ( <code>txid\_snapshot</code> ) → <code>setof bigint</code></p><p>See <code>pg\_snapshot\_xip()</code>.</p>                                 |
| <p><code>txid\_snapshot\_xmax</code> ( <code>txid\_snapshot</code> ) → <code>bigint</code></p><p>See <code>pg\_snapshot\_xmax()</code>.</p>                                     |
| <p><code>txid\_snapshot\_xmin</code> ( <code>txid\_snapshot</code> ) → <code>bigint</code></p><p>See <code>pg\_snapshot\_xmin()</code>.</p>                                     |
| <p><code>txid\_visible\_in\_snapshot</code> ( <code>bigint</code>, <code>txid\_snapshot</code> ) → <code>boolean</code></p><p>See <code>pg\_visible\_in\_snapshot()</code>.</p> |
| <p><code>txid\_status</code> ( <code>bigint</code> ) → <code>text</code></p><p>See <code>pg\_xact\_status()</code>.</p>                                                         |

<br>

[返回 9.27. 系統資訊函式與運算子](https://docs.postgresql.tw/19/the-sql-language/functions/functions-info.md#functions-info)

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/functions-info.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-info/functions-info-snapshot.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.
