CREATE_EVENT_TRIGGER(7pg18)

๐Ÿ“ฆ version 18.6, in opensuse-leap 16.0

๐Ÿ’– CREATE EVENT TRIGGER(7) ๐Ÿ’–

okay bestie, so we're deep in the PostgreSQL 18.6 Documentation and this page is literally about CREATE EVENT TRIGGER โ€” define a new event trigger. yes, the big-girl version of triggers. pull up a chair ๐ŸŽ€


โœจ NAME โœจ

CREATE_EVENT_TRIGGER โ€” define a new event trigger ๐Ÿ‘‘

โœจ SYNOPSIS โœจ

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

โœจ DESCRIPTION โœจ

so basically, CREATE EVENT TRIGGER makes a brand new event trigger. whenever the event you picked happens, and the WHEN condition attached to the trigger (if there even is one) is satisfied, the trigger function runs. simple as that. ๐Ÿ’…

for the full general intro to event triggers, go look at Chapter 38.

and the glow-up detail: whoever creates an event trigger becomes its owner. main character energy fr fr ๐Ÿซถ

โœจ PARAMETERS โœจ

  • name โ€” the name you're giving your new trigger. it has to be unique within the database, no duplicates, we don't do that here. ๐Ÿšซ
  • event โ€” the name of the event that fires a call to the function. head to Section 38.1 for the deets on event names. ๐Ÿ“–
  • filter_variable โ€” the name of a variable used to filter events. this is the part that lets you restrict the trigger so it only fires for a subset of the cases where it's supported. currently the only supported filter_variable is TAG. that's it. that's the whole list. ๐Ÿ’€
  • filter_value โ€” a list of values for the associated filter_variable that you want the trigger to fire for. for TAG, this means a list of command tags (like, for example, 'DROP FUNCTION'). ๐Ÿท๏ธ
  • function_name โ€” a user-supplied function that's declared as taking no argument and returning type event_trigger.
okay important side note: in the syntax of CREATE EVENT TRIGGER, the keywords FUNCTION and PROCEDURE are equivalent, BUT the thing you reference has to be a function in any case, not a procedure. the PROCEDURE keyword here is historical and deprecated. so likeโ€ฆ don't, bestie. ๐Ÿ™ˆ

โœจ NOTES โœจ

only superusers can create event triggers. that's the vibe check. ๐Ÿ‘‘

event triggers are disabled in single-user mode (see postgres(1)), and also disabled whenever event_triggers is set to false.

and listen, if some erroneous event trigger breaks your database SO badly that you can't even drop the trigger? ๐Ÿ˜ญ restart with event_triggers set to false to temporarily disable event triggers, or go in via single-user mode โ€” and then you'll be able to drop it. you're welcome. ๐Ÿซถ

โœจ EXAMPLES โœจ

want to forbid the execution of any DDL command? slay, here you go:

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. not a thing. we're built different. ๐ŸŒˆ

โœจ SEE ALSO โœจ


and that's the whole page, bestie ๐Ÿ’œ this was PostgreSQL 18.6, dated 2026, page CREATE EVENT TRIGGER(7) โ€” go forth and trigger responsibly, mwah ๐ŸŽ€โœจ๐Ÿฆ‹

๐Ÿ“ฆ where this page comes from