From 3a0e4d36ebd7f477822d5bae41ba121a40d22ccc Mon Sep 17 00:00:00 2001 From: Robert Haas Date: Fri, 20 Jul 2012 11:38:47 -0400 Subject: Make new event trigger facility actually do something. Commit 3855968f328918b6cd1401dd11d109d471a54d40 added syntax, pg_dump, psql support, and documentation, but the triggers didn't actually fire. With this commit, they now do. This is still a pretty basic facility overall because event triggers do not get a whole lot of information about what the user is trying to do unless you write them in C; and there's still no option to fire them anywhere except at the very beginning of the execution sequence, but it's better than nothing, and a good building block for future work. Along the way, add a regression test for ALTER LARGE OBJECT, since testing of event triggers reveals that we haven't got one. Dimitri Fontaine and Robert Haas --- doc/src/sgml/plpgsql.sgml | 69 +++++++++++++++++++++++++++++- doc/src/sgml/ref/create_event_trigger.sgml | 6 --- 2 files changed, 68 insertions(+), 7 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/plpgsql.sgml b/doc/src/sgml/plpgsql.sgml index 4840f6ea9c6..ab408456506 100644 --- a/doc/src/sgml/plpgsql.sgml +++ b/doc/src/sgml/plpgsql.sgml @@ -3377,7 +3377,10 @@ RAISE unique_violation USING MESSAGE = 'Duplicate user ID: ' || user_id; in PL/pgSQL - + + Triggers on data changes + + PL/pgSQL can be used to define trigger procedures. A trigger procedure is created with the CREATE FUNCTION command, declaring it as a function with @@ -3924,6 +3927,70 @@ UPDATE sales_fact SET units_sold = units_sold * 2; SELECT * FROM sales_summary_bytime; + + + + Triggers on events + + + PL/pgSQL can be used to define event + triggers. PostgreSQL requires that a procedure that + is to be called as an event trigger must be declared as a function with + no arguments and a return type of event_trigger. + + + + When a PL/pgSQL function is called as a + event trigger, several special variables are created automatically + in the top-level block. They are: + + + + TG_EVENT + + + Data type text; a string representing the event the + trigger is fired for. + + + + + + TG_TAG + + + Data type text; variable that contains the command tag + for which the trigger is fired. + + + + + + + + shows an example of a + event trigger procedure in PL/pgSQL. + + + + A <application>PL/pgSQL</application> Event Trigger Procedure + + + This example trigger simply raises a NOTICE message + each time a supported command is executed. + + + +CREATE OR REPLACE FUNCTION snitch() RETURNS event_trigger AS $$ +BEGIN + RAISE NOTICE 'snitch: % %', tg_event, tg_tag; +END; +$$ LANGUAGE plpgsql; + +CREATE EVENT TRIGGER snitch ON ddl_command_start EXECUTE PROCEDURE snitch(); + + + diff --git a/doc/src/sgml/ref/create_event_trigger.sgml b/doc/src/sgml/ref/create_event_trigger.sgml index 56c7b52a59c..08894b22cfb 100644 --- a/doc/src/sgml/ref/create_event_trigger.sgml +++ b/doc/src/sgml/ref/create_event_trigger.sgml @@ -98,12 +98,6 @@ CREATE EVENT TRIGGER name A user-supplied function that is declared as taking no argument and returning type event_trigger. - - If your event trigger is implemented in C then it - will be called with an argument, of - type internal, which is a pointer to - the Node * parse tree. - -- cgit v1.2.3