CREATE_EVENT_TRIGGER(7pg16)
π 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 supportedfilter_variableisTAGπfilter_valueβ a list of values for the associatedfilter_variablethat should make the trigger fire. forTAG, 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 typeevent_trigger.
and here's the fine print, bestie: in the syntax ofCREATE EVENT TRIGGER, the keywordsFUNCTIONandPROCEDUREare equivalent β BUT the referenced function must in any case be a function, not a procedure. the keywordPROCEDUREhere 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 β
ALTER EVENT TRIGGER(ALTER_EVENT_TRIGGER(7))DROP EVENT TRIGGER(DROP_EVENT_TRIGGER(7))CREATE FUNCTION(CREATE_FUNCTION(7))
π¦ 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
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms