diff options
author | Robert Haas <rhaas@postgresql.org> | 2012-07-20 11:38:47 -0400 |
---|---|---|
committer | Robert Haas <rhaas@postgresql.org> | 2012-07-20 11:39:01 -0400 |
commit | 3a0e4d36ebd7f477822d5bae41ba121a40d22ccc (patch) | |
tree | 323cd89ebb88c51b796b86f17f6efa1db76a761e /doc/src/sgml/plpgsql.sgml | |
parent | be86e3dd5b42c33387ae976c014e6276c9439f7f (diff) | |
download | postgresql-3a0e4d36ebd7f477822d5bae41ba121a40d22ccc.tar.gz |
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
Diffstat (limited to 'doc/src/sgml/plpgsql.sgml')
-rw-r--r-- | doc/src/sgml/plpgsql.sgml | 69 |
1 files changed, 68 insertions, 1 deletions
diff --git a/doc/src/sgml/plpgsql.sgml b/doc/src/sgml/plpgsql.sgml index 4840f6ea9c..ab40845650 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; <secondary>in PL/pgSQL</secondary> </indexterm> - <para> + <sect2 id="plpgsql-dml-trigger"> + <title>Triggers on data changes</title> + + <para> <application>PL/pgSQL</application> can be used to define trigger procedures. A trigger procedure is created with the <command>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; </programlisting> </example> +</sect2> + + <sect2 id="plpgsql-event-trigger"> + <title>Triggers on events</title> + + <para> + <application>PL/pgSQL</application> can be used to define event + triggers. <productname>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 <literal>event_trigger</>. + </para> + + <para> + When a <application>PL/pgSQL</application> function is called as a + event trigger, several special variables are created automatically + in the top-level block. They are: + + <variablelist> + <varlistentry> + <term><varname>TG_EVENT</varname></term> + <listitem> + <para> + Data type <type>text</type>; a string representing the event the + trigger is fired for. + </para> + </listitem> + </varlistentry> + + <varlistentry> + <term><varname>TG_TAG</varname></term> + <listitem> + <para> + Data type <type>text</type>; variable that contains the command tag + for which the trigger is fired. + </para> + </listitem> + </varlistentry> + </variablelist> + </para> + + <para> + <xref linkend="plpgsql-event-trigger-example"> shows an example of a + event trigger procedure in <application>PL/pgSQL</application>. + </para> + + <example id="plpgsql-event-trigger-example"> + <title>A <application>PL/pgSQL</application> Event Trigger Procedure</title> + + <para> + This example trigger simply raises a <literal>NOTICE</literal> message + each time a supported command is executed. + </para> + +<programlisting> +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(); +</programlisting> + </example> + </sect2> </sect1> |