Home Explore Blog CI



postgresql

3rd chunk of `doc/src/sgml/trigger.sgml`
8622b652f74ebb04a9439b6f83e1ec2698cd9af0ac75801f0000000100000fa2
 transaction, rather than the end of the statement,
    if it was defined as a <firstterm>constraint trigger</firstterm>.
    In all cases, a trigger is executed as part of the same transaction as
    the statement that triggered it, so if either the statement or the
    trigger causes an error, the effects of both will be rolled back.
    Also, the trigger will always run in the security context of the role
    that executed the statement that caused the trigger to fire, unless
    the trigger function is defined as <literal>SECURITY DEFINER</literal>,
    in which case it will run as the function owner.
   </para>

   <para>
    If an <command>INSERT</command> contains an <literal>ON CONFLICT
    DO UPDATE</literal> clause, it is possible for row-level
    <literal>BEFORE</literal> <command>INSERT</command> and then
    <literal>BEFORE</literal> <command>UPDATE</command> triggers
    to be executed on triggered rows.  Such interactions can be
    complex if the triggers are not idempotent because change made by
    <literal>BEFORE</literal> <command>INSERT</command> triggers will be
    seen by <literal>BEFORE</literal> <command>UPDATE</command> triggers,
    including changes to <varname>EXCLUDED</varname> columns.
   </para>

   <para>
    Note that statement-level
    <command>UPDATE</command> triggers are executed when <literal>ON
    CONFLICT DO UPDATE</literal> is specified, regardless of whether or not
    any rows were affected by the <command>UPDATE</command> (and
    regardless of whether the alternative <command>UPDATE</command>
    path was ever taken).  An <command>INSERT</command> with an
    <literal>ON CONFLICT DO UPDATE</literal> clause will execute
    statement-level <literal>BEFORE</literal> <command>INSERT</command>
    triggers first, then statement-level <literal>BEFORE</literal>
    <command>UPDATE</command> triggers, followed by statement-level
    <literal>AFTER</literal> <command>UPDATE</command> triggers and finally
    statement-level <literal>AFTER</literal> <command>INSERT</command>
    triggers.
   </para>

   <para>
    A statement that targets a parent table in an inheritance or partitioning
    hierarchy does not cause the statement-level triggers of affected child
    tables to be fired; only the parent table's statement-level triggers are
    fired.  However, row-level triggers of any affected child tables will be
    fired.
   </para>

   <para>
    If an <command>UPDATE</command> on a partitioned table causes a row to move
    to another partition, it will be performed as a <command>DELETE</command>
    from the original partition followed by an <command>INSERT</command> into
    the new partition. In this case, all row-level <literal>BEFORE</literal>
    <command>UPDATE</command> triggers and all row-level
    <literal>BEFORE</literal> <command>DELETE</command> triggers are fired on
    the original partition. Then all row-level <literal>BEFORE</literal>
    <command>INSERT</command> triggers are fired on the destination partition.
    The possibility of surprising outcomes should be considered when all these
    triggers affect the row being moved. As far as <literal>AFTER ROW</literal>
    triggers are concerned, <literal>AFTER</literal> <command>DELETE</command>
    and <literal>AFTER</literal> <command>INSERT</command> triggers are
    applied; but <literal>AFTER</literal> <command>UPDATE</command> triggers
    are not applied because the <command>UPDATE</command> has been converted to
    a <command>DELETE</command> and an <command>INSERT</command>. As far as
    statement-level triggers are concerned, none of the
    <command>DELETE</command> or <command>INSERT</command> triggers are fired,
    even if row movement occurs; only the <command>UPDATE</command> triggers
    defined on the target table used in the <command>UPDATE</command> statement
    will be fired.
   </para>

   <para>
    No separate triggers are defined for <command>MERGE</command>. Instead,
    statement-level or

Title: Trigger Execution and Interaction
Summary
This section discusses the execution of triggers in various scenarios, including transactions, ON CONFLICT DO UPDATE clauses, inheritance and partitioning hierarchies, and row movement between partitions, and how triggers interact with each other and with the statements that trigger them, including the order of execution and the effects of idempotence and security context.