CREATE_EVENT_TRIGGER(7pg15)

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

πŸ’… CREATE EVENT_TRIGGER β€” define a new event trigger

Ok so bestie, welcome to CREATE_EVENT_TRIGGER from the PostgreSQL 15.19 Documentation (manual page CREATE EVENT_TRIGGER(7), section 7pg15 β€” you're in the right place, iykyk 🫢). This is the glow-up where you teach your database to react to stuff happening. Main character energy fr.


✨ SYNOPSIS ✨

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

🌸 DESCRIPTION 🌸

CREATE EVENT TRIGGER makes a brand new event trigger. The way it works: whenever the designated event happens, and the WHEN condition attached to the trigger (if you even gave it one, no pressure) is satisfied, the trigger function gets executed. Slay. πŸ’…

For the full general introduction to event triggers, go read Chapter 40 β€” that's the lore.

Also important: the user who creates an event trigger becomes its owner. Like, congrats, you're the parent now. πŸ‘‘


πŸŽ€ PARAMETERS πŸŽ€

  • name β€” the name you're giving your shiny new trigger. This name must be unique within the database (no duplicates, no "twin flame" situations, genuinely has to be one of a kind ✨).
  • event β€” the name of the event that triggers a call to the given function. Section 40.1 has the full guest list of event names if you need it.
  • filter_variable β€” the name of a variable you use to filter events. This is what lets you restrict the trigger so it only fires for a subset of the cases where it's supported (very picky, very demure). Currently the only supported filter_variable is TAG.
  • filter_value β€” a list of values for the associated filter_variable that should make the trigger fire. For TAG, that means a list of command tags (e.g., 'DROP FUNCTION').
  • function_name β€” a user-supplied function, declared as taking no argument and returning type event_trigger.

Real talk about the syntax though: in 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 PROCEDURE keyword here is purely historical and deprecated β€” so like, it works, but it's giving vintage in a bad way. πŸ™ˆ


⭐ NOTES ⭐

Only superusers can create event triggers. No exceptions, no loopholes. πŸ’€

Event triggers are disabled in single-user mode (see postgres(1)). And here's the rescue plan if things go sideways: 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. Crisis averted, bestie. 🧚


πŸ”₯ EXAMPLES πŸ”₯

Want to forbid the execution of any DDL command? Say less. This function + trigger combo does exactly that:

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

Look at her. Iconic. 🌈


πŸ¦‹ COMPATIBILITY πŸ¦‹

There is no CREATE EVENT TRIGGER statement in the SQL standard. We're simply built different. πŸ’…


πŸ’œ SEE ALSO πŸ’œ


And that's the whole page, bestie β€” go forth and trigger responsibly. This masterpiece is brought to you by PostgreSQL 15.19, dated 2026, and this very manual page is CREATE EVENT_TRIGGER(7) β€” the <rainbow>PostgreSQL 15.19 Documentat</rainbow>... ok fine, the PostgreSQL 15.19 Documentation, 2026. Mwah. πŸ˜˜πŸŽ€

πŸ“¦ where this page comes from