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:

Name Type Description
classid Oid OID of catalog the object belongs in
objid Oid OID of the object in the catalog
objsubid integer Object sub-id (e.g. attribute number for columns)
command_tag text command tag
object_type text Type of the object
schema_name text Name of the schema the object belongs in, if any; otherwiseNULL. No quoting is applied.
object_identity text Text rendering of the object identity, schema-qualified. Each and every identifier present in the identity is quoted if necessary.
in_extension bool whether the command is part of an extension script
command pg_ddl_command A complete representation of the command, in internal format. This cannot be output directly, but it can be passed to other functions to obtain different pieces of information about the command.

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:

Name Type Description
classid Oid OID of catalog the object belonged in
objid Oid OID the object had within the catalog
objsubid int32 Object sub-id (e.g. attribute number for columns)
original bool Flag used to identify the root object(s) of the deletion
normal bool Flag indicating that there’s a normal dependency relationship in the dependency graph leading to this object
is_temporary bool Flag indicating that the object was a temporary object.
object_type text Type of the object
schema_name text Name of the schema the object belonged in, if any; otherwiseNULL. No quoting is applied.
object_name text Name of the object, if the combination of schema and name can be used as a unique identifier for the object; otherwiseNULL. No quoting is applied, and name is never schema-qualified.
object_identity text Text rendering of the object identity, schema-qualified. Each and every identifier present in the identity is quoted if necessary.
address_names text[] An array that, together withobject_typeandaddress_args, can be used by thepg_get_object_address()to recreate the object address in a remote server containing an identically named object of the same kind.
address_args text[] Complement foraddress_namesabove.

Thepg_event_trigger_dropped_objectsfunction can be used in an event trigger like this:

  1. CREATE FUNCTION test_event_trigger_for_drops()
  2. RETURNS event_trigger LANGUAGE plpgsql AS $$
  3. DECLARE
  4. obj record;
  5. BEGIN
  6. FOR obj IN SELECT * FROM pg_event_trigger_dropped_objects()
  7. LOOP
  8. RAISE NOTICE '% dropped object: % %.% %',
  9. tg_tag,
  10. obj.object_type,
  11. obj.schema_name,
  12. obj.object_name,
  13. obj.object_identity;
  14. END LOOP;
  15. END
  16. $$;
  17. CREATE EVENT TRIGGER test_event_trigger_for_drops
  18. ON sql_drop
  19. 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

Name Return Type Description
pg_event_trigger_table_rewrite_oid() Oid The OID of the table about to be rewritten.
pg_event_trigger_table_rewrite_reason() int The reason code(s) explaining the reason for rewriting. The exact meaning of the codes is release dependent.

Thepg_event_trigger_table_rewrite_oidfunction can be used in an event trigger like this:

  1. CREATE FUNCTION test_event_trigger_table_rewrite_oid()
  2. RETURNS event_trigger
  3. LANGUAGE plpgsql AS
  4. $$
  5. BEGIN
  6. RAISE NOTICE 'rewriting table % for reason %',
  7. pg_event_trigger_table_rewrite_oid()::regclass,
  8. pg_event_trigger_table_rewrite_reason();
  9. END;
  10. $$;
  11. CREATE EVENT TRIGGER test_table_rewrite_oid
  12. ON table_rewrite
  13. EXECUTE PROCEDURE test_event_trigger_table_rewrite_oid();