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

# 9.28.9. 通用檔案存取函式

## 9.28.9. 通用檔案存取函式 [#](#functions-admin-genfile) <a href="#functions-admin-genfile" id="functions-admin-genfile"></a>

[表 9.108](#functions-admin-genfile-table) 所列的函式提供對伺服器所在主機上檔案的原生存取。除非使用者是超級使用者或被授予 `pg_read_server_files` 角色，否則只能存取資料庫叢集目錄與 `log_directory` 之內的檔案。對於叢集目錄中的檔案，請使用相對路徑；對於日誌檔案，請使用與 `log_directory` 組態設定相符的路徑。

請注意，授予使用者對 `pg_read_file()` 或相關函式的 EXECUTE 權限，會讓他們能夠讀取伺服器上任何資料庫伺服器程序可讀取的檔案；這些函式會略過所有資料庫內的權限檢查。這表示，舉例來說，具有這種存取權的使用者能夠讀取存放認證資訊的 `pg_authid` 資料表內容，也能讀取資料庫中的任何資料表資料。因此，授予這些函式的存取權時應審慎考慮。

授予這些函式的權限時，請注意，資料表中顯示選用參數的項目，大多是實作為數個具有不同參數列表的實體函式。如果要使用這類函式，就必須個別對每個函式授予權限。psql 的 `\df` 指令可以用來檢查實際的函式簽章為何。

其中部分函式接受選用的 *`missing_ok`* 參數，用來指定檔案或目錄不存在時的行為。如果為 `true`，函式會視情況回傳 `NULL` 或空的結果集合。如果為 `false`，則會引發錯誤。（除了「找不到檔案」之外的失敗情況，在任何情況下都會以錯誤回報。）預設值為 `false`。

### 表 9.108. 通用檔案存取函式 [#](#functions-admin-genfile-table) <a href="#functions-admin-genfile-table" id="functions-admin-genfile-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>pg_ls_dir</code> ( <em><code>dirname</code></em> <code>text</code> [, <em><code>missing_ok</code></em> <code>boolean</code>, <em><code>include_dot_dirs</code></em> <code>boolean</code> ] ) → <code>setof text</code></p><p>回傳指定目錄中所有檔案（以及目錄與其他特殊檔案）的名稱。<em><code>include_dot_dirs</code></em> 參數表示是否要在結果集合中包含「.」與「..」；預設是排除它們。當 <em><code>missing_ok</code></em> 為 <code>true</code> 時，包含它們可以用來區分空目錄與不存在的目錄。</p><p>這個函式預設只限超級使用者使用，但可以將 EXECUTE 權限授予其他使用者來執行此函式。</p></td></tr><tr><td><p><code>pg_ls_logdir</code> () → <code>setof record</code> ( <em><code>name</code></em> <code>text</code>, <em><code>size</code></em> <code>bigint</code>, <em><code>modification</code></em> <code>timestamp with time zone</code> )</p><p>回傳伺服器日誌目錄中每個一般檔案的名稱、大小與最後修改時間（mtime）。以點開頭的檔名、目錄以及其他特殊檔案會被排除。</p><p>這個函式預設只限超級使用者與具有 <code>pg_monitor</code> 角色權限的角色使用，但可以將 EXECUTE 權限授予其他使用者來執行此函式。</p></td></tr><tr><td><p><code>pg_ls_waldir</code> () → <code>setof record</code> ( <em><code>name</code></em> <code>text</code>, <em><code>size</code></em> <code>bigint</code>, <em><code>modification</code></em> <code>timestamp with time zone</code> )</p><p>回傳伺服器預寫式日誌（WAL）目錄中每個一般檔案的名稱、大小與最後修改時間（mtime）。以點開頭的檔名、目錄以及其他特殊檔案會被排除。</p><p>這個函式預設只限超級使用者與具有 <code>pg_monitor</code> 角色權限的角色使用，但可以將 EXECUTE 權限授予其他使用者來執行此函式。</p></td></tr><tr><td><p><code>pg_ls_logicalmapdir</code> () → <code>setof record</code> ( <em><code>name</code></em> <code>text</code>, <em><code>size</code></em> <code>bigint</code>, <em><code>modification</code></em> <code>timestamp with time zone</code> )</p><p>回傳伺服器 <code>pg_logical/mappings</code> 目錄中每個一般檔案的名稱、大小與最後修改時間（mtime）。以點開頭的檔名、目錄以及其他特殊檔案會被排除。</p><p>這個函式預設只限超級使用者與 <code>pg_monitor</code> 角色的成員使用，但可以將 EXECUTE 權限授予其他使用者來執行此函式。</p></td></tr><tr><td><p><code>pg_ls_logicalsnapdir</code> () → <code>setof record</code> ( <em><code>name</code></em> <code>text</code>, <em><code>size</code></em> <code>bigint</code>, <em><code>modification</code></em> <code>timestamp with time zone</code> )</p><p>回傳伺服器 <code>pg_logical/snapshots</code> 目錄中每個一般檔案的名稱、大小與最後修改時間（mtime）。以點開頭的檔名、目錄以及其他特殊檔案會被排除。</p><p>這個函式預設只限超級使用者與 <code>pg_monitor</code> 角色的成員使用，但可以將 EXECUTE 權限授予其他使用者來執行此函式。</p></td></tr><tr><td><p><code>pg_ls_replslotdir</code> ( <em><code>slot_name</code></em> <code>text</code> ) → <code>setof record</code> ( <em><code>name</code></em> <code>text</code>, <em><code>size</code></em> <code>bigint</code>, <em><code>modification</code></em> <code>timestamp with time zone</code> )</p><p>回傳伺服器 <code>pg_replslot/slot_name</code> 目錄中每個一般檔案的名稱、大小與最後修改時間（mtime），其中 <em><code>slot_name</code></em> 是作為函式輸入所提供的複寫槽名稱。以點開頭的檔名、目錄以及其他特殊檔案會被排除。</p><p>這個函式預設只限超級使用者與 <code>pg_monitor</code> 角色的成員使用，但可以將 EXECUTE 權限授予其他使用者來執行此函式。</p></td></tr><tr><td><p><code>pg_ls_summariesdir</code> () → <code>setof record</code> ( <em><code>name</code></em> <code>text</code>, <em><code>size</code></em> <code>bigint</code>, <em><code>modification</code></em> <code>timestamp with time zone</code> )</p><p>回傳伺服器 WAL 摘要目錄（<code>pg_wal/summaries</code>）中每個一般檔案的名稱、大小與最後修改時間（mtime）。以點開頭的檔名、目錄以及其他特殊檔案會被排除。</p><p>這個函式預設只限超級使用者與 <code>pg_monitor</code> 角色的成員使用，但可以將 EXECUTE 權限授予其他使用者來執行此函式。</p></td></tr><tr><td><p><code>pg_ls_archive_statusdir</code> () → <code>setof record</code> ( <em><code>name</code></em> <code>text</code>, <em><code>size</code></em> <code>bigint</code>, <em><code>modification</code></em> <code>timestamp with time zone</code> )</p><p>回傳伺服器 WAL 封存狀態目錄（<code>pg_wal/archive_status</code>）中每個一般檔案的名稱、大小與最後修改時間（mtime）。以點開頭的檔名、目錄以及其他特殊檔案會被排除。</p><p>這個函式預設只限超級使用者與 <code>pg_monitor</code> 角色的成員使用，但可以將 EXECUTE 權限授予其他使用者來執行此函式。</p></td></tr><tr><td><p><code>pg_ls_tmpdir</code> ( [ <em><code>tablespace</code></em> <code>oid</code> ] ) → <code>setof record</code> ( <em><code>name</code></em> <code>text</code>, <em><code>size</code></em> <code>bigint</code>, <em><code>modification</code></em> <code>timestamp with time zone</code> )</p><p>回傳指定 <em><code>tablespace</code></em> 之暫存檔案目錄中每個一般檔案的名稱、大小與最後修改時間（mtime）。如果未提供 <em><code>tablespace</code></em>，就會檢查 <code>pg_default</code> 資料表空間。以點開頭的檔名、目錄以及其他特殊檔案會被排除。</p><p>這個函式預設只限超級使用者與 <code>pg_monitor</code> 角色的成員使用，但可以將 EXECUTE 權限授予其他使用者來執行此函式。</p></td></tr><tr><td><p><code>pg_read_file</code> ( <em><code>filename</code></em> <code>text</code> [, <em><code>offset</code></em> <code>bigint</code>, <em><code>length</code></em> <code>bigint</code> ] [, <em><code>missing_ok</code></em> <code>boolean</code> ] ) → <code>text</code></p><p>回傳文字檔案的全部或一部分，從指定的位元組 <em><code>offset</code></em> 開始，最多回傳 <em><code>length</code></em> 個位元組（如果先到達檔案結尾則會較少）。如果 <em><code>offset</code></em> 為負值，則是相對於檔案結尾的位置。如果省略 <em><code>offset</code></em> 與 <em><code>length</code></em>，就會回傳整個檔案。從檔案讀取的位元組會以資料庫的編碼解讀為字串；如果它們在該編碼中無效，就會拋出錯誤。</p><p>這個函式預設只限超級使用者使用，但可以將 EXECUTE 權限授予其他使用者來執行此函式。</p></td></tr><tr><td><p><code>pg_read_binary_file</code> ( <em><code>filename</code></em> <code>text</code> [, <em><code>offset</code></em> <code>bigint</code>, <em><code>length</code></em> <code>bigint</code> ] [, <em><code>missing_ok</code></em> <code>boolean</code> ] ) → <code>bytea</code></p><p>回傳檔案的全部或一部分。這個函式與 <code>pg_read_file</code> 相同，差別在於它可以讀取任意的二進位資料，並將結果以 <code>bytea</code> 而非 <code>text</code> 回傳；因此不會進行任何編碼檢查。</p><p>這個函式預設只限超級使用者使用，但可以將 EXECUTE 權限授予其他使用者來執行此函式。</p><p>搭配 <code>convert_from</code> 函式，這個函式可以用來讀取以指定編碼儲存的文字檔案，並將其轉換為資料庫的編碼：</p><pre><code>SELECT convert_from(pg_read_binary_file('file_in_utf8.txt'), 'UTF8');
</code></pre></td></tr><tr><td><p><code>pg_stat_file</code> ( <em><code>filename</code></em> <code>text</code> [, <em><code>missing_ok</code></em> <code>boolean</code> ] ) → <code>record</code> ( <em><code>size</code></em> <code>bigint</code>, <em><code>access</code></em> <code>timestamp with time zone</code>, <em><code>modification</code></em> <code>timestamp with time zone</code>, <em><code>change</code></em> <code>timestamp with time zone</code>, <em><code>creation</code></em> <code>timestamp with time zone</code>, <em><code>isdir</code></em> <code>boolean</code> )</p><p>回傳一筆紀錄，其中包含檔案的大小、最後存取時間戳記、最後修改時間戳記、最後檔案狀態變更時間戳記（僅限 Unix 平台）、檔案建立時間戳記（僅限 Windows），以及表示它是否為目錄的旗標。</p><p>這個函式預設只限超級使用者使用，但可以將 EXECUTE 權限授予其他使用者來執行此函式。</p></td></tr></tbody></table>

<br>

[返回 9.28. 系統管理函式](https://docs.postgresql.tw/19/the-sql-language/functions/functions-admin.md#functions-admin)

***

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