src/test/regress/regress.cと contrib/spiにはもっと複雑な例があります。
トリガの使い方に関するとても簡単な例をここに示します。trigf関数はトリガを発行したttestリレーション内にあるタプル数を報告し、問い合わせがxにNULLを挿入しようとしていた場合はその操作をとばします(つまり、この関数は、トランザクションを中断させないNOT NULL制約のような動作をします)。
#include "executor/spi.h" /* これは SPIを使用する場合に必要なものです */ #include "commands/trigger.h" /* これは SPI とトリガの場合です */ extern Datum trigf(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(trigf); Datum trigf(PG_FUNCTION_ARGS) { TriggerData *trigdata = (TriggerData *) fcinfo->context; TupleDesc tupdesc; HeapTuple rettuple; char *when; bool checknull = false; bool isnull; int ret, i; /* trigdata が期待どおりのものを指していることを確認します */ if (!CALLED_AS_TRIGGER(fcinfo)) elog(ERROR, "trigf: not fired by trigger manager"); /* エクゼキュータに返すタプル */ if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event)) rettuple = trigdata->tg_newtuple; else rettuple = trigdata->tg_trigtuple; /* NULLに関する点検を行うかどうか */ if (!TRIGGER_FIRED_BY_DELETE(trigdata->tg_event) && TRIGGER_FIRED_BEFORE(trigdata->tg_event)) checknull = true; if (TRIGGER_FIRED_BEFORE(trigdata->tg_event)) when = "before"; else when = "after "; tupdesc = trigdata->tg_relation->rd_att; /* SPIマネージャに接続します */ if ((ret = SPI_connect()) < 0) elog(NOTICE, "trigf (fired %s): SPI_connect returned %d", when, ret); /* リレーション中のタプル数を取得します */ ret = SPI_exec("SELECT count(*) FROM ttest", 0); if (ret < 0) elog(NOTICE, "trigf (fired %s): SPI_exec returned %d", when, ret); /* PG7.2 では count(*) は int8 を返します。変換に注意して下さい*/ i = (int) DatumGetInt64(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &isnull)); elog (NOTICE, "trigf (fired %s): there are %d tuples in ttest", when, i); SPI_finish(); if (checknull) { (void) SPI_getbinval(rettuple, tupdesc, 1, &isnull); if (isnull) rettuple = NULL; } return PointerGetDatum(rettuple); }
ここでコンパイルを行い、トリガ関数を作ります。
CREATE FUNCTION trigf () RETURNS OPAQUE AS '...path_to_so' LANGUAGE 'C'; CREATE TABLE ttest (x int4);
vac=> CREATE TRIGGER tbefore BEFORE INSERT OR UPDATE OR DELETE ON ttest FOR EACH ROW EXECUTE PROCEDURE trigf(); CREATE vac=> CREATE TRIGGER tafter AFTER INSERT OR UPDATE OR DELETE ON ttest FOR EACH ROW EXECUTE PROCEDURE trigf(); CREATE vac=> INSERT INTO ttest VALUES (NULL); NOTICE:trigf (fired before): there are 0 tuples in ttest INSERT 0 0 -- 挿入操作はとばされ、また、AFTERトリガも発行されません。 vac=> SELECT * FROM ttest; x - (0 rows) vac=> INSERT INTO ttest VALUES (1); NOTICE:trigf (fired before): there are 0 tuples in ttest NOTICE:trigf (fired after ): there are 1 tuples in ttest ^^^^^^^^ 可視性の説明を思い出して下さい。 INSERT 167793 1 vac=> SELECT * FROM ttest; x - 1 (1 row) vac=> INSERT INTO ttest SELECT x * 2 FROM ttest; NOTICE:trigf (fired before): there are 1 tuples in ttest NOTICE:trigf (fired after ): there are 2 tuples in ttest ^^^^^^^^ 可視性の説明を思い出して下さい。 INSERT 167794 1 vac=> SELECT * FROM ttest; x - 1 2 (2 rows) vac=> UPDATE ttest SET x = null WHERE x = 2; NOTICE:trigf (fired before): there are 2 tuples in ttest UPDATE 0 vac=> UPDATE ttest SET x = 4 WHERE x = 2; NOTICE:trigf (fired before): there are 2 tuples in ttest NOTICE:trigf (fired after ): there are 2 tuples in ttest UPDATE 1 vac=> SELECT * FROM ttest; x - 1 4 (2 rows) vac=> DELETE FROM ttest; NOTICE:trigf (fired before): there are 2 tuples in ttest NOTICE:trigf (fired after ): there are 1 tuples in ttest NOTICE:trigf (fired before): there are 1 tuples in ttest NOTICE:trigf (fired after ): there are 0 tuples in ttest ^^^^^^^^ 可視性の説明を思い出して下さい。 DELETE 2 vac=> SELECT * FROM ttest; x - (0 rows)