Skip to content

Event Trigger Functions

Event Trigger Functions

PL/Python can be used to define event triggers (see also Event Triggers). PostgreSQL requires that a function that is to be called as an event trigger must be declared as a function with no arguments and a return type of event_trigger.

When a function is used as an event trigger, the dictionary TD contains trigger-related values:

TD["event"] : The event the trigger was fired for, as a string, for example ddl_command_start.

TD["tag"] : The command tag for which the trigger was fired, as a string, for example DROP TABLE.

A PL/Python Event Trigger Function shows an example of an event trigger function in PL/Python.

Example: A PL/Python Event Trigger Function

This example trigger simply raises a NOTICE message each time a supported command is executed.

CREATE OR REPLACE FUNCTION pysnitch() RETURNS event_trigger
LANGUAGE plpython3u
AS $$
  plpy.notice("TD[event] => " + TD["event"] + " ; TD[tag] => " + TD["tag"]);
$$;

CREATE EVENT TRIGGER pysnitch ON ddl_command_start EXECUTE FUNCTION pysnitch();