Sophie

Sophie

distrib > Mandriva > 10.0 > i586 > by-pkgid > db7d48fed1469a51f3fb965d5b5b2ac1 > files > 545

postgresql-docs-7.4.1-2.5.100mdk.i586.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Writing Trigger Functions in C</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 7.4.1 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="Triggers"
HREF="triggers.html"><LINK
REL="PREVIOUS"
TITLE="Visibility of Data Changes"
HREF="trigger-datachanges.html"><LINK
REL="NEXT"
TITLE="A Complete Example"
HREF="trigger-example.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
NAME="creation"
CONTENT="2003-12-22T03:48:47"></HEAD
><BODY
CLASS="SECT1"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
>PostgreSQL 7.4.1 Documentation</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="trigger-datachanges.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="triggers.html"
>Fast Backward</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
>Chapter 35. Triggers</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="top"
><A
HREF="triggers.html"
>Fast Forward</A
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="top"
><A
HREF="trigger-example.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="TRIGGER-INTERFACE"
>35.3. Writing Trigger Functions in C</A
></H1
><A
NAME="AEN31070"
></A
><P
>    This section describes the low-level details of the interface to a
    trigger function.  This information is only needed when writing a
    trigger function in C.  If you are using a higher-level
    language then these details are handled for you.  The documentation
    of each procedural language explains how to write a trigger in that
    language.
   </P
><P
>    Trigger functions must use the <SPAN
CLASS="QUOTE"
>"version 1"</SPAN
> function manager
    interface.
   </P
><P
>    When a function is called by the trigger manager, it is not passed
    any normal arguments, but it is passed a <SPAN
CLASS="QUOTE"
>"context"</SPAN
>
    pointer pointing to a <TT
CLASS="STRUCTNAME"
>TriggerData</TT
> structure.  C
    functions can check whether they were called from the trigger
    manager or not by executing the macro
</P><PRE
CLASS="PROGRAMLISTING"
>CALLED_AS_TRIGGER(fcinfo)</PRE
><P>
    which expands to
</P><PRE
CLASS="PROGRAMLISTING"
>((fcinfo)-&#62;context != NULL &#38;&#38; IsA((fcinfo)-&#62;context, TriggerData))</PRE
><P>
    If this returns true, then it is safe to cast
    <TT
CLASS="LITERAL"
>fcinfo-&#62;context</TT
> to type <TT
CLASS="LITERAL"
>TriggerData
    *</TT
> and make use of the pointed-to
    <TT
CLASS="STRUCTNAME"
>TriggerData</TT
> structure.  The function must
    <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>not</I
></SPAN
> alter the <TT
CLASS="STRUCTNAME"
>TriggerData</TT
>
    structure or any of the data it points to.
   </P
><P
>    <TT
CLASS="STRUCTNAME"
>struct TriggerData</TT
> is defined in
    <TT
CLASS="FILENAME"
>commands/trigger.h</TT
>:

</P><PRE
CLASS="PROGRAMLISTING"
>typedef struct TriggerData
{
    NodeTag       type;
    TriggerEvent  tg_event;
    Relation      tg_relation;
    HeapTuple     tg_trigtuple;
    HeapTuple     tg_newtuple;
    Trigger      *tg_trigger;
} TriggerData;</PRE
><P>

    where the members are defined as follows:

    <P
></P
></P><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="STRUCTFIELD"
>type</TT
></DT
><DD
><P
>        Always <TT
CLASS="LITERAL"
>T_TriggerData</TT
>.
       </P
></DD
><DT
><TT
CLASS="STRUCTFIELD"
>tg_event</TT
></DT
><DD
><P
>	Describes the event for which the function is called. You may use the
	following macros to examine <TT
CLASS="LITERAL"
>tg_event</TT
>:

	<P
></P
></P><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="LITERAL"
>TRIGGER_FIRED_BEFORE(tg_event)</TT
></DT
><DD
><P
>	    Returns true if the trigger fired before the operation.
	   </P
></DD
><DT
><TT
CLASS="LITERAL"
>TRIGGER_FIRED_AFTER(tg_event)</TT
></DT
><DD
><P
>	    Returns true if the trigger fired after the operation.
	   </P
></DD
><DT
><TT
CLASS="LITERAL"
>TRIGGER_FIRED_FOR_ROW(tg_event)</TT
></DT
><DD
><P
>	    Returns true if the trigger fired for a row-level event.
	   </P
></DD
><DT
><TT
CLASS="LITERAL"
>TRIGGER_FIRED_FOR_STATEMENT(tg_event)</TT
></DT
><DD
><P
>	    Returns true if the trigger fired for a statement-level event.
	   </P
></DD
><DT
><TT
CLASS="LITERAL"
>TRIGGER_FIRED_BY_INSERT(tg_event)</TT
></DT
><DD
><P
>	    Returns true if the trigger was fired by an <TT
CLASS="COMMAND"
>INSERT</TT
> command.
	   </P
></DD
><DT
><TT
CLASS="LITERAL"
>TRIGGER_FIRED_BY_UPDATE(tg_event)</TT
></DT
><DD
><P
>	    Returns true if the trigger was fired by an <TT
CLASS="COMMAND"
>UPDATE</TT
> command.
	   </P
></DD
><DT
><TT
CLASS="LITERAL"
>TRIGGER_FIRED_BY_DELETE(tg_event)</TT
></DT
><DD
><P
>	    Returns true if the trigger was fired by a <TT
CLASS="COMMAND"
>DELETE</TT
> command.
	   </P
></DD
></DL
></DIV
><P>
       </P
></DD
><DT
><TT
CLASS="STRUCTFIELD"
>tg_relation</TT
></DT
><DD
><P
>	A pointer to a structure describing the relation that the trigger fired for.
	Look at <TT
CLASS="FILENAME"
>utils/rel.h</TT
> for details about
	this structure.  The most interesting things are
	<TT
CLASS="LITERAL"
>tg_relation-&#62;rd_att</TT
> (descriptor of the relation
	tuples) and <TT
CLASS="LITERAL"
>tg_relation-&#62;rd_rel-&#62;relname</TT
>
	(relation name; the type is not <TT
CLASS="TYPE"
>char*</TT
> but
	<TT
CLASS="TYPE"
>NameData</TT
>; use
	<TT
CLASS="LITERAL"
>SPI_getrelname(tg_relation)</TT
> to get a <TT
CLASS="TYPE"
>char*</TT
> if you
	need a copy of the name).
       </P
></DD
><DT
><TT
CLASS="STRUCTFIELD"
>tg_trigtuple</TT
></DT
><DD
><P
>	A pointer to the row for which the trigger was fired. This is
	the row being inserted, updated, or deleted.  If this trigger
	was fired for an <TT
CLASS="COMMAND"
>INSERT</TT
> or
	<TT
CLASS="COMMAND"
>DELETE</TT
> then this is what you should return
	to from the function if you don't want to replace the row with
	a different one (in the case of <TT
CLASS="COMMAND"
>INSERT</TT
>) or
	skip the operation.
       </P
></DD
><DT
><TT
CLASS="STRUCTFIELD"
>tg_newtuple</TT
></DT
><DD
><P
>	A pointer to the new version of the row, if the trigger was
	fired for an <TT
CLASS="COMMAND"
>UPDATE</TT
>, and <TT
CLASS="SYMBOL"
>NULL</TT
> if
	it is for an <TT
CLASS="COMMAND"
>INSERT</TT
> or a
	<TT
CLASS="COMMAND"
>DELETE</TT
>. This is what you have to return
	from the function if the event is an <TT
CLASS="COMMAND"
>UPDATE</TT
>
	and you don't want to replace this row by a different one or
	skip the operation.
       </P
></DD
><DT
><TT
CLASS="STRUCTFIELD"
>tg_trigger</TT
></DT
><DD
><P
>	A pointer to a structure of type <TT
CLASS="STRUCTNAME"
>Trigger</TT
>,
	defined in <TT
CLASS="FILENAME"
>utils/rel.h</TT
>:

</P><PRE
CLASS="PROGRAMLISTING"
>typedef struct Trigger
{
    Oid         tgoid;
    char       *tgname;
    Oid         tgfoid;
    int16       tgtype;
    bool        tgenabled;
    bool        tgisconstraint;
    Oid         tgconstrrelid;
    bool        tgdeferrable;
    bool        tginitdeferred;
    int16       tgnargs;
    int16       tgattr[FUNC_MAX_ARGS];
    char      **tgargs;
} Trigger;</PRE
><P>

       where <TT
CLASS="STRUCTFIELD"
>tgname</TT
> is the trigger's name,
       <TT
CLASS="STRUCTFIELD"
>tgnargs</TT
> is number of arguments in
       <TT
CLASS="STRUCTFIELD"
>tgargs</TT
>, and <TT
CLASS="STRUCTFIELD"
>tgargs</TT
> is an array of
       pointers to the arguments specified in the <TT
CLASS="COMMAND"
>CREATE
       TRIGGER</TT
> statement. The other members are for internal use
       only.
       </P
></DD
></DL
></DIV
><P>
   </P
><P
>    A trigger function must return either <TT
CLASS="SYMBOL"
>NULL</TT
> or a
    <TT
CLASS="STRUCTNAME"
>HeapTuple</TT
> pointer.  Be careful to return either
    <TT
CLASS="STRUCTFIELD"
>tg_trigtuple</TT
> or <TT
CLASS="STRUCTFIELD"
>tg_newtuple</TT
>,
    as appropriate, if you don't want to modify the row being operated on.
   </P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="trigger-datachanges.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="trigger-example.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Visibility of Data Changes</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="triggers.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>A Complete Example</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>