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

# 9.27.1. 工作階段資訊函式

## 9.27.1. 工作階段資訊函式 [#](#functions-info-session) <a href="#functions-info-session" id="functions-info-session"></a>

[表 9.71](#functions-info-session-table) 列出了幾個擷取工作階段與系統資訊的函式。

除了本節所列的函式之外，還有一些與統計系統相關的函式也會提供系統資訊。詳情請參閱[第 27.2.26 節](https://docs.postgresql.tw/19/server-administration/monitoring/monitoring-stats.md#monitoring-stats-functions)。

### 表 9.71. 工作階段資訊函式 [#](#functions-info-session-table) <a href="#functions-info-session-table" id="functions-info-session-table"></a>

| <p>函式</p><p>說明</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><code>current\_catalog</code> → <code>name</code></p><p><code>current\_database</code> () → <code>name</code></p><p>回傳目前資料庫的名稱。（在 SQL 標準中，資料庫稱為「catalog」，因此 <code>current\_catalog</code> 是標準的寫法。）</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| <p><code>current\_query</code> () → <code>text</code></p><p>回傳目前正在執行之查詢的文字，也就是用戶端所送出的內容（其中可能包含不只一個陳述句）。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| <p><code>current\_role</code> → <code>name</code></p><p>這等同於 <code>current\_user</code>。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| <p><code>current\_schema</code> → <code>name</code></p><p><code>current\_schema</code> () → <code>name</code></p><p>回傳搜尋路徑中第一個綱要的名稱（如果搜尋路徑為空，則回傳 NULL 值）。這個綱要就是在未指定目標綱要的情況下，建立任何資料表或其他具名物件時所使用的綱要。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| <p><code>current\_schemas</code> ( <em><code>include\_implicit</code></em> <code>boolean</code> ) → <code>name\[]</code></p><p>依優先順序回傳目前有效搜尋路徑中所有綱要名稱所組成的陣列。（目前 <a href="https://docs.postgresql.tw/19/server-administration/runtime-config/runtime-config-client.md#GUC-SEARCH-PATH">search\_path</a> 設定中，不對應於現存且可搜尋之綱要的項目會被省略。）如果布林引數為 <code>true</code>，則像 <code>pg\_catalog</code> 這類會被隱含搜尋的系統綱要也會包含在結果中。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| <p><code>current\_user</code> → <code>name</code></p><p>回傳目前執行情境的使用者名稱。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| <p><code>inet\_client\_addr</code> () → <code>inet</code></p><p>回傳目前用戶端的 IP 位址；如果目前的連線是透過 Unix-domain socket，則回傳 <code>NULL</code>。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| <p><code>inet\_client\_port</code> () → <code>integer</code></p><p>回傳目前用戶端的 IP 連接埠號碼；如果目前的連線是透過 Unix-domain socket，則回傳 <code>NULL</code>。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| <p><code>inet\_server\_addr</code> () → <code>inet</code></p><p>回傳伺服器接受目前連線時所使用的 IP 位址；如果目前的連線是透過 Unix-domain socket，則回傳 <code>NULL</code>。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| <p><code>inet\_server\_port</code> () → <code>integer</code></p><p>回傳伺服器接受目前連線時所使用的 IP 連接埠號碼；如果目前的連線是透過 Unix-domain socket，則回傳 <code>NULL</code>。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| <p><code>pg\_backend\_pid</code> () → <code>integer</code></p><p>回傳附屬於目前工作階段之伺服器程序的程序 ID。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| <p><code>pg\_blocking\_pids</code> ( <code>integer</code> ) → <code>integer\[]</code></p><p>回傳一個陣列，內容為阻擋指定程序 ID 之伺服器程序取得鎖的那些工作階段的程序 ID；如果沒有這樣的伺服器程序，或者它並未被阻擋，則回傳空陣列。</p><p>一個伺服器程序會阻擋另一個伺服器程序的情形有兩種：它持有與被阻擋程序的鎖請求相衝突的鎖（硬性阻擋），或者它正在等待一個會與被阻擋程序的鎖請求相衝突的鎖，且在等待佇列中排在被阻擋程序之前（軟性阻擋）。使用平行查詢時，即使實際的鎖是由子工作程序持有或等待，結果中列出的一律是用戶端可見的程序 ID（也就是 <code>pg\_backend\_pid</code> 的結果）。因此，結果中可能會有重複的 PID。另請注意，當一個已預備的交易持有相衝突的鎖時，會以程序 ID 零來表示。</p><p>頻繁呼叫這個函式可能會對資料庫效能造成一些影響，因為它需要短暫地獨占存取鎖管理器的共享狀態。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| <p><code>pg\_conf\_load\_time</code> () → <code>timestamp with time zone</code></p><p>回傳伺服器組態檔最後一次載入的時間。如果目前的工作階段在那時已經存在，這會是該工作階段本身重新讀取組態檔的時間（因此在不同的工作階段中，讀到的值會稍有不同）。否則，就是 postmaster 程序重新讀取組態檔的時間。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| <p><code>pg\_current\_logfile</code> ( \[ <code>text</code> ] ) → <code>text</code></p><p>回傳日誌收集器目前正在使用之日誌檔的路徑名稱。此路徑包含 <a href="https://docs.postgresql.tw/19/server-administration/runtime-config/runtime-config-logging.md#GUC-LOG-DIRECTORY">log\_directory</a> 目錄以及個別的日誌檔名稱。如果日誌收集器已停用，結果為 <code>NULL</code>。當存在多個各自採用不同格式的日誌檔時，不帶引數的 <code>pg\_current\_logfile</code> 會依照 <code>stderr</code>、<code>csvlog</code>、<code>jsonlog</code> 的順序，回傳第一個找到之格式的檔案路徑。如果沒有任何日誌檔採用這些格式，則回傳 <code>NULL</code>。若要取得特定日誌檔格式的資訊，請以 <code>csvlog</code>、<code>jsonlog</code> 或 <code>stderr</code> 作為選用參數的值。如果所要求的日誌格式未設定於 <a href="https://docs.postgresql.tw/19/server-administration/runtime-config/runtime-config-logging.md#GUC-LOG-DESTINATION">log\_destination</a> 中，結果為 <code>NULL</code>。結果反映的是 <code>current\_logfiles</code> 檔案的內容。</p><p>這個函式預設僅限超級使用者以及具有 <code>pg\_monitor</code> 角色權限的角色使用，但也可以授予其他使用者 EXECUTE 權限來執行此函式。</p> |
| <p><code>pg\_get\_loaded\_modules</code> () → <code>setof record</code> ( <em><code>module\_name</code></em> <code>text</code>, <em><code>version</code></em> <code>text</code>, <em><code>file\_name</code></em> <code>text</code> )</p><p>回傳已載入目前伺服器工作階段的可載入模組清單。<em><code>module\_name</code></em> 與 <em><code>version</code></em> 欄位的值為 NULL，除非模組作者已使用 <code>PG\_MODULE\_MAGIC\_EXT</code> 巨集為它們提供值。<em><code>file\_name</code></em> 欄位提供模組（共享函式庫）的檔案名稱。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| <p><code>pg\_my\_temp\_schema</code> () → <code>oid</code></p><p>回傳目前工作階段之暫存綱要的 OID；如果沒有暫存綱要（因為它尚未建立任何暫存資料表），則回傳零。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| <p><code>pg\_is\_other\_temp\_schema</code> ( <code>oid</code> ) → <code>boolean</code></p><p>如果給定的 OID 是其他工作階段之暫存綱要的 OID，就回傳 true。（這很有用，例如可用來在顯示系統目錄內容時排除其他工作階段的暫存資料表。）</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| <p><code>pg\_jit\_available</code> () → <code>boolean</code></p><p>如果有可用的 JIT 編譯器擴充功能（請參閱<a href="https://docs.postgresql.tw/19/server-administration/jit.md">第 30 章</a>），並且 <a href="https://docs.postgresql.tw/19/server-administration/runtime-config/runtime-config-query.md#GUC-JIT">jit</a> 組態參數設定為 <code>on</code>，就回傳 true。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| <p><code>pg\_numa\_available</code> () → <code>boolean</code></p><p>如果伺服器編譯時加入了 NUMA 支援，就回傳 true。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| <p><code>pg\_listening\_channels</code> () → <code>setof text</code></p><p>回傳目前工作階段正在監聽的非同步通知頻道名稱集合。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| <p><code>pg\_notification\_queue\_usage</code> () → <code>double precision</code></p><p>回傳非同步通知佇列目前被等待處理之通知所占用的比例（0–1），以佇列最大容量為基準。詳情請參閱 <a href="https://docs.postgresql.tw/19/reference/sql-commands/sql-listen.md">LISTEN</a> 與 <a href="https://docs.postgresql.tw/19/reference/sql-commands/sql-notify.md">NOTIFY</a>。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| <p><code>pg\_postmaster\_start\_time</code> () → <code>timestamp with time zone</code></p><p>回傳伺服器啟動的時間。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| <p><code>pg\_safe\_snapshot\_blocking\_pids</code> ( <code>integer</code> ) → <code>integer\[]</code></p><p>回傳一個陣列，內容為阻擋指定程序 ID 之伺服器程序取得安全快照的那些工作階段的程序 ID；如果沒有這樣的伺服器程序，或者它並未被阻擋，則回傳空陣列。</p><p>執行 <code>SERIALIZABLE</code> 交易的工作階段，會阻擋 <code>SERIALIZABLE READ ONLY DEFERRABLE</code> 交易取得快照，直到後者判定可以安全地避免取得任何述詞鎖為止。關於可序列化與可延後交易的更多資訊，請參閱<a href="https://docs.postgresql.tw/19/the-sql-language/mvcc/transaction-iso.md#xact-serializable">第 13.2.3 節</a>。</p><p>頻繁呼叫這個函式可能會對資料庫效能造成一些影響，因為它需要短暫地存取述詞鎖管理器的共享狀態。</p>                                                                                                                                                                                                                                                                                                                                                                                                              |
| <p><code>pg\_trigger\_depth</code> () → <code>integer</code></p><p>回傳 PostgreSQL 觸發程序目前的巢狀層級（如果不是直接或間接從觸發程序內部呼叫，則為 0）。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| <p><code>session\_user</code> → <code>name</code></p><p>回傳工作階段使用者的名稱。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| <p><code>system\_user</code> → <code>text</code></p><p>回傳使用者在被指派資料庫角色之前，於認證過程中所提出的認證方法與身分（如果有的話）。其表示形式為 <code>auth\_method:identity</code>；如果使用者未經認證（例如使用了 <a href="https://docs.postgresql.tw/19/server-administration/client-authentication/auth-trust.md">Trust 認證</a>），則為 <code>NULL</code>。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| <p><code>user</code> → <code>name</code></p><p>這等同於 <code>current\_user</code>。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |

<br>

### 注意

`current_catalog`、`current_role`、`current_schema`、`current_user`、`session_user` 與 `user` 在 SQL 中具有特殊的語法地位：呼叫它們時不能加上尾端的括號。在 PostgreSQL 中，`current_schema` 可以選擇性地加上括號，但其他幾個則不行。

`session_user` 通常是發起目前資料庫連線的使用者；但超級使用者可以使用 [SET SESSION AUTHORIZATION](https://docs.postgresql.tw/19/reference/sql-commands/sql-set-session-authorization.md) 變更這項設定。`current_user` 是用於權限檢查的使用者識別。通常它與工作階段使用者相同，但可以使用 [SET ROLE](https://docs.postgresql.tw/19/reference/sql-commands/sql-set-role.md) 加以變更。在執行具有 `SECURITY DEFINER` 屬性的函式期間，它也會改變。以 Unix 的說法，工作階段使用者是「真實使用者」（real user），而目前使用者是「有效使用者」（effective user）。`current_role` 與 `user` 是 `current_user` 的同義詞。（SQL 標準區分了 `current_role` 與 `current_user`，但 PostgreSQL 並不區分，因為它將使用者與角色統一為單一種類的實體。）

[返回 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-session.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.
