Postgresql 中文操作指南
40.3. Writing Event Trigger Functions in C #
本节描述了事件触发器函数的接口中底层详细信息。只有在用 C 编写事件触发器函数时才需要这些信息。如果您使用更高级别的语言,那么这些细节将为您处理。在大多数情况下,您应考虑在用 C 编写事件触发器之前使用过程语言。每种过程语言的文档都说明了如何使用该语言编写事件触发器。
This section describes the low-level details of the interface to an event trigger function. This information is only needed when writing event trigger functions in C. If you are using a higher-level language then these details are handled for you. In most cases you should consider using a procedural language before writing your event triggers in C. The documentation of each procedural language explains how to write an event trigger in that language.
事件触发器函数必须使用“版本 1”函数管理器接口。
Event trigger functions must use the “version 1” function manager interface.
当函数被事件触发器管理器调用时,不会向它传递任何普通参数,但是会向它传递一个指向 EventTriggerData 结构的“上下文”指针。C 函数可以通过执行宏来检查函数是否是由事件触发器管理器调用的:
When a function is called by the event trigger manager, it is not passed any normal arguments, but it is passed a “context” pointer pointing to a EventTriggerData structure. C functions can check whether they were called from the event trigger manager or not by executing the macro:
CALLED_AS_EVENT_TRIGGER(fcinfo)
它将扩展为:
which expands to:
((fcinfo)->context != NULL && IsA((fcinfo)->context, EventTriggerData))
如果返回 true,那么可以安全地将 fcinfo→context 转换成 EventTriggerData * 类型并利用指向 EventTriggerData 结构。函数必须 not 更改 EventTriggerData 结构或其指向的任何数据。
If this returns true, then it is safe to cast fcinfo→context to type EventTriggerData * and make use of the pointed-to EventTriggerData structure. The function must not alter the EventTriggerData structure or any of the data it points to.
struct EventTriggerData 在 commands/event_trigger.h 中定义:
struct EventTriggerData is defined in commands/event_trigger.h:
typedef struct EventTriggerData
{
NodeTag type;
const char *event; /* event name */
Node *parsetree; /* parse tree */
CommandTag tag; /* command tag */
} EventTriggerData;
其中,成员定义如下:
where the members are defined as follows:
-
type
-
Always T_EventTriggerData.
-
-
event
-
Describes the event for which the function is called, one of "ddl_command_start", "ddl_command_end", "sql_drop", "table_rewrite". See Section 40.1 for the meaning of these events.
-
-
parsetree
-
A pointer to the parse tree of the command. Check the PostgreSQL source code for details. The parse tree structure is subject to change without notice.
-
-
tag
-
The command tag associated with the event for which the event trigger is run, for example "CREATE FUNCTION".
-
事件触发器函数必须返回一个 NULL 指针 (not 一个 SQL NULL 值,即不设置 isNull 为 true)。
An event trigger function must return a NULL pointer (not an SQL null value, that is, do not set isNull true).