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

# 38.3. 完整的事件觸發程序範例 \#

### 38.3. 完整的事件觸發程序範例 [#](#event-trigger-example) <a href="#event-trigger-example" id="event-trigger-example"></a>

以下是一個以 C 撰寫的非常簡單的事件觸發程序函式範例。（以程序語言撰寫的觸發程序範例，可以在各程序語言各自的文件中找到。）

函式 `noddl` 每次被呼叫時都會拋出一個例外。這個事件觸發程序的定義，把該函式與 `ddl_command_start` 事件關聯起來。其效果就是所有 DDL 命令（除了[第 38.1 節](https://docs.postgresql.tw/19/server-programming/event-triggers/event-trigger-definition.md)中提到的例外情況）都會被阻止執行。

以下是該觸發程序函式的原始碼：

```

#include "postgres.h"

#include "commands/event_trigger.h"
#include "fmgr.h"

PG_MODULE_MAGIC;

PG_FUNCTION_INFO_V1(noddl);

Datum
noddl(PG_FUNCTION_ARGS)
{
    EventTriggerData *trigdata;

    if (!CALLED_AS_EVENT_TRIGGER(fcinfo))  /* internal error */
        elog(ERROR, "not fired by event trigger manager");

    trigdata = (EventTriggerData *) fcinfo->context;

    ereport(ERROR,
            (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
             errmsg("command \"%s\" denied",
                    GetCommandTagName(trigdata->tag))));

    PG_RETURN_NULL();
}
```

編譯完原始碼之後（見[第 36.10.5 節](https://docs.postgresql.tw/19/server-programming/extend/xfunc-c.md#dfunc)），宣告函式與觸發程序：

```

CREATE FUNCTION noddl() RETURNS event_trigger
    AS 'noddl' LANGUAGE C;

CREATE EVENT TRIGGER noddl ON ddl_command_start
    EXECUTE FUNCTION noddl();
```

現在你可以測試這個觸發程序的運作：

```

=# \dy
                     List of event triggers
 Name  |       Event       | Owner | Enabled | Function | Tags
-------+-------------------+-------+---------+----------+------
 noddl | ddl_command_start | dim   | enabled | noddl    |
(1 row)

=# CREATE TABLE foo(id serial);
ERROR:  command "CREATE TABLE" denied
```

在這種情況下，如果你需要能夠執行某些 DDL 命令，就必須刪除該事件觸發程序，或是將它停用。你也可以只在某個交易期間停用該觸發程序，這樣會比較方便：

```

BEGIN;
ALTER EVENT TRIGGER noddl DISABLE;
CREATE TABLE foo (id serial);
ALTER EVENT TRIGGER noddl ENABLE;
COMMIT;
```

（回想一下，事件觸發程序本身的 DDL 命令，並不會受到事件觸發程序的影響。）

***

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


---

# 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/event-triggers/event-trigger-example.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.
