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

# 37.2. 資料變更的可見性 \#

### 37.2. 資料變更的可見性 [#](#trigger-datachanges) <a href="#trigger-datachanges" id="trigger-datachanges"></a>

如果你在觸發程序函式中執行 SQL 指令，而這些指令又存取了該觸發程序所屬的資料表，那麼你必須留意資料可見性規則，因為這些規則決定了這些 SQL 指令是否看得到那筆導致觸發程序被觸發的資料變更。簡要來說：

* 陳述式層級的觸發程序遵循簡單的可見性規則：陳述式所做的任何變更對陳述式層級的 `BEFORE` 觸發程序都不可見，而所有的修改對陳述式層級的 `AFTER` 觸發程序則都是可見的。
* 導致觸發程序被觸發的那筆資料變更（插入、更新或刪除）自然*不會*被資料列層級 `BEFORE` 觸發程序中所執行的 SQL 指令看見，因為它還沒有發生。
* 不過，在資料列層級 `BEFORE` 觸發程序中執行的 SQL 指令*確實會*看到同一個外層指令中先前已處理過的資料列所產生的資料變更效果。這一點需要謹慎，因為這些變更事件的順序一般而言是無法預測的；一個影響多筆資料列的 SQL 指令可以用任意順序走訪這些資料列。
* 同樣地，資料列層級的 `INSTEAD OF` 觸發程序會看到同一個外層指令中先前幾次觸發 `INSTEAD OF` 觸發程序所做的資料變更效果。
* 當資料列層級的 `AFTER` 觸發程序被觸發時，外層指令所做的所有資料變更都已經完成，而且對被呼叫的觸發程序函式都是可見的。

如果你的觸發程序函式是以任何一種標準的程序語言撰寫的，那麼上述說明只有在該函式被宣告為 `VOLATILE` 時才適用。被宣告為 `STABLE` 或 `IMMUTABLE` 的函式在任何情況下都不會看到呼叫端指令所做的變更。

關於資料可見性規則的進一步資訊，請參閱[第 45.5 節](https://docs.postgresql.tw/19/server-programming/spi/spi-visibility.md)。[第 37.4 節](https://docs.postgresql.tw/19/server-programming/triggers/trigger-example.md)中的範例示範了這些規則。

***

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