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

# 44.5. 觸發程序函式 \#

### 44.5. 觸發程序函式 [#](#plpython-trigger) <a href="#plpython-trigger" id="plpython-trigger"></a>

當某個函式被用作觸發程序時，字典 `TD` 會包含與觸發程序相關的值：

`TD["event"]` : 以字串形式包含事件：`INSERT`、`UPDATE`、`DELETE` 或 `TRUNCATE`。

`TD["when"]` : 包含 `BEFORE`、`AFTER` 或 `INSTEAD OF` 其中之一。

`TD["level"]` : 包含 `ROW` 或 `STATEMENT`。

`TD["new"]`\
`TD["old"]` : 對於資料列層級的觸發程序，依觸發事件而定，這兩個欄位中會有一個或兩個包含對應的觸發資料列。

`TD["name"]` : 包含觸發程序的名稱。

`TD["table_name"]` : 包含觸發程序所發生之資料表的名稱。

`TD["table_schema"]` : 包含觸發程序所發生之資料表的綱要。

`TD["relid"]` : 包含觸發程序所發生之資料表的 OID。

`TD["args"]` : 如果 `CREATE TRIGGER` 指令中含有引數，可以在 `TD["args"][0]` 到 `TD["args"][n-1]` 取得它們。

如果 `TD["when"]` 是 `BEFORE` 或 `INSTEAD OF`，而且 `TD["level"]` 是 `ROW`，你可以從 Python 函式回傳 `None` 或 `"OK"` 表示該資料列未被修改，回傳 `"SKIP"` 以中止該事件；或者，若 `TD["event"]` 是 `INSERT` 或 `UPDATE`，你可以回傳 `"MODIFY"` 表示你已修改了新的資料列。除此之外的情況，回傳值都會被忽略。

***

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


---

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