As mentioned above, when function is called by the trigger manager, structure TriggerData *CurrentTriggerData is NOT NULL and initialized. So it is better to check CurrentTriggerData against being NULL at the start and set it to NULL just after fetching the information to prevent calls to a trigger function not from the trigger manager.
上述の通り、トリガーマネージャから関数が呼び出された時、 TriggerData *CurrentTriggerData 構造体は NOT NULL で初期化され ます。トリガーマネージャ以外からのトリガー関数の呼び出しを防ぐ ために、関数の最初で CurrentTriggerData が NULL になっていないか を点検し、情報を取り出した後はすぐに NULL を設定した方が良いで しょう。
struct TriggerData is defined in src/include/commands/trigger.h:
TriggerData 構造体は src/include/commands/trigger.h で次のよう に定義されています。
typedef struct TriggerData { TriggerEvent tg_event; Relation tg_relation; HeapTuple tg_trigtuple; HeapTuple tg_newtuple; Trigger *tg_trigger; } TriggerData;
tg_event describes event for which the function is called. You may use the following macros to examine tg_event: TRIGGER_FIRED_BEFORE(event) returns TRUE if trigger fired BEFORE; TRIGGER_FIRED_AFTER(event) returns TRUE if trigger fired AFTER; TRIGGER_FIRED_FOR_ROW(event) returns TRUE if trigger fired for ROW-level event; TRIGGER_FIRED_FOR_STATEMENT(event) returns TRUE if trigger fired for STATEMENT-level event; TRIGGER_FIRED_BY_INSERT(event) returns TRUE if trigger fired by INSERT; TRIGGER_FIRED_BY_DELETE(event) returns TRUE if trigger fired by DELETE; TRIGGER_FIRED_BY_UPDATE(event) returns TRUE if trigger fired by UPDATE.
tg_event は、関数を呼び出したイベントの種類を示します。次のマクロを tg_event を検査するために使用できます。 TRIGGER_FIRED_BEFORE(event) は BEFORE トリガーならばTRUE を返 します。 TRIGGER_FIRED_AFTER(event) は AFTER トリガーならば TRUE を返し ます。 TRIGGER_FIRED_FOR_ROW(event) は ROW レベルのトリガーならば TRUE を返します。 TRIGGER_FIRED_FOR_STATEMENT(event) は STATEMENT レベルのトリガ ーならば TRUE を返します。 TRIGGER_FIRED_BY_INSERT(event) は INSERT によって発行されたトリ ガーならば TRUE を返します。 TRIGGER_FIRED_BY_DELETE(event) は DELETE によって発行されたトリ ガーならば TRUE を返します。 TRIGGER_FIRED_BY_UPDATE(event) は UPDATE によって発行されたトリ ガーならば TRUE を返します。 tg_relation is pointer to structure describing the triggered relation. Look at src/include/utils/rel.h for details about this structure. The most interest things are tg_relation->rd_att (descriptor of the relation tuples) and tg_relation->rd_rel->relname (relation's name. This is not char*, but NameData. Use SPI_getrelname(tg_relation) to get char* if you need a copy of name). tg_relation は、トリガーが発行されたリレーションを示す構造体へのポインタで す。この構造体の詳細については src/include/utils/rel.h を見て下 さい。最も興味があることは、(リレーションのタプルの記述子を示 す)tg_relation->rd_att と(リレーションの名前を示す) tg_relation->rd_rel->relname です。( relname は、char* でなく、 NameData です。この名前のコピーが必要な場合は SPI_getrelname(tg_relation) を使って char* を取得して下さい。) tg_trigtuple is a pointer to the tuple for which the trigger is fired. This is the tuple being inserted (if INSERT), deleted (if DELETE) or updated (if UPDATE). If INSERT/DELETE then this is what you are to return to Executor if you don't want to replace tuple with another one (INSERT) or skip the operation. tg_trigtuple はトリガーが発行されたタプルのポインタです。( INSERT の場合は) 挿入された、( DELETE の場合は)削除された、( UPDATE の場合は) 更新されたタプルを示します。INSERT または DELETE の場合に、( INSERT の場合)他のものでそのタプルを置き換えたくない時や操作を とばしたくない時、これをエグゼキュータに返します。 tg_newtuple is a pointer to the new version of tuple if UPDATE and NULL if this is for an INSERT or a DELETE. This is what you are to return to Executor if UPDATE and you don't want to replace this tuple with another one or skip the operation. tg_newtuple は、UPDATE の場合はタプルの新しいバージョンへのポインタとなり、 INSERT または DELETE の場合は NULL となります。UPDATE の場合に、 他のものでこのタプルを置き換えたくない時や操作をとばしたくない 時、これをエグゼキュータに返します。 tg_trigger is pointer to structure Trigger defined in src/include/utils/rel.h: tg_trigger は、src/include/utils/rel.h で定義された Trigger 構造体へのポイ ンタです。 typedef struct Trigger { char *tgname; Oid tgfoid; func_ptr tgfunc; int16 tgtype; int16 tgnargs; int16 tgattr[8]; char **tgargs; } Trigger; tgname is the trigger's name, tgnargs is number of arguments in tgargs, tgargs is an array of pointers to the arguments specified in the CREATE TRIGGER statement. Other members are for internal use only. tgname はトリガー名、tgnargs は tgargs 内にある引数の数、tgargs は CREATE TRIGGER 文で指定した引数へのポインタの配列です。他のメ ンバは内部処理でのみに使用されます。