November 19, 2026: PostgreSQL 19 Released!
Supported Versions: Current (19)
Development Versions: devel

44.6. Event Trigger Functions #

PL/Python can be used to define event triggers (see also Chapter 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 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.

Example 44.1 shows an example of an event trigger function in PL/Python.

Example 44.1. 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();

Submit correction

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.