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

40.3. C言語によるイベントトリガ関数の書き方 #

<title>Writing Event Trigger Functions in C</title>

This section describes the low-level details of the interface to an event trigger function. This information is only needed when writing event trigger functions in C. If you are using a higher-level language then these details are handled for you. In most cases you should consider using a procedural language before writing your event triggers in C. The documentation of each procedural language explains how to write an event trigger in that language. 本節ではトリガ関数とのインタフェースについて低レベルな詳細を説明します。 この情報はC言語でトリガ関数を作成する時にのみ必要です。 高レベルな言語で作成すれば、こうした詳細は代わりに扱ってもらえます。 たいていの場合、Cでトリガを作成する前に手続き言語を使用することを検討すべきです。 各手続き言語の文書で、その言語を使用したイベントトリガの作成方法を説明します。

Event trigger functions must use the <quote>version 1</quote> function manager interface. トリガ関数はversion 1関数マネージャインタフェースを使わなくてはいけません。

When a function is called by the event trigger manager, it is not passed any normal arguments, but it is passed a <quote>context</quote> pointer pointing to a <structname>EventTriggerData</structname> structure. C functions can check whether they were called from the event trigger manager or not by executing the macro: 関数がイベントトリガマネージャから呼び出される時は、通常の引数が渡されるのではなく、EventTriggerData構造体を指すcontextポインタが渡されます。 C関数は、イベントトリガマネージャから呼び出されたのかどうかを以下のマクロを実行することで検査することができます。

CALLED_AS_EVENT_TRIGGER(fcinfo)

which expands to: これは以下に展開されます。

((fcinfo)->context != NULL && IsA((fcinfo)->context, EventTriggerData))

If this returns true, then it is safe to cast <literal>fcinfo-&gt;context</literal> to type <literal>EventTriggerData *</literal> and make use of the pointed-to <structname>EventTriggerData</structname> structure. The function must <emphasis>not</emphasis> alter the <structname>EventTriggerData</structname> structure or any of the data it points to. もしこれが真を返す場合、fcinfo->contextEventTriggerData *型にキャストし、指されたEventTriggerData構造体を使用することは安全です。 その関数は、TriggerData構造体やそれが指すどのようなデータも変更してはいけません

<structname>struct EventTriggerData</structname> is defined in <filename>commands/event_trigger.h</filename>: struct EventTriggerDatacommands/event_trigger.hの中で定義されています。

typedef struct EventTriggerData
{
    NodeTag     type;

    const char *event;      /* event name */
    Node       *parsetree;  /* parse tree */
    CommandTag  tag;        /* command tag */

    const char *event;      /* イベント名 */
    Node       *parsetree;  /* 解析ツリー */
    CommandTag  tag;        /* コマンドタグ */
} EventTriggerData;

where the members are defined as follows: メンバは下記のように定義されています。

type

Always <literal>T_EventTriggerData</literal>. 常にT_EventTriggerDataです。

event

Describes the event for which the function is called, one of <literal>"ddl_command_start"</literal>, <literal>"ddl_command_end"</literal>, <literal>"sql_drop"</literal>, <literal>"table_rewrite"</literal>. See <xref linkend="event-trigger-definition"/> for the meaning of these events. その関数が呼び出されたイベント、"ddl_command_start""ddl_command_end""sql_drop""table_rewrite"のうちの1つを記述します。 これらのイベントの内容は、40.1を参照してください。

parsetree

A pointer to the parse tree of the command. Check the PostgreSQL source code for details. The parse tree structure is subject to change without notice. コマンドの解析ツリーへのポインタです。 詳細はPostgreSQLのソースコードを確認してください。 解析ツリーの構造は予告なく変更されることがあります。

tag

The command tag associated with the event for which the event trigger is run, for example <literal>"CREATE FUNCTION"</literal>. イベントトリガの実行対象となるイベントに関連するコマンドタグです。たとえば、"CREATE FUNCTION"です。

An event trigger function must return a <symbol>NULL</symbol> pointer (<emphasis>not</emphasis> an SQL null value, that is, do not set <parameter>isNull</parameter> true). イベントトリガ関数はNULLポインタ(SQLのNULLではありません。したがって、isNullは真にはなりません)を返さなければなりません。