¾�ΥС�������ʸ�� �� 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9.6 | 9.5 | 9.4 | 9.3 | 9.2 | 9.1 | 9.0 | 8.4 | 8.3 | 8.2 | 8.1 | 8.0 | 7.4 | 7.3 | 7.2

36.4. �����ʥȥꥬ����

C����Ǻ��������ȥꥬ�ؿ��˴ؤ���ȤƤ��ñ����򤳤��˼����ޤ� �ʼ�³������Ǻ��������ȥꥬ����ϡ����μ�³�������ʸ��˵��ܤ���Ƥ��ޤ�����

trigf�ؿ��ϡ�ttest�ơ��֥���ˤ���Կ�����𤷡��䤤��碌��x��NULL�ͤ��������褦�Ȥ��Ƥ������ϡ������������Ф��ޤ� �ʤĤޤꡢ���Υȥꥬ�ϡ��ȥ�󥶥����������Ǥ����ʤ�NOT NULL����Τ褦��ư��򤷤ޤ�����

�ޤ����ʲ��Τ褦�˥ơ��֥��������ޤ���

CREATE TABLE ttest (
    x integer
);

�ʲ����ȥꥬ�ؿ��Υ����������ɤǤ���

#include "postgres.h"


#include "executor/spi.h"       /* �����SPI����Ѥ������ɬ�פʤ�� */
#include "commands/trigger.h"   /* ����ϥȥꥬ��ɬ�פʤ�� */

#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif

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(ERROR, "trigf (fired %s): SPI_connect returned %d", when, ret);


    /* �ơ��֥���ιԿ������ */
    ret = SPI_exec("SELECT count(*) FROM ttest", 0);

    if (ret < 0)
        elog(ERROR, "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);
}

�����������ɤ򥳥�ѥ����項35.9.6�򻲾Ȥ��Ƥ��������ˤ�����ˡ��ʲ����ͤ˴ؿ��ȥȥꥬ��������ޤ���

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�ˤϤ�ä�ʣ�����㤬����ޤ���