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

# 9.14. UUID 函式 \#

### 9.14. UUID 函式 [#](#functions-uuid) <a href="#functions-uuid" id="functions-uuid"></a>

[表 9.45](#functions-uuid-table) 列出了可用來產生 UUID 的 PostgreSQL 函式。

#### 表 9.45. UUID 產生函式 [#](#functions-uuid-table) <a href="#functions-uuid-table" id="functions-uuid-table"></a>

| <p>函式</p><p>說明</p><p>範例</p>                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><code>gen\_random\_uuid</code> ( ) → <code>uuid</code></p><p><code>uuidv4</code> ( ) → <code>uuid</code></p><p>產生第 4 版（隨機）UUID</p><p><code>gen\_random\_uuid()</code> → <code>5b30857f-0bfa-48b5-ac0b-5c64e28078d1</code></p><p><code>uuidv4()</code> → <code>b42410ee-132f-42ee-9e4f-09a6485c95b8</code></p>                                                                                                                                                     |
| <p><code>uuidv7</code> ( \[ <em><code>shift</code></em> <code>interval</code> ] ) → <code>uuid</code></p><p>產生第 7 版（依時間排序）UUID。時間戳記是使用毫秒精度的 UNIX 時間戳記 + 次毫秒時間戳記 + 隨機值計算而得。選用參數 <em><code>shift</code></em> 會將計算出的時間戳記位移給定的 <code>interval</code>。不接受無限的 interval 值。位移後的時間戳記必須落在 UUID 第 7 版之 48 位元毫秒時間戳記欄位所支援的範圍內：從 1970-01-01 00:00:00 UTC 到大約西元 10889 年。如果得到的時間戳記超出這個範圍，就會引發錯誤。</p><p><code>uuidv7()</code> → <code>019535d9-3df7-79fb-b466-fa907fa17f9e</code></p> |

<br>

#### 注意

[uuid-ossp](https://docs.postgresql.tw/19/appendixes/contrib/uuid-ossp.md) 模組提供了額外的函式，實作其他產生 UUID 的標準演算法。

[表 9.46](#func_uuid_extract_table) 列出了可用來從 UUID 擷取資訊的 PostgreSQL 函式。

#### 表 9.46. UUID 擷取函式 [#](#func_uuid_extract_table) <a href="#func_uuid_extract_table" id="func_uuid_extract_table"></a>

| <p>函式</p><p>說明</p><p>範例</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><code>uuid\_extract\_timestamp</code> ( <code>uuid</code> ) → <code>timestamp with time zone</code></p><p>從第 1 版或第 7 版的 UUID 中擷取 <code>timestamp with time zone</code>。對於其他版本，這個函式會回傳 null。請注意，擷取出的時間戳記不一定與產生該 UUID 的時間完全相同；這取決於產生該 UUID 的實作。</p><p><code>uuid\_extract\_timestamp('019535d9-3df7-79fb-b466-​fa907fa17f9e'::uuid)</code> → <code>2025-02-23 21:46:24.503-05</code></p>                                                                                                      |
| <p><code>uuid\_extract\_version</code> ( <code>uuid</code> ) → <code>smallint</code></p><p>從屬於 <a href="https://datatracker.ietf.org/doc/html/rfc9562">RFC 9562</a> 所描述之變體的 UUID 中擷取版本。對於其他變體，這個函式會回傳 null。例如，對於由 <code>gen\_random\_uuid()</code> 產生的 UUID，這個函式會回傳 4。</p><p><code>uuid\_extract\_version('41db1265-8bc1-4ab3-992f-​885799a4af1d'::uuid)</code> → <code>4</code></p><p><code>uuid\_extract\_version('019535d9-3df7-79fb-b466-​fa907fa17f9e'::uuid)</code> → <code>7</code></p> |

<br>

PostgreSQL 也為 UUID 提供了[表 9.1](https://docs.postgresql.tw/19/the-sql-language/functions/functions-comparison.md#functions-comparison-table) 所列的一般比較運算子。

關於 PostgreSQL 中 `uuid` 資料型別的細節，請參閱[第 8.12 節](https://docs.postgresql.tw/19/the-sql-language/datatype/datatype-uuid.md)。

***

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