バージョンごとのドキュメント一覧

40.1. イベントトリガ動作の概要 #

<title>Overview of Event Trigger Behavior</title>

An event trigger fires whenever the event with which it is associated occurs in the database in which it is defined. Currently, the only supported events are <literal>ddl_command_start</literal>, <literal>ddl_command_end</literal>, <literal>table_rewrite</literal> and <literal>sql_drop</literal>. Support for additional events may be added in future releases. イベントトリガは、関連づけられたイベントが、定義されたデータベースで起こるたびに実行されます。 現在の所サポートされているイベントは、ddl_command_startddl_command_endtable_rewritesql_dropです。 今後のリリースで新たなイベントが追加されるかもしれません。

The <literal>ddl_command_start</literal> event occurs just before the execution of a <literal>CREATE</literal>, <literal>ALTER</literal>, <literal>DROP</literal>, <literal>SECURITY LABEL</literal>, <literal>COMMENT</literal>, <literal>GRANT</literal> or <literal>REVOKE</literal> command. No check whether the affected object exists or doesn't exist is performed before the event trigger fires. As an exception, however, this event does not occur for DDL commands targeting shared objects &mdash; databases, roles, and tablespaces &mdash; or for commands targeting event triggers themselves. The event trigger mechanism does not support these object types. <literal>ddl_command_start</literal> also occurs just before the execution of a <literal>SELECT INTO</literal> command, since this is equivalent to <literal>CREATE TABLE AS</literal>. ddl_command_startイベントは、CREATEALTERDROPSECURITY LABELCOMMENTGRANTREVOKEコマンドの実行の直前に発生します。 影響するオブジェクトが存在するかどうかのチェックはイベントトリガが実行されるまで行われません。 しかしながら例外として、共有オブジェクト — データベース、ロール、テーブルスペース — を対象としているDDLコマンド、もしくは、イベントトリガ自体をターゲットにしたコマンドに対してのイベントは起こりません。 ddl_command_startはまた、SELECT INTOコマンドの実行直前にも発生します。このコマンドはCREATE TABLE ASコマンドと同等だからです。

The <literal>ddl_command_end</literal> event occurs just after the execution of this same set of commands. To obtain more details on the <acronym>DDL</acronym> operations that took place, use the set-returning function <literal>pg_event_trigger_ddl_commands()</literal> from the <literal>ddl_command_end</literal> event trigger code (see <xref linkend="functions-event-triggers"/>). Note that the trigger fires after the actions have taken place (but before the transaction commits), and thus the system catalogs can be read as already changed. ddl_command_endイベントは、ddl_command_startイベントが対象とする同様のコマンドの実行後に発生します。 発生したDDL操作のより詳細を取得するには、ddl_command_endイベントトリガコード(9.29を参照してください)で集合を返す関数pg_event_trigger_ddl_commands()を使ってください。 トリガはアクションが起きた後(ただし、トランザクションのコミットの前)に起動し、システムカタログは既に変更されたものとして読まれることに注意してください。

The <literal>sql_drop</literal> event occurs just before the <literal>ddl_command_end</literal> event trigger for any operation that drops database objects. To list the objects that have been dropped, use the set-returning function <literal>pg_event_trigger_dropped_objects()</literal> from the <literal>sql_drop</literal> event trigger code (see <xref linkend="functions-event-triggers"/>). Note that the trigger is executed after the objects have been deleted from the system catalogs, so it's not possible to look them up anymore. sql_dropイベントは、データベースオブジェクトを削除する命令に対するddl_command_endイベントトリガの直前に発生します。 削除されたオブジェクトを一覧として出力するために、sql_dropイベントトリガコード(9.29を参照してください)からpg_event_trigger_dropped_objects()という集合を返す関数を使用します。 トリガはオブジェクトがシステムカタログから削除された後に実行されるため、以後、そのオブジェクトを見ることができないことに注意してください。

The <literal>table_rewrite</literal> event occurs just before a table is rewritten by some actions of the commands <literal>ALTER TABLE</literal> and <literal>ALTER TYPE</literal>. While other control statements are available to rewrite a table, like <literal>CLUSTER</literal> and <literal>VACUUM</literal>, the <literal>table_rewrite</literal> event is not triggered by them. コマンドALTER TABLEALTER TYPEのアクションによりテーブルが書き換えられる直前に、table_rewriteイベントは発生します。 CLUSTERVACUUMのような他の制御文でもテーブルを書き換えられますが、それらではtable_rewriteイベントは引き起こされません。

Event triggers (like other functions) cannot be executed in an aborted transaction. Thus, if a DDL command fails with an error, any associated <literal>ddl_command_end</literal> triggers will not be executed. Conversely, if a <literal>ddl_command_start</literal> trigger fails with an error, no further event triggers will fire, and no attempt will be made to execute the command itself. Similarly, if a <literal>ddl_command_end</literal> trigger fails with an error, the effects of the DDL statement will be rolled back, just as they would be in any other case where the containing transaction aborts. イベントトリガは(他の関数のように)中断したトランザクションでは実行されません。 従って、DDLコマンドがエラーで失敗した場合、関連するddl_command_endトリガは実行されません。 逆に、もしddl_command_startトリガがエラーで失敗した場合、他のイベントトリガは起動されず、コマンド自体も実行されません。 同様に、もしddl_command_endトリガがエラーで失敗した場合、それを含むトランザクションが失敗した場合のようにDDL文はロールバックされます。

For a complete list of commands supported by the event trigger mechanism, see <xref linkend="event-trigger-matrix"/>. イベントトリガでサポートされているコマンドの一覧は、40.2を参照してください。

Event triggers are created using the command <xref linkend="sql-createeventtrigger"/>. In order to create an event trigger, you must first create a function with the special return type <literal>event_trigger</literal>. This function need not (and may not) return a value; the return type serves merely as a signal that the function is to be invoked as an event trigger. イベントトリガは、コマンドCREATE EVENT TRIGGERを使用して作成されます。 イベントトリガを作成するために、まず特別な型event_triggerを返す関数を作る必要があります。 この関数は値を返す必要はありません。というのも、その戻り値型は単にシグナルとして、その関数がイベントトリガを呼び出していることを示しているだけだからです。

If more than one event trigger is defined for a particular event, they will fire in alphabetical order by trigger name. 特定のイベントに対して複数のイベントトリガが定義された場合、トリガ名のアルファベット順で起動されます。

A trigger definition can also specify a <literal>WHEN</literal> condition so that, for example, a <literal>ddl_command_start</literal> trigger can be fired only for particular commands which the user wishes to intercept. A common use of such triggers is to restrict the range of DDL operations which users may perform. トリガ定義はWHEN条件で特定されます。そのため、例えばddl_command_startトリガはユーザが望む特定のコマンドのみを契機に実行させることができます。 このようなトリガの一般的な使い方として、ユーザが実行するかもしれないDDL文の範囲を狭めることができます。