9.28. 事件觸發函式

PostgreSQLprovides these helper functions to retrieve information from event triggers.

For more information about event triggers, seeChapter 39.

9.28.1. Capturing Changes at Command End

pg_event_trigger_ddl_commandsreturns a list ofDDLcommands executed by each user action, when invoked in a function attached to addl_command_endevent trigger. If called in any other context, an error is raised.pg_event_trigger_ddl_commandsreturns 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.28.2. Processing Objects Dropped by a DDL Command

pg_event_trigger_dropped_objectsreturns a list of all objects dropped by the command in whosesql_dropevent it is called. If called in any other context,pg_event_trigger_dropped_objectsraises an error.pg_event_trigger_dropped_objectsreturns the following columns:

Thepg_event_trigger_dropped_objectsfunction 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 PROCEDURE test_event_trigger_for_drops();

9.28.3. Handling a Table Rewrite Event

The functions shown inTable 9.90provide information about a table for which atable_rewriteevent has just been called. If called in any other context, an error is raised.

Table 9.90. Table Rewrite information

Thepg_event_trigger_table_rewrite_oidfunction 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 PROCEDURE test_event_trigger_table_rewrite_oid();

Last updated