32.3. �� C �����

�������������������ĵͲ�ϸ�ڡ�ֻ�е�����C��д������������ʱ�����Ҫ��Щ��Ϣ�� �������ij�ָ߼�����д����������ôϵͳ�ͻ�Ϊ�㴦����Щϸ�ڡ� ÿ�ֹ������Ե��ĵ������й�������ø�������д�������Ľ��͡�

��������������ʹ��"�汾 1��version 1��"�ĺ����������ӿڡ�

��һ������������������������ʱ���������յ��κ���ͨ�����������յ�һ��ָ��TriggerData�ṹ��"����"ָ�롣 C ��������ͨ��ִ��ʵ���ϱ���չΪ

(fcinfo)->context != NULL && IsA((fcinfo)->context, TriggerData))

�ĺ�

CALLED_AS_TRIGGER(fcinfo)

CALLED_AS_TRIGGER(fcinfo)�� ���ж��Լ��Ƿ�Ӵ������������е��õġ� ����˺귵����(TRUE)������԰�ȫ�ذ�fcinfo->contextת�������� TriggerData * Ȼ��ʹ�����ָ�� TriggerData �Ľṹ�� ����������������� TriggerData �ṹ������ָ����κ����ݡ�

struct TriggerData ���� commands/trigger.h ���涨��ģ�

typedef struct TriggerData
{
    NodeTag       type;
    TriggerEvent  tg_event;
    Relation      tg_relation;
    HeapTuple     tg_trigtuple;
    HeapTuple     tg_newtuple;
    Trigger      *tg_trigger;
    Buffer        tg_trigtuplebuf;
    Buffer        tg_newtuplebuf;
} TriggerData;

��Щ��Ա�Ķ������£�

type

���� T_TriggerData��

tg_event

�������ú������¼��������������ĺ��� tg_event��

TRIGGER_FIRED_BEFORE(tg_event)

������������ڲ���ǰ�����������档

TRIGGER_FIRED_AFTER(tg_event)

������������ڲ����󴥷��������档

TRIGGER_FIRED_FOR_ROW(tg_event)

������������м����¼������������档

TRIGGER_FIRED_FOR_STATEMENT(tg_event)

�������������伶���¼������������档

TRIGGER_FIRED_BY_INSERT(tg_event)

�������������INSERT�����������档

TRIGGER_FIRED_BY_UPDATE(tg_event)

�������������UPDATE�����������档

TRIGGER_FIRED_BY_DELETE(tg_event)

�������������DELETE�����������档

tg_relation

��һ��ָ�������������Ĺ�ϵ�Ľṹ��ָ�롣 ��ο�utils/rel.h��ȡ���ڴ˽ṹ����ϸ��Ϣ�� �����˸���Ȥ�������� tg_relation->rd_att����ϵ�е������� ��tg_relation->rd_rel->relname����ϵ����������������Ͳ��� char*������NameData�� �������Ҫһ�����ֵĿ������� SPI_getrelname(tg_relation)��ȡchar* ����

tg_trigtuple

��һ��ָ�򴥷����������е�ָ�롣����һ�����ڱ����루INSERT���� ɾ����DELETE������£�UPDATE�����С������ INSERT��DELETE�� ��ô������㽫���ظ�ִ���ߵĶ��� — ����㲻������һ���и��Ǵ��У�INSERT������Բ�������DELETE��ʱ�򣩡�

tg_newtuple

�����UPDATE������һ��ָ���°汾���е�ָ�룬�����INSERT ��DELETE�� ����NULL��������㽫���ظ�ִ���ߵĶ��� — ����¼��� UPDATE �����㲻������һ�����滻�����л���Բ�����

tg_trigger

��һ��ָ��ṹTrigger��ָ�룬�ýṹ��utils/rel.h�ﶨ�壺

typedef struct Trigger
{
    Oid	 tgoid;
    char       *tgname;
    Oid	 tgfoid;
    int16       tgtype;
    bool	tgenabled;
    bool	tgisconstraint;
    Oid	 tgconstrrelid;
    bool	tgdeferrable;
    bool	tginitdeferred;
    int16       tgnargs;
    int16       tgattr[FUNC_MAX_ARGS];
    char      **tgargs;
} Trigger;

tgname�Ǵ����������ƣ�tgnargs ����tgargs������������� tgargs��һ��ָ�����飬������ÿ��ָ��ָ���� CREATE TRIGGER����������IJ�����������Աֻ���ڲ�ʹ�á�

tg_trigtuplebuf

���û��������Զ�����û�д洢�ڴ��̻������ ���ǰ��� tg_trigtuple ���� InvalidBuffer �Ļ�������

tg_newtuplebuf

���û��������Զ���������δ�洢�ڴ��̻��������ô���ǰ��� tg_newtuple�� ���� InvalidBuffer �Ļ�������

һ���������������뷵��һ�� HeapTuple ָ�����һ�� NULL ָ�� ������ SQL NULL��Ҳ����˵����Ҫ���� isNull Ϊ�棩�� ��ע������㲻���޸����ڱ��������У���ôҪ����������� tg_trigtuple ���� tg_newtuple��