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

# 9.30. 事件觸發程序函式 \#

### 9.30. 事件觸發程序函式 [#](#functions-event-triggers) <a href="#functions-event-triggers" id="functions-event-triggers"></a>

[9.30.1. 在命令結束時擷取變更](#pg-event-trigger-ddl-command-end-functions)

[9.30.2. 處理被 DDL 命令刪除的物件](#pg-event-trigger-sql-drop-functions)

[9.30.3. 處理資料表改寫事件](https://docs.postgresql.tw/19/the-sql-language/functions/functions-event-triggers/pg-event-trigger-table-rewrite-functions.md#pg-event-trigger-table-rewrite-functions)

PostgreSQL 提供了這些輔助函式，用來從事件觸發程序中取得資訊。

關於事件觸發程序的更多資訊，請參閱[第 38 章](https://docs.postgresql.tw/19/server-programming/event-triggers.md)。

#### 9.30.1. 在命令結束時擷取變更 [#](#pg-event-trigger-ddl-command-end-functions) <a href="#pg-event-trigger-ddl-command-end-functions" id="pg-event-trigger-ddl-command-end-functions"></a>

```

pg_event_trigger_ddl_commands () → setof record
```

在附加到 `ddl_command_end` 事件觸發程序的函式中呼叫時，`pg_event_trigger_ddl_commands` 會回傳每個使用者動作所執行之 DDL 命令的清單。如果在任何其他情境中呼叫，就會引發錯誤。`pg_event_trigger_ddl_commands` 會為每個執行的基本命令回傳一筆資料列；有些屬於單一 SQL 語句的命令可能會回傳不只一筆資料列。這個函式回傳下列欄位：

| 名稱                | 型別               | 說明                                              |
| ----------------- | ---------------- | ----------------------------------------------- |
| `classid`         | `oid`            | 物件所屬之系統目錄的 OID                                  |
| `objid`           | `oid`            | 物件本身的 OID                                       |
| `objsubid`        | `integer`        | 子物件 ID（例如欄位的屬性編號）                               |
| `command_tag`     | `text`           | 命令標籤                                            |
| `object_type`     | `text`           | 物件的類型                                           |
| `schema_name`     | `text`           | 物件所屬綱要的名稱（如果有的話）；否則為 `NULL`。不加任何引號。             |
| `object_identity` | `text`           | 物件識別的文字表示，以綱要限定。識別中所包含的每個識別符號，必要時都會加上引號。        |
| `in_extension`    | `boolean`        | 如果該命令是擴充功能指令碼的一部分，則為 true                       |
| `command`         | `pg_ddl_command` | 命令以內部格式表示的完整內容。它無法直接輸出，但可以傳給其他函式，以取得關於該命令的各種資訊。 |

#### 9.30.2. 處理被 DDL 命令刪除的物件 [#](#pg-event-trigger-sql-drop-functions) <a href="#pg-event-trigger-sql-drop-functions" id="pg-event-trigger-sql-drop-functions"></a>

```

pg_event_trigger_dropped_objects () → setof record
```

`pg_event_trigger_dropped_objects` 會回傳在其 `sql_drop` 事件中呼叫它的那個命令所刪除之所有物件的清單。如果在任何其他情境中呼叫，就會引發錯誤。這個函式回傳下列欄位：

| 名稱                | 型別        | 說明                                                                                             |
| ----------------- | --------- | ---------------------------------------------------------------------------------------------- |
| `classid`         | `oid`     | 物件原本所屬之系統目錄的 OID                                                                               |
| `objid`           | `oid`     | 物件本身的 OID                                                                                      |
| `objsubid`        | `integer` | 子物件 ID（例如欄位的屬性編號）                                                                              |
| `original`        | `boolean` | 如果這是刪除操作的根物件之一，則為 true                                                                         |
| `normal`          | `boolean` | 如果在相依關係圖中，有一般的相依關係通往這個物件，則為 true                                                               |
| `is_temporary`    | `boolean` | 如果這是暫時物件，則為 true                                                                               |
| `object_type`     | `text`    | 物件的類型                                                                                          |
| `schema_name`     | `text`    | 物件原本所屬綱要的名稱（如果有的話）；否則為 `NULL`。不加任何引號。                                                          |
| `object_name`     | `text`    | 如果綱要與名稱的組合可以用作該物件的唯一識別碼，則為物件的名稱；否則為 `NULL`。不加任何引號，而且名稱永遠不會以綱要限定。                               |
| `object_identity` | `text`    | 物件識別的文字表示，以綱要限定。識別中所包含的每個識別符號，必要時都會加上引號。                                                       |
| `address_names`   | `text[]`  | 一個陣列，可與 `object_type` 及 `address_args` 一起，讓 `pg_get_object_address` 函式在包含同類同名物件的遠端伺服器中重建該物件位址。 |
| `address_args`    | `text[]`  | `address_names` 的補充                                                                            |

`pg_event_trigger_dropped_objects` 函式可以像這樣在事件觸發程序中使用：

```

CREATE FUNCTION test_event_trigger_for_drops()
        RETURNS event_trigger LANGUAGE plpgsql AS $$
DECLARE
    obj record;
BEGIN
    FOR obj IN SELECT * FROM pg_event_trigger_dropped_objects()
    LOOP
        RAISE NOTICE '% dropped object: % %.% %',
                     tg_tag,
                     obj.object_type,
                     obj.schema_name,
                     obj.object_name,
                     obj.object_identity;
    END LOOP;
END;
$$;
CREATE EVENT TRIGGER test_event_trigger_for_drops
   ON sql_drop
   EXECUTE FUNCTION test_event_trigger_for_drops();
```

#### 9.30.3. 處理資料表改寫事件 [#](#pg-event-trigger-table-rewrite-functions) <a href="#pg-event-trigger-table-rewrite-functions" id="pg-event-trigger-table-rewrite-functions"></a>

完整內容請見 [9.30.3. 處理資料表改寫事件](https://docs.postgresql.tw/19/the-sql-language/functions/functions-event-triggers/pg-event-trigger-table-rewrite-functions.md#pg-event-trigger-table-rewrite-functions)。

***

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


---

# 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/the-sql-language/functions/functions-event-triggers.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.
