9.30. Event Trigger Functions#
Postgres Pro provides these helper functions to retrieve information from event triggers.
For more information about event triggers, seeChapter 38.
9.30.2. Processing Objects Dropped by a DDL Command#
pg_event_trigger_dropped_objects
() →setof record
pg_event_trigger_dropped_objects
returns a list of all objects dropped by the command in whosesql_drop
event it is called. If called in any other context, an error is raised. This function returns the following columns:
Name | Type | Description |
---|---|---|
classid | oid | OID of catalog the object belonged in |
objid | oid | OID of the object itself |
objsubid | integer | Sub-object ID (e.g., attribute number for a column) |
original | boolean | True if this was one of the root object(s) of the deletion |
normal | boolean | True if there was a normal dependency relationship in the dependency graph leading to this object |
is_temporary | boolean | True if this 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 identifier included in the identity is quoted if necessary. |
address_names | text[] | An array that, together withobject_type andaddress_args , can be used by thepg_get_object_address function to recreate the object address in a remote server containing an identically named object of the same kind. |
address_args | text[] | Complement foraddress_names |
Thepg_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.30.3. Handling a Table Rewrite Event#
The functions shown inTable 9.111 provide information about a table for which atable_rewrite
event has just been called. If called in any other context, an error is raised.
Table 9.111. Table Rewrite Information Functions
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();