Sophie

Sophie

distrib > Mandriva > 2008.1 > x86_64 > media > main-testing > by-pkgid > bab02a23fa9f3df8d66a9a3231b50245 > files > 855

postgresql8.3-docs-8.3.6-2mdv2008.1.x86_64.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>Overview of Trigger Behavior</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 8.3.6 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="Triggers"
HREF="triggers.html"><LINK
REL="PREVIOUS"
TITLE="Triggers"
HREF="triggers.html"><LINK
REL="NEXT"
TITLE="Visibility of Data Changes"
HREF="trigger-datachanges.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2009-02-03T04:34:16"></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 8.3.6 Documentation</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="triggers.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-datachanges.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="TRIGGER-DEFINITION"
>35.1. Overview of Trigger Behavior</A
></H1
><P
>    A trigger is a specification that the database should automatically
    execute a particular function whenever a certain type of operation is
    performed.  Triggers can be defined to execute either before or after any
    <TT
CLASS="COMMAND"
>INSERT</TT
>, <TT
CLASS="COMMAND"
>UPDATE</TT
>, or
    <TT
CLASS="COMMAND"
>DELETE</TT
> operation, either once per modified row,
    or once per <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> statement.
    If a trigger event occurs, the trigger's function is called
    at the appropriate time to handle the event.
   </P
><P
>    The trigger function must be defined before the trigger itself can be
    created.  The trigger function must be declared as a 
    function taking no arguments and returning type <TT
CLASS="LITERAL"
>trigger</TT
>.
    (The trigger function receives its input through a specially-passed
    <TT
CLASS="STRUCTNAME"
>TriggerData</TT
> structure, not in the form of ordinary function
    arguments.)
   </P
><P
>    Once a suitable trigger function has been created, the trigger is
    established with
    <A
HREF="sql-createtrigger.html"
><I
>CREATE TRIGGER</I
></A
>.
    The same trigger function can be used for multiple triggers.
   </P
><P
>    <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> offers both <I
CLASS="FIRSTTERM"
>per-row</I
>
    triggers and <I
CLASS="FIRSTTERM"
>per-statement</I
> triggers.  With a per-row
    trigger, the trigger function
    is invoked once for each row that is affected by the statement
    that fired the trigger. In contrast, a per-statement trigger is
    invoked only once when an appropriate statement is executed,
    regardless of the number of rows affected by that statement. In
    particular, a statement that affects zero rows will still result
    in the execution of any applicable per-statement triggers. These
    two types of triggers are sometimes called <I
CLASS="FIRSTTERM"
>row-level</I
>
    triggers and <I
CLASS="FIRSTTERM"
>statement-level</I
> triggers,
    respectively.
   </P
><P
>    Triggers are also classified as <I
CLASS="FIRSTTERM"
>before</I
> triggers and
    <I
CLASS="FIRSTTERM"
>after</I
> triggers.
    Statement-level before triggers naturally fire before the
    statement starts to do anything, while statement-level after
    triggers fire at the very end of the statement.  Row-level before
    triggers fire immediately before a particular row is operated on,
    while row-level after triggers fire at the end of the statement
    (but before any statement-level after triggers).
   </P
><P
>    Trigger functions invoked by per-statement triggers should always
    return <TT
CLASS="SYMBOL"
>NULL</TT
>. Trigger functions invoked by per-row
    triggers can return a table row (a value of
    type <TT
CLASS="STRUCTNAME"
>HeapTuple</TT
>) to the calling executor,
    if they choose.  A row-level trigger fired before an operation has
    the following choices:

    <P
></P
></P><UL
><LI
><P
>       It can return <TT
CLASS="SYMBOL"
>NULL</TT
> to skip the operation for the
       current row. This instructs the executor to not perform the
       row-level operation that invoked the trigger (the insertion or
       modification of a particular table row).
      </P
></LI
><LI
><P
>       For row-level <TT
CLASS="COMMAND"
>INSERT</TT
>
       and <TT
CLASS="COMMAND"
>UPDATE</TT
> triggers only, the returned row
       becomes the row that will be inserted or will replace the row
       being updated.  This allows the trigger function to modify the
       row being inserted or updated.
      </P
></LI
></UL
><P>

    A row-level before trigger that does not intend to cause either of
    these behaviors must be careful to return as its result the same
    row that was passed in (that is, the <TT
CLASS="VARNAME"
>NEW</TT
> row
    for <TT
CLASS="COMMAND"
>INSERT</TT
> and <TT
CLASS="COMMAND"
>UPDATE</TT
>
    triggers, the <TT
CLASS="VARNAME"
>OLD</TT
> row for
    <TT
CLASS="COMMAND"
>DELETE</TT
> triggers).
   </P
><P
>    The return value is ignored for row-level triggers fired after an
    operation, and so they can return <TT
CLASS="SYMBOL"
>NULL</TT
>.
   </P
><P
>    If more than one trigger is defined for the same event on the same
    relation, the triggers will be fired in alphabetical order by
    trigger name.  In the case of before triggers, the
    possibly-modified row returned by each trigger becomes the input
    to the next trigger.  If any before trigger returns
    <TT
CLASS="SYMBOL"
>NULL</TT
>, the operation is abandoned for that row and subsequent
    triggers are not fired.
   </P
><P
>    Typically, row before triggers are used for checking or
    modifying the data that will be inserted or updated.  For example,
    a before trigger might be used to insert the current time into a
    <TT
CLASS="TYPE"
>timestamp</TT
> column, or to check that two elements of the row are
    consistent. Row after triggers are most sensibly
    used to propagate the updates to other tables, or make consistency
    checks against other tables.  The reason for this division of labor is
    that an after trigger can be certain it is seeing the final value of the
    row, while a before trigger cannot; there might be other before triggers
    firing after it.  If you have no specific reason to make a trigger before
    or after, the before case is more efficient, since the information about
    the operation doesn't have to be saved until end of statement.
   </P
><P
>    If a trigger function executes SQL commands then these
    commands might fire triggers again. This is known as cascading
    triggers.  There is no direct limitation on the number of cascade
    levels.  It is possible for cascades to cause a recursive invocation
    of the same trigger; for example, an <TT
CLASS="COMMAND"
>INSERT</TT
>
    trigger might execute a command that inserts an additional row
    into the same table, causing the <TT
CLASS="COMMAND"
>INSERT</TT
> trigger
    to be fired again.  It is the trigger programmer's responsibility
    to avoid infinite recursion in such scenarios.
   </P
><P
>    When a trigger is being defined, arguments can be specified for
    it.<A
NAME="AEN42293"
></A
> The purpose of including arguments in the
    trigger definition is to allow different triggers with similar
    requirements to call the same function.  As an example, there
    could be a generalized trigger function that takes as its
    arguments two column names and puts the current user in one and
    the current time stamp in the other.  Properly written, this
    trigger function would be independent of the specific table it is
    triggering on.  So the same function could be used for
    <TT
CLASS="COMMAND"
>INSERT</TT
> events on any table with suitable
    columns, to automatically track creation of records in a
    transaction table for example. It could also be used to track
    last-update events if defined as an <TT
CLASS="COMMAND"
>UPDATE</TT
>
    trigger.
   </P
><P
>    Each programming language that supports triggers has its own method
    for making the trigger input data available to the trigger function.
    This input data includes the type of trigger event (e.g.,
    <TT
CLASS="COMMAND"
>INSERT</TT
> or <TT
CLASS="COMMAND"
>UPDATE</TT
>) as well as any
    arguments that were listed in <TT
CLASS="COMMAND"
>CREATE TRIGGER</TT
>.
    For a row-level trigger, the input data also includes the
    <TT
CLASS="VARNAME"
>NEW</TT
> row for <TT
CLASS="COMMAND"
>INSERT</TT
> and
    <TT
CLASS="COMMAND"
>UPDATE</TT
> triggers, and/or the <TT
CLASS="VARNAME"
>OLD</TT
> row
    for <TT
CLASS="COMMAND"
>UPDATE</TT
> and <TT
CLASS="COMMAND"
>DELETE</TT
> triggers.
    Statement-level triggers do not currently have any way to examine the
    individual row(s) modified by the statement.
   </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="triggers.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-datachanges.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Triggers</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="triggers.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Visibility of Data Changes</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>