CREATE_EVENT_TRIGGER(7pg17)

πŸ“¦ version 17.11, in opensuse-leap 16.0

okay bestie, welcome to the CREATE EVENT TRIGGER(7) page from the PostgreSQL 17.11 docs πŸ’– this one's giving "make a trigger that fires on events" and it's honestly iconic ✨

🌸 NAME 🌸

CREATE_EVENT_TRIGGER β€” defines a brand-new event trigger, periodt πŸ’…

πŸ¦‹ SYNOPSIS πŸ¦‹

CREATE EVENT TRIGGER name
    ON event
    [ WHEN filter_variable IN (filter_value [, ... ]) [ AND ... ] ]
    EXECUTE { FUNCTION | PROCEDURE } function_name()

✨ DESCRIPTION ✨

CREATE EVENT TRIGGER creates a new event trigger, no cap. Whenever the designated event happens, and the WHEN condition associated with the trigger β€” if there is one β€” is satisfied, the trigger function gets executed πŸ”₯

For a general introduction to event triggers, see Chapter 38. And listen: the user who creates an event trigger becomes its owner πŸ‘‘

πŸŽ€ PARAMETERS πŸŽ€

  • name β€” the name you're giving the new trigger. This name must be unique within the database, so no duplicate-name drama πŸ™ˆ
  • event β€” the name of the event that triggers a call to the given function. See Section 38.1 for more info on event names πŸ“–
  • filter_variable β€” the name of a variable used to filter events. This makes it possible to restrict the firing of the trigger to a subset of the cases in which it is supported. Currently the only supported filter_variable is TAG 🏷️
  • filter_value β€” a list of values for the associated filter_variable for which the trigger should fire. For TAG, this means a list of command tags (e.g., 'DROP FUNCTION') πŸ’¬
  • function_name β€” a user-supplied function that is declared as taking no argument and returning type event_trigger.

In the syntax of CREATE EVENT TRIGGER, the keywords FUNCTION and PROCEDURE are equivalent, but the referenced function must in any case be a function, not a procedure. The use of the keyword PROCEDURE here is historical and deprecated β€” so yeah, it's giving old-school, not main character energy 😭

πŸ’€ NOTES πŸ’€

Only superusers can create event triggers. That's the rule, bestie 🚨

Event triggers are disabled in single-user mode (see postgres(1)) as well as when event_triggers is set to false. If an erroneous event trigger disables the database so much that you can't even drop the trigger, restart with event_triggers set to false to temporarily disable event triggers, or in single-user mode, and you'll be able to do that 🫢

okay but fr fr, if your trigger nuked the vibe that hard, this is the emergency exit 😭 use it.

🌈 EXAMPLES 🌈

Forbid the execution of any DDL command:

CREATE OR REPLACE FUNCTION abort_any_command()
  RETURNS event_trigger
 LANGUAGE plpgsql
  AS $$
BEGIN
  RAISE EXCEPTION 'command % is disabled', tg_tag;
END;
$$;

CREATE EVENT TRIGGER abort_ddl ON ddl_command_start
   EXECUTE FUNCTION abort_any_command();

🧚 COMPATIBILITY 🧚

There is no CREATE EVENT TRIGGER statement in the SQL standard. It's simply not on the standard's radar, bestie πŸ’…

πŸ‘‘ SEE ALSO πŸ‘‘


byeee bestie πŸ’– this page is from PostgreSQL 17.11, dated 2026 β€” CREATE EVENT TRIGGER(7) out 🫢✨

πŸ“¦ where this page comes from