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

# 第 39 章 規則系統

### 第 39 章 規則系統

**目錄**

* [39.1. 查詢樹](https://docs.postgresql.tw/19/server-programming/rules/querytree.md)
* [39.2. 檢視表與規則系統](https://docs.postgresql.tw/19/server-programming/rules/rules-views.md)
  * [39.2.1. `SELECT` 規則如何運作](https://docs.postgresql.tw/19/server-programming/rules/rules-views.md#rules-select)
  * [39.2.2. 非 `SELECT` 陳述式中的檢視表規則](https://docs.postgresql.tw/19/server-programming/rules/rules-views.md#rules-views-non-select)
  * [39.2.3. PostgreSQL 中檢視表的威力](https://docs.postgresql.tw/19/server-programming/rules/rules-views.md#rules-views-power)
  * [39.2.4. 更新檢視表](https://docs.postgresql.tw/19/server-programming/rules/rules-views.md#rules-views-update)
* [39.3. 具體化檢視表](https://docs.postgresql.tw/19/server-programming/rules/rules-materializedviews.md)
* [39.4. `INSERT`、`UPDATE` 與 `DELETE` 上的規則](https://docs.postgresql.tw/19/server-programming/rules/rules-update.md)
  * [39.4.1. 更新規則如何運作](https://docs.postgresql.tw/19/server-programming/rules/rules-update.md#rules-update-how)
  * [39.4.2. 與檢視表的搭配](https://docs.postgresql.tw/19/server-programming/rules/rules-update.md#rules-update-views)
* [39.5. 規則與權限](https://docs.postgresql.tw/19/server-programming/rules/rules-privileges.md)
* [39.6. 規則與指令狀態](https://docs.postgresql.tw/19/server-programming/rules/rules-status.md)
* [39.7. 規則與觸發程序的比較](https://docs.postgresql.tw/19/server-programming/rules/rules-triggers.md)

本章討論 PostgreSQL 中的規則系統。產生式規則系統（production rule system）在概念上很簡單，但實際使用時會牽涉到許多微妙之處。

某些其他資料庫系統定義了主動式資料庫規則，那些通常是預存程序與觸發程序。在 PostgreSQL 中，這些同樣可以用函式與觸發程序來實作。

規則系統（更精確地說，是查詢重寫規則系統）與預存程序及觸發程序完全不同。它會修改查詢以將規則納入考量，然後把修改後的查詢交給查詢規劃器進行規劃與執行。它非常強大，可以用在許多地方，例如查詢語言程序、檢視表與版本。這套規則系統的理論基礎與威力，在 [\[ston90b\]](https://docs.postgresql.tw/19/bibliography.md#STON90B) 與 [\[ong90\]](https://docs.postgresql.tw/19/bibliography.md#ONG90) 中也有討論。

***

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