CREATE EVENT TRIGGER

CREATE EVENT TRIGGER

7

SQL - Language Statements

CREATE EVENT TRIGGER

define a new event trigger

CREATE EVENT TRIGGER

name

ON

event

\[ WHEN

filter\_variable

IN (

filter\_value

\[, \... \]) \[ AND \... \] \] EXECUTE { FUNCTION \| PROCEDURE }

function\_name

()

Description
===========

`CREATE EVENT TRIGGER` creates a new event trigger. Whenever the
designated event occurs and the `WHEN` condition associated with the
trigger, if any, is satisfied, the trigger function will be executed.
For a general introduction to event triggers, see
[???](#event-triggers). The user who creates an event trigger becomes
its owner.

Parameters
==========

name

:   The name to give the new trigger. This name must be unique within
    the database.

event

:   The name of the event that triggers a call to the given function.
    See [???](#event-trigger-definition) for more information on event
    names.

filter\_variable

:   The name of a variable used to filter events. This makes it possible
    to restrict the firing of the trigger to a subset of the cases in
    which it is supported. Currently the only supported filter\_variable
    is `TAG`.

filter\_value

:   A list of values for the associated filter\_variable for which the
    trigger should fire. For `TAG`, this means a list of command tags
    (e.g., `'DROP FUNCTION'`).

function\_name

:   A user-supplied function that is declared as taking no argument and
    returning type `event_trigger`.

    In the syntax of `CREATE EVENT TRIGGER`, the keywords `FUNCTION` and
    `PROCEDURE` are equivalent, but the referenced function must in any
    case be a function, not a procedure. The use of the keyword
    `PROCEDURE` here is historical and deprecated.

Notes {#sql-createeventtrigger-notes}
=====

Only superusers can create event triggers.

Event triggers are disabled in single-user mode (see
[???](#app-postgres)). If an erroneous event trigger disables the
database so much that you can\'t even drop the trigger, restart in
single-user mode and you\'ll be able to do that.

Examples {#sql-createeventtrigger-examples}
========

Forbid the execution of any [DDL](#ddl) command:

    CREATE OR REPLACE FUNCTION abort_any_command()
      RETURNS event_trigger
     LANGUAGE plpgsql
      AS $$
    BEGIN
      RAISE EXCEPTION 'command % is disabled', tg_tag;
    END;
    $$;

    CREATE EVENT TRIGGER abort_ddl ON ddl_command_start
       EXECUTE FUNCTION abort_any_command();

Compatibility {#sql-createeventtrigger-compatibility}
=============

There is no `CREATE EVENT TRIGGER` statement in the SQL standard.

See Also
========
