CREATE_EVENT_TRIGGER(7pg13)

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

πŸ’… CREATE EVENT TRIGGER πŸ’…

okay so real quick β€” this page is CREATE EVENT TRIGGER(7) from the PostgreSQL 13.23 Documentation, and it's literally the glow-up guide for making things happen automatically when stuff goes down in your database. bestie, let's get into it πŸŽ€


✨ 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 CREATE EVENT TRIGGER creates a brand new event trigger, no cap. the way it works: whenever the designated event happens and the WHEN condition that's attached to the trigger (if there even is one) is satisfied, the trigger function gets executed. periodt. πŸ’–

if you want the whole general introduction to event triggers, that lives in Chapter 39 β€” go read it, iykyk.

and here's a cute little detail: the user who creates an event trigger becomes its owner. main character energy, honestly. 🌈

πŸ’œ PARAMETERS πŸ’œ

  • name β€” the name you're giving your new trigger. it has to be unique within the database, so no copy-paste names, bestie πŸ™ˆ
  • event β€” the name of the event that triggers a call to the given function. Section 39.1 has all the deets on event names ✨
  • filter_variable β€” the name of a variable that's used to filter events. this is how you restrict the trigger so it only fires for a subset of the cases where it's supported β€” lowkey so useful. currently the only supported filter_variable is TAG 🏷️
  • filter_value β€” a list of values for the associated filter_variable that you want the trigger to fire for. for TAG, that means a list of command tags (like 'DROP FUNCTION') πŸ’…
  • function_name β€” a user-supplied function that's declared as taking no argument and returning type event_trigger.

and here's the tea β˜• β€” in the syntax of CREATE EVENT TRIGGER, the keywords FUNCTION and PROCEDURE are equivalent, but the referenced function has to be a function in any case, not a procedure. the PROCEDURE keyword here is historical and highkey deprecated, so yeah, watch out for that 🫢

⚠️ fr fr: even though the syntax lets you say PROCEDURE, whatever you point at must actually be a function. don't get caught out.

πŸ”₯ NOTES πŸ”₯

  • ngl, only superusers can create event triggers. that's just how it is πŸ‘‘
  • event triggers are disabled in single-user mode (peep postgres(1)). and if some erroneous event trigger disables your database so hard that you can't even drop the trigger?? restart in single-user mode and you'll be able to do it. lifesaver, honestly πŸ˜­πŸ’–

πŸ“ EXAMPLES πŸ“

want to forbid the execution of any DDL command? iconic. here's how:

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

slay πŸ’… that's the whole vibe.

🌸 COMPATIBILITY 🌸

there is no CREATE EVENT TRIGGER statement in the SQL standard. we're built different πŸ¦‹

⭐ SEE ALSO ⭐


okay that's the whole page, bestie, you're officially caught up 🫢✨

this came from the PostgreSQL 13.23 Documentation, dated 2025, and that's the CREATE EVENT TRIGGER(7) page β€” go forth and trigger responsibly πŸ’œπŸ‘‘

πŸ“¦ where this page comes from