您的位置:首页 > 数据库

PostgreSQL中的事件触发器

2021-04-26 15:16 756 查看

作者:瀚高PG实验室(Highgo PG Lab) 丹心明月

注:本文章主要翻译自《PostgreSQL 13.0 Documentation》第三十九章

PostgreSQL还提供了事件触发器实现第38章探讨的触发器机制。与普通的触发器不同,事件触发器针对整个数据库,且可捕获DDL事件。


事件触发器可使用具有事件触发器支持的过程语言或C编写,但不可使用纯SQL编写。

39.1 事件触发器概览

事件触发器在相关事件发生时触发。当前,支持的事件有ddl_command_start,ddl_command_end,table_rewrite和sql_drop。


ddl_command_start事件在执行CREATE,ALTER,DROP,SECURITY LABEL,COMMENT,GRANT或REVOKE命令前发生。在事件触发器触发之前,不会检查所影响的对象是否存在。不过,针对共享对象(如数据库,角色和表空间)或对事件触发器本身的DDL命令不会触发该事件。事件触发器机制不支持此类对象类型。因为SELECT INTO命令等价于CREATE TABLE AS,故也会触发ddl_command_start事件。


ddl_comand_end事件在相同DDL命令之后触发。触发器在操作发生后(事务提交前)触发。


sql_drop在删除数据库对象时触发(在ddl_comand_end之前)。可使用pg_event_trigger_dropped_objects()列出删除的数据库对象。该触发器是在将对象从系统视图删除后执行的。


table_rewrite事件在表被ALTER TABLE和ALTER TYPE重写前触发。


事件触发器支持的命令列表,请参见第39.2节


事件触发器使用命令CREATE EVENT TRIGGER命令创建。创建事件触发器之前,需要先创建返回event_trigger类型的触发器函数。


触发器定义中,也可使用WHEN指定触发的特定命令。

39.2 事件触发器触发矩阵

表39.1列出了事件触发器支持的所有命令。

39.3 使用C编写事件触发器函数

本节介绍使用C编写事件触发器函数相关信息。


事件触发器函数必须使用“version  1”函数管理接口。


事件触发器管理者调用函数时,不会传递普通参数,而是一个指向EventTriggerData架构的上下文指针。C函数可使用如下宏检查是否由时间触发器管理者调用:

CALLED_AS_EVENT_TRIGGER(fcinfo)

扩展为:

((fcinfo)->context != NULL && IsA((fcinfo)->context, EventTriggerData))

如果返回true,则可将fcinfo->context转换为EventTriggerData *并使用EventTriggerData结构。函数不可对EventTriggerData结构或其指向的任何数据进行修改。


struct EventTriggerData在commands/event_trigger.h中定义:

typedef struct EventTriggerData{
       NodeTag type;       const char* event; /* event name */
       Node* parsetree; /* parse tree */
       CommandTag tag; /* command tag */} EventTriggerData;

参数解释:

type

    T_EventTriggerData。

event

    触发事件。

parsetree

    指向命令解析树的指针。

tag

    命令标签。


事件触发器仅可返回NULL指针。

39.4 事件触发器示例

此处为C编写的事件触发器函数示例。


函数noddl每次调用时均返回异常。触发事件为ddl_command_start。其作用为禁止运行所有的DDL命令。


以下为触发器函数的源码:

#include "postgres.h"#include "commands/event_trigger.h"PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(noddl);Datumnoddl(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", trigdata->tag)));
       PG_RETURN_NULL();
}

编译源码后,声明函数和触发器:

CREATE FUNCTION noddl() RETURNS event_triggerAS 'noddl' LANGUAGE C;CREATE EVENT TRIGGER noddl ON ddl_command_startEXECUTE 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;

39.5 表重写事件触发器示例

以下示例,使用table_event事件,实现仅在维护期间可进行表重写:

CREATE OR REPLACE FUNCTION no_rewrite()
RETURNS event_trigger
LANGUAGE plpgsql AS
$$
---
--- Implement local Table Rewriting policy:
--- public.foo is not allowed rewriting, ever
--- other tables are only allowed rewriting between 1am and 6am
--- unless they have more than 100 blocks
---
DECLARE
table_oid oid := pg_event_trigger_table_rewrite_oid();
current_hour integer := extract('hour' from current_time);
pages integer;
max_pages integer := 100;
BEGIN
IF pg_event_trigger_table_rewrite_oid() = 'public.foo'::regclass
THEN
RAISE EXCEPTION 'you''re not allowed to rewrite the table
%',
table_oid::regclass;
END IF;
SELECT INTO pages relpages FROM pg_class WHERE oid = table_oid;
IF pages > max_pages
THEN
RAISE EXCEPTION 'rewrites only allowed for table with less
than % pages',
max_pages;
END IF;
IF current_hour NOT BETWEEN 1 AND 6
THEN
RAISE EXCEPTION 'rewrites only allowed between 1am and
6am';
END IF;
END;
$$;
CREATE EVENT TRIGGER no_rewrite_allowed
ON table_rewrite
EXECUTE FUNCTION no_rewrite();


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: