¿©±â¼ C ¾ð¾î·Î ±¸ÇöÇÑ ¾ÆÁÖ °£´ÜÇÑ Æ®¸®°Å ÇÔ¼ö Çϳª¸¦ ¼Ò°³ÇÑ´Ù. (°¢ ÇÁ·Î½ÃÁ®º° Æ®¸®°Å ÇÔ¼ö ¿¹Á¦´Â °¢ ¾ð¾îº° ¼³¸í¼¸¦ ÂüÁ¶Ç϶ó.)
trigf
ÇÔ¼ö´Â ttest Å×ÀÌºí¿¡¼
¾î¶² ÀÛ¾÷ÀÌ ÀϾ¸é, ±× Å×À̺íÀÇ ·Î¿ì °³¼ö¸¦ º¸¿©ÁÖ´Â ÇÔ¼ö´Ù.
À̶§, x Ä®·³ÀÇ °ªÀÌ null ÀÌ µÉ °æ¿ì´Â ¾î¶°ÇÑ ÀÛ¾÷µµ ÇÏÁö ¾Êµµ·Ï ÁöÁ¤Çß´Ù.
¸ÕÀú, Å×À̺í Á¤ÀÇ´Â ´ÙÀ½°ú °°´Ù:
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(*) °á°ú´Â 8¹ÙÀÌÆ® Á¤¼öÇüÀÌ´Ù. Çü º¯È¯ÀÌ ÇÊ¿äÇÏ´Ù */ 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 -- °ªÀÌ nullÀ̱⠶§¹®¿¡, BEFORE Æ®¸®°Å´Â ÀÛµ¿ÇÏ°í, 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 µð·ºÅ丮 ¾È¿¡ ÀÖ´Ù.