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

# 第 44 章 PL/Python — Python 程序語言

### 第 44 章 PL/Python — Python 程序語言

**目錄**

* [44.1. PL/Python 函式](https://docs.postgresql.tw/19/server-programming/plpython/plpython-funcs.md)
* [44.2. 資料值](https://docs.postgresql.tw/19/server-programming/plpython/plpython-data.md)
  * [44.2.1. 資料型別對應](https://docs.postgresql.tw/19/server-programming/plpython/plpython-data.md#plpython-data-type-mapping)
  * [44.2.2. Null、None](https://docs.postgresql.tw/19/server-programming/plpython/plpython-data.md#plpython-data-null)
  * [44.2.3. 陣列、清單](https://docs.postgresql.tw/19/server-programming/plpython/plpython-data.md#plpython-arrays)
  * [44.2.4. 複合型別](https://docs.postgresql.tw/19/server-programming/plpython/plpython-data.md#plpython-data-composite-types)
  * [44.2.5. 回傳集合的函式](https://docs.postgresql.tw/19/server-programming/plpython/plpython-data.md#plpython-data-set-returning-funcs)
* [44.3. 共享資料](https://docs.postgresql.tw/19/server-programming/plpython/plpython-sharing.md)
* [44.4. 匿名程式碼區塊](https://docs.postgresql.tw/19/server-programming/plpython/plpython-do.md)
* [44.5. 觸發程序函式](https://docs.postgresql.tw/19/server-programming/plpython/plpython-trigger.md)
* [44.6. 存取資料庫](https://docs.postgresql.tw/19/server-programming/plpython/plpython-database.md)
  * [44.6.1. 資料庫存取函式](https://docs.postgresql.tw/19/server-programming/plpython/plpython-database.md#plpython-database-access-funcs)
  * [44.6.2. 攔截錯誤](https://docs.postgresql.tw/19/server-programming/plpython/plpython-database.md#plpython-trapping)
* [44.7. 明確子交易](https://docs.postgresql.tw/19/server-programming/plpython/plpython-subtransaction.md)
  * [44.7.1. 子交易情境管理器](https://docs.postgresql.tw/19/server-programming/plpython/plpython-subtransaction.md#plpython-subtransaction-context-managers)
* [44.8. 交易管理](https://docs.postgresql.tw/19/server-programming/plpython/plpython-transactions.md)
* [44.9. 公用函式](https://docs.postgresql.tw/19/server-programming/plpython/plpython-util.md)
* [44.10. Python 2 與 Python 3 的差異](https://docs.postgresql.tw/19/server-programming/plpython/plpython-python23.md)
* [44.11. 環境變數](https://docs.postgresql.tw/19/server-programming/plpython/plpython-envar.md)

PL/Python 程序語言讓 PostgreSQL 函式與程序可以用 [Python 語言](https://www.python.org)撰寫。

若要在特定資料庫中安裝 PL/Python，請使用 `CREATE EXTENSION plpython3u`。

#### 提示

如果某個語言已安裝到 `template1` 中，那麼之後所有新建立的資料庫都會自動安裝該語言。

PL/Python 只以「不受信任」語言的形式提供，也就是說它並沒有提供任何限制使用者可以在其中做什麼的方式，因此才會命名為 `plpython3u`。如果未來 Python 開發出安全的執行機制，或許就會推出受信任的變體 `plpython`。撰寫不受信任 PL/Python 函式的人，必須注意該函式不能被用來做任何不想要的事，因為它能做到任何以資料庫管理員身分登入的使用者所能做的事。只有超級使用者才能建立像 `plpython3u` 這類不受信任語言的函式。

#### 注意

使用原始碼套件的使用者，必須在安裝過程中特別啟用 PL/Python 的建置（更多資訊請參閱安裝說明）。使用二進位套件的使用者，可能會發現 PL/Python 位於獨立的子套件中。

***

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


---

# 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-programming/plpython.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.
