CREATE_EVENT_TRIGGER(7pg15)
π 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 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 (e.g.,'DROP FUNCTION').function_nameβ a user-supplied function, declared as taking no argument and returning typeevent_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 π
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 β 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
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-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