CREATE_EVENT_TRIGGER(7pg14)

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

🌸 NAME 🌸

CREATE_EVENT_TRIGGER β€” define a new event trigger. πŸ’–

okay bestie, this is the CREATE_EVENT_TRIGGER(7pg14) page from the PostgreSQL 14.24 documentation. It’s literally here to help you define a new event trigger β€” and yes, it’s giving very much β€œautomate the drama” energy. ✨

✨ 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. Whenever the designated event occurs and the WHEN condition associated with the trigger β€” if any β€” is satisfied, the trigger function will be executed. fr fr, that’s the whole point. πŸ”₯

For a general introduction to event triggers, see Chapter 40. The user who creates an event trigger becomes its owner. main character ownership moment ✨

πŸŽ€ PARAMETERS πŸŽ€

  • name β€” the name you’re giving the new trigger. It must be unique within the database. no duplicate names, bestie πŸ’…
  • event β€” the name of the event that triggers a call to the given function. See Section 40.1 for more information 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. iykyk ⭐
  • 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. 🫢

πŸ¦‹ NOTES πŸ¦‹

  • Only superusers can create event triggers. no cap. πŸ‘‘
  • Event triggers are disabled in single-user mode (see postgres(1)). If an erroneous event trigger disables the database so much that you can't even drop the trigger, restart in single-user mode and you'll be able to do that. rescue-mode arc πŸ’œ

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

iconic. periodt. πŸ’…βœ¨

⭐ COMPATIBILITY ⭐

There is no CREATE EVENT TRIGGER statement in the SQL standard. the SQL standard is literally not giving this one πŸ’€

πŸ’œ SEE ALSO πŸ’œ


xoxo, your PostgreSQL 14.24 docs bestie, dated 2026 β€” now go slay those triggers πŸ’…βœ¨

πŸ“¦ where this page comes from