Annotation Type Trigger


@Target({})
@Retention(CLASS)
@Documented
public @interface Trigger
Annotation, only used in @Function(triggers=...), to specify what trigger(s) the function will be called for.

Transition tables (tableOld() and tableNew()) appear in PostgreSQL 10. If a trigger is declared with tableOld="oo", tableNew="nn", then the trigger function can query oo and nn as if they are actual tables with the same columns as the table responsible for the trigger, and containing the affected rows before and after the changes. Only an AFTER trigger can have transition tables. An UPDATE will populate both tables. INSERT will not populate the old table, and DELETE will not populate the new table. It is an error to specify either table if events does not include at least one event that could populate that table. As long as at least one such event is included, the table can be specified, and will simply have no rows if the trigger is invoked for an event that does not populate it.

In an after-statement trigger, the transition tables include all rows affected by the statement. In an after-row trigger, the same is true: after-row triggers are all queued until the statement completes, and then the function will be invoked for each row that was affected, but will see the complete transition tables on each invocation.

Author:
Thomas Hallgren
  • Nested Class Summary

    Nested Classes
    Modifier and Type Class Description
    static class  Trigger.Called
    Whether the trigger is invoked before or after the specified event.
    static class  Trigger.Constraint
    Deferrability (only applies to constraint triggers).
    static class  Trigger.Event
    Types of event that can occasion a trigger.
    static class  Trigger.Scope
    Whether the trigger will occur only once for a statement of interest, or once for each row affected by the statement.
  • Required Element Summary

    Required Elements
    Modifier and Type Required Element Description
    Trigger.Called called
    Denotes if the trigger is fired before, after, or instead of its scope (row or statement)
    Trigger.Event[] events
    The event(s) that will trigger the call.
    String table
    The table that this trigger is tied to.
  • Optional Element Summary

    Optional Elements
    Modifier and Type Optional Element Description
    String[] arguments
    Arguments to be passed to the trigger function.
    String[] columns
    A list of columns (only meaningful for an UPDATE trigger).
    String comment
    A comment to be associated with the trigger.
    Trigger.Constraint constraint
    Only for a constraint trigger, whether it is deferrable and, if so, initially immediate or deferred.
    String from
    The name of another table referenced by the constraint.
    String fromSchema
    The schema containing another table referenced by the constraint.
    String name
    Name of the trigger.
    String schema
    The name of the schema containing the table for this trigger.
    Trigger.Scope scope
    Scope: statement or row.
    String tableNew
    Name to refer to "after" table of affected rows.
    String tableOld
    Name to refer to "before" table of affected rows.
    String when
    A boolean condition limiting when the trigger can be fired.
  • Element Details

    • events

      Trigger.Event[] events
      The event(s) that will trigger the call.
    • table

      String table
      The table that this trigger is tied to.
    • called

      Denotes if the trigger is fired before, after, or instead of its scope (row or statement)
    • arguments

      String[] arguments
      Arguments to be passed to the trigger function.
      Default:
      {}
    • constraint

      Trigger.Constraint constraint
      Only for a constraint trigger, whether it is deferrable and, if so, initially immediate or deferred. To create a constraint trigger that is not deferrable, this attribute must be explicitly given with the value NOT_DEFERRABLE; leaving it to default is not the same. When this attribute is not specified, a normal trigger, not a constraint trigger, is created.

      A constraint trigger must have called=AFTER and scope=ROW.

      Default:
      NOT_DEFERRABLE
    • from

      String from
      The name of another table referenced by the constraint. This option is used for foreign-key constraints and is not recommended for general use. This can only be specified for constraint triggers. If the name should be schema-qualified, use fromSchema to specify the schema.
      Default:
      ""
    • fromSchema

      String fromSchema
      The schema containing another table referenced by the constraint. This can only be specified for constraint triggers, and only to name the schema for a table named with from.
      Default:
      ""
    • name

      String name
      Name of the trigger. If not set, the name will be generated.
      Default:
      ""
    • schema

      String schema
      The name of the schema containing the table for this trigger.
      Default:
      ""
    • scope

      Scope: statement or row.
      Default:
      STATEMENT
    • when

      String when
      A boolean condition limiting when the trigger can be fired. This text is injected verbatim into the generated SQL, after the keyword WHEN.
      Default:
      ""
    • columns

      String[] columns
      A list of columns (only meaningful for an UPDATE trigger). The trigger will only fire for update if at least one of the columns is mentioned as a target of the update command.
      Default:
      {}
    • tableOld

      String tableOld
      Name to refer to "before" table of affected rows. Only usable in an AFTER trigger whose events include UPDATE or DELETE. The trigger function can issue queries as if a table by this name exists and contains all rows affected by the event, in their prior state. (If the trigger is called for an event other than UPDATE or DELETE, the function can still query a table by this name, which will appear to be empty.)
      Default:
      ""
    • tableNew

      String tableNew
      Name to refer to "after" table of affected rows. Only usable in an AFTER trigger whose events include UPDATE or INSERT. The trigger function can issue queries as if a table by this name exists and contains all rows affected by the event, in their new state. (If the trigger is called for an event other than UPDATE or INSERT, the function can still query a table by this name, which will appear to be empty.)
      Default:
      ""
    • comment

      String comment
      A comment to be associated with the trigger. If left to default, and the Java function has a doc comment, its first sentence will be used. If an empty string is explicitly given, no comment will be set.
      Default:
      ""