CREATE_EVENT_TRIGGER(7pg13)
π 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 supportedfilter_variableisTAGπ·οΈfilter_valueβ a list of values for the associatedfilter_variablethat you want the trigger to fire for. forTAG, 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 typeevent_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 β
ALTER EVENT TRIGGER(ALTER_EVENT_TRIGGER(7))DROP EVENT TRIGGER(DROP_EVENT_TRIGGER(7))CREATE FUNCTION(CREATE_FUNCTION(7))
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
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-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