CREATE_EVENT_TRIGGER(7pg18)
๐ 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 supportedfilter_variableisTAG. that's it. that's the whole list. ๐filter_valueโ a list of values for the associatedfilter_variablethat you want the trigger to fire for. forTAG, 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 typeevent_trigger.
okay important side note: in the syntax ofCREATE EVENT TRIGGER, the keywordsFUNCTIONandPROCEDUREare equivalent, BUT the thing you reference has to be a function in any case, not a procedure. thePROCEDUREkeyword 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 withevent_triggersset tofalseto 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 โจ
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 ๐ this was PostgreSQL 18.6, dated 2026, page CREATE EVENT TRIGGER(7) โ go forth and trigger responsibly, mwah ๐โจ๐ฆ
๐ฆ where this page comes from
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms