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

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

### 45.5. 資料變更的可見性 [#](#spi-visibility) <a href="#spi-visibility" id="spi-visibility"></a>

在使用 SPI 的函式（或任何其他 C 函式）中，資料變更的可見性由以下規則決定：

* 在一個 SQL 指令的執行期間，該指令本身所做的任何資料變更對該指令來說都是不可見的。例如在：

  ```

  INSERT INTO a SELECT * FROM a;
  ```

  當中，被插入的資料列對 `SELECT` 的部分是不可見的。
* 指令 C 所做的變更，對所有在 C 之後開始的指令都是可見的，不論那些指令是在 C 內部（於 C 執行期間）啟動，還是在 C 完成之後才啟動。
* 在被某個 SQL 指令所呼叫的函式（可能是一般函式或觸發程序）內部透過 SPI 執行的指令，會依照傳給 SPI 的讀寫旗標而遵循上述兩條規則的其中一條。以唯讀模式執行的指令遵循第一條規則：它們看不到呼叫端指令所做的變更。以讀寫模式執行的指令遵循第二條規則：它們可以看到目前為止所做的所有變更。
* 所有標準的程序語言都會依照函式的變動性（volatility）屬性來設定 SPI 的讀寫模式。`STABLE` 與 `IMMUTABLE` 函式中的指令以唯讀模式執行，而 `VOLATILE` 函式中的指令則以讀寫模式執行。雖然 C 函式的作者有辦法違反這項慣例，但這麼做不太可能是個好主意。

下一節包含一個範例，說明這些規則的應用方式。

***

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