認麯滇婔婻䫘 C 喍䔇麂婩䞔剘䔇蓥埏単冋床㔗庘昄 trigf
檖只 ttest 臘䔇臯昄麟幽婫套悩变傴臘商檪 NULL 某噖彄庖枕 x 麯(幘儌滇垄啔婺婔婻麂䷺亥溘嘖婉锔庺庋媇)施䘖誺淉嘩㔗
饡噽臘垔幬
CREATE TABLE ttest ( x integer );
認麯滇蓥埏単庘昄䔇溊傼乕
#include "postgres.h" #include "executor/spi.h" /* 嘹䫘 SPI 䔇施唍襕䫘䔇崘桺傽 */ #include "commands/trigger.h" /* 䫘蓥埏単施襕䫘䔇崘桺傽 */ 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; /* 䇞媇躻噌滇嘩婺蓥埏単脄䫘䔇 */ if (!CALLED_AS_TRIGGER(fcinfo)) elog(ERROR, "trigf: not called 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(INFO, "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); /* count(*) 誫啂 int8 欔傖襕償媄蘸扵 */ i = DatumGetInt64(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &isnull)); elog (INFO, "trigf (fired %s): there are %d rows in ttest", when, i); SPI_finish(); if (checknull) { SPI_getbinval(rettuple, tupdesc, 1, &isnull); if (isnull) rettuple = NULL; } return PointerGetDatum(rettuple); }
䚡臏垯溊傼乕劯弄滯庘昄幽录傺蓥埏単
CREATE FUNCTION trigf() RETURNS trigger AS 'filename' LANGUAGE C; CREATE TRIGGER tbefore BEFORE INSERT OR UPDATE OR DELETE ON ttest FOR EACH ROW EXECUTE PROCEDURE trigf(); CREATE TRIGGER tafter AFTER INSERT OR UPDATE OR DELETE ON ttest FOR EACH ROW EXECUTE PROCEDURE trigf();
䯄婘嘹埇傖敋臘蓥埏単䔇淉嘩
=> INSERT INTO ttest VALUES (NULL); INFO: trigf (fired before): there are 0 rows in ttest INSERT 0 0 -- 某噖赆媘䘖AFTER 蓥埏単澇橬蓥埏 => SELECT * FROM ttest; x --- (0 rows) => INSERT INTO ttest VALUES (1); INFO: trigf (fired before): there are 0 rows in ttest INFO: trigf (fired after ): there are 1 rows in ttest ^^^^^^^^ 誻螄冖螾誺䔇噿庯埇蓖攓䔇寘彍劖 INSERT 167793 1 vac=> SELECT * FROM ttest; x --- 1 (1 row) => INSERT INTO ttest SELECT x * 2 FROM ttest; INFO: trigf (fired before): there are 1 rows in ttest INFO: trigf (fired after ): there are 2 rows in ttest ^^^^^^ 誻螄冖螾誺䔇噿庯埇蓖攓䔇寘彍劖 INSERT 167794 1 => SELECT * FROM ttest; x --- 1 2 (2 rows) => UPDATE ttest SET x = NULL WHERE x = 2; INFO: trigf (fired before): there are 2 rows in ttest UPDATE 0 => UPDATE ttest SET x = 4 WHERE x = 2; INFO: trigf (fired before): there are 2 rows in ttest INFO: trigf (fired after ): there are 2 rows in ttest UPDATE 1 vac=> SELECT * FROM ttest; x --- 1 4 (2 rows) => DELETE FROM ttest; INFO: trigf (fired before): there are 2 rows in ttest INFO: trigf (fired before): there are 1 rows in ttest INFO: trigf (fired after ): there are 0 rows in ttest INFO: trigf (fired after ): there are 0 rows in ttest ^^^^^^ 誻螄冖螾誺䔇噿庯埇蓖攓䔇寘彍劖 DELETE 2 => SELECT * FROM ttest; x --- (0 rows)
婘 src/test/regress/regress.c 启 contrib/spi 麯誻橬敘崉溗䔇冋床㔗