| PostgreSQL 8.3.3¹®¼ | ||||
|---|---|---|---|---|
| Prev | Fast Backward | Chapter 34. Æ®¸®°Å | Fast Forward | Next |
C¾ð¾î·Î ÀÛ¼ºÇÑ Æ®¸®°Å ÇÔ¼ö¿¡ °üÇÑ ¸Å¿ì °£´ÜÇÑ ¿¹ÀÔ´Ï´Ù. (ÀýÂ÷»óÀÇ ¾ð¾î·Î ÀÛ¼ºÇÑ Æ®¸®°ÅÀÇ ¿¹´Â, ±× ÀýÂ÷»óÀÇ ¾ð¾îÀÇ ¹®¼¿¡ ±âÀçµÇ¾î ÀÖ½À´Ï´Ù).
trigfÇÔ¼ö´Â,ttestÅ×ÀÌºí³»¿¡ ÀÖ´Â Çà ¼ö¸¦ º¸°íÇØ, ±× ¸í·ÉÀÌxÄ÷³¿¡ NULL°ª¸¦ »ðÀÔÇÏ·Á°í Çϰí ÀÖ¾úÀ» °æ¿ì´Â,
±× ½ÇÁúÀûÀÎ Á¶ÀÛÀ» »ý·«ÇÕ´Ï´Ù
(Áï, ÀÌ Æ®¸®°Å´Â, Æ®·£Àè¼Ç(transaction)À» ÁߴܽÃŰÁö ¾Ê´Â NOT 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(*)´Â 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
^^^^^^
remember what we said about visibility.
DELETE 2
=> SELECT * FROM ttest;
x
---
(0 rows)
src/test/regress/regress.c¿Ícontrib/spi¿¡´Â Á» ´õ º¹ÀâÇÑ ¿¹°¡ ÀÖ½À´Ï´Ù.