DocumentationPostgreSQL devel (2025-08-28 16:20:15 - git commit 16a9165ce4a)
Development Versions: devel
This documentation is for an unsupported version of PostgreSQL.
You may want to view the same page for the current version, or one of the other supported versions listed above instead.

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();