Postgresql 中文操作指南

9.29. Event Trigger Functions #

PostgreSQL 提供了这些帮助程序函数,以从事件触发器中检索信息。

PostgreSQL provides these helper functions to retrieve information from event triggers.

请参阅 Chapter 40 了解更多有关事件触发器的信息。

For more information about event triggers, see Chapter 40.

9.29.1. Capturing Changes at Command End #

pg_event_trigger_ddl_commands () → setof record

pg_event_trigger_ddl_commands 调用附加到 ddl_command_end 事件触发器的函数时,返回每个用户操作执行的 DDL 命令的列表。如果在任何其他上下文中调用,则会引发错误。pg_event_trigger_ddl_commands 为执行的每条基本命令返回一行;一些是一条 SQL 语句的命令可能返回多行。此函数返回以下列:

pg_event_trigger_ddl_commands returns a list of DDL commands executed by each user action, when invoked in a function attached to a ddl_command_end event trigger. If called in any other context, an error is raised. pg_event_trigger_ddl_commands returns one row for each base command executed; some commands that are a single SQL sentence may return more than one row. This function returns the following columns:

9.29.2. Processing Objects Dropped by a DDL Command #

pg_event_trigger_dropped_objects () → setof record

pg_event_trigger_dropped_objects 返回在 sql_drop 事件中调用的命令中删除的所有对象的列表。如果在任何其他上下文中调用,则会引发错误。此函数返回以下列:

pg_event_trigger_dropped_objects returns a list of all objects dropped by the command in whose sql_drop event it is called. If called in any other context, an error is raised. This function returns the following columns:

pg_event_trigger_dropped_objects 函数可以像这样在事件触发器中使用:

The pg_event_trigger_dropped_objects function can be used in an event trigger like this:

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.29.3. Handling a Table Rewrite Event #

Table 9.104 中显示的函数提供一个 table_rewrite 事件刚刚被调用的表的信息。如果在任何其他上下文中进行调用,则会引发错误。

The functions shown in Table 9.104 provide information about a table for which a table_rewrite event has just been called. If called in any other context, an error is raised.

Table 9.104. Table Rewrite Information Functions

Function

Description

pg_event_trigger_table_rewrite_oid () → oid

Returns the OID of the table about to be rewritten.

pg_event_trigger_table_rewrite_reason () → integer

Returns a code explaining the reason(s) for rewriting. The exact meaning of the codes is release dependent.

这些函数可以像这样在事件触发器中使用:

These functions can be used in an event trigger like this:

CREATE FUNCTION test_event_trigger_table_rewrite_oid()
 RETURNS event_trigger
 LANGUAGE plpgsql AS
$$
BEGIN
  RAISE NOTICE 'rewriting table % for reason %',
                pg_event_trigger_table_rewrite_oid()::regclass,
                pg_event_trigger_table_rewrite_reason();
END;
$$;

CREATE EVENT TRIGGER test_table_rewrite_oid
                  ON table_rewrite
   EXECUTE FUNCTION test_event_trigger_table_rewrite_oid();