PL/Python can be used to define event triggers (see alsoChapter 38).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 ofevent_trigger.
When a function is used as an event trigger, the dictionaryTD contains trigger-related values:
TD["event"]The event the trigger was fired for, as a string, for exampleddl_command_start.
TD["tag"]The command tag for which the trigger was fired, as a string, for exampleDROP TABLE.
Example 44.1 shows an example of an event trigger function inPL/Python.
Example 44.1. APL/Python Event Trigger Function
This example trigger simply raises aNOTICE message each time a supported command is executed.
CREATE OR REPLACE FUNCTION pysnitch() RETURNS event_triggerLANGUAGE plpython3uAS $$ plpy.notice("TD[event] => " + TD["event"] + " ; TD[tag] => " + TD["tag"]);$$;CREATE EVENT TRIGGER pysnitch ON ddl_command_start EXECUTE FUNCTION pysnitch();