1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-28 23:42:10 +03:00

Make new event trigger facility actually do something.

Commit 3855968f32 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
This commit is contained in:
Robert Haas
2012-07-20 11:38:47 -04:00
parent be86e3dd5b
commit 3a0e4d36eb
28 changed files with 1087 additions and 197 deletions

View File

@ -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>

View File

@ -98,12 +98,6 @@ CREATE EVENT TRIGGER <replaceable class="PARAMETER">name</replaceable>
A user-supplied function that is declared as taking no argument and
returning type <literal>event_trigger</literal>.
</para>
<para>
If your event trigger is implemented in <literal>C</literal> then it
will be called with an argument, of
type <literal>internal</literal>, which is a pointer to
the <literal>Node *</literal> parse tree.
</para>
</listitem>
</varlistentry>