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

# 21.6. 函式安全性

### 21.6. 函式安全性 [#](#perm-functions) <a href="#perm-functions" id="perm-functions"></a>

函式、觸發器與資料列層級安全性原則，讓使用者可以將程式碼放入後端伺服器，而其他使用者可能在不知情的情況下執行它。因此，這些機制讓使用者相對容易以「特洛伊木馬」攻擊他人。最強的防護是嚴格控制誰能定義物件。若無法做到，撰寫查詢時就應只參照擁有者可信任的物件。請從 `search_path` 移除所有允許不受信任使用者建立物件的 schema。

函式在後端伺服器程序內執行，使用資料庫伺服器背景程式的作業系統權限。如果撰寫函式的程式語言允許未經檢查的記憶體存取，就可能變更伺服器的內部資料結構。因此，這類函式除了能進行許多其他操作之外，還能繞過任何系統存取控制。允許這種存取的函式語言被視為「不受信任」，PostgreSQL 只允許超級使用者建立以這些語言撰寫的函式。

***

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


---

# 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/server-administration/user-manag/perm-functions.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.
