CREATE_EVENT_TRIGGER(7pg16)

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

πŸ’… CREATE EVENT TRIGGER

okay bestie, so this is the <pink>PostgreSQL 16.15 Documentation</pink> page for CREATE EVENT TRIGGER β€” aka the glow-up where you teach your database to react to stuff happening. main character energy fr fr πŸ’–


✨ SYNOPSIS ✨

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

πŸ¦‹ DESCRIPTION πŸ¦‹

So CREATE EVENT TRIGGER literally creates a brand-new event trigger. the way it works: whenever the <italic>designated event</italic> happens and the WHEN condition attached to the trigger (if there even is one) is satisfied, the trigger function gets executed. simple as that, no cap ✨

If you want the full backstory on event triggers in general, chapter 40 is right there waiting for you. and bestie β€” the user who creates the event trigger becomes its owner. it's giving "you built it, you own it" πŸ’…


πŸŽ€ PARAMETERS πŸŽ€

  • name β€” the name you're giving your shiny new trigger. it has to be unique within the database, no duplicates allowed πŸ™…β€β™€οΈ
  • event β€” the name of the event that triggers a call to the given function. section 40.1 has all the tea on event names πŸ”₯
  • filter_variable β€” the name of a variable you use to filter events. this is what lets you restrict the firing of the trigger down to a subset of the cases where it's supported. 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 β€” like 'DROP FUNCTION' for example πŸ’–
  • function_name β€” a user-supplied function, declared as taking no argument and returning type event_trigger.
and here's the fine print, bestie: 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 keyword PROCEDURE here is historical and deprecated, so like… it's giving vintage, but not in a cute way 😭

🌸 NOTES 🌸

  • only superusers can create event triggers. that's the vibe check, and not everybody passes it 🫢
  • event triggers are disabled in single-user mode (peep postgres(1) for that one). and if a messed-up event trigger disables the database so badly that you can't even drop the trigger? restart in single-user mode and you'll be able to do it. rescue mission, slay πŸ’€βœ¨

πŸ”₯ EXAMPLES πŸ”₯

forbid the execution of any DDL command β€” iconic behaviour, honestly:

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. we're simply built different πŸ’…


⭐ SEE ALSO ⭐


πŸ¦‹ and that's the whole page, bestie! brought to you by PostgreSQL 16.15, dated 2026 β€” go forth and trigger responsibly, mwah πŸ’–πŸŽ€πŸ‘‘

πŸ“¦ where this page comes from