PostgreSQL 8.2.3 婺桺桺懼
劯锔媆锔䆹34. 蓥埏単媆誕嬉誕

34.3. 䫘 C 喍蓥埏単

橸䆹柟誄蓥埏単庘昄䔇嘯北䂖誗㔗埻橬嘷嘹䫘 C 幥喍蓥埏単庘昄䔇施唍欉驔襕認底媇敇㔗套悩嘹䫘昊䓉醻亓臺蘔喍蓥埏単闼幽係䂘儌嚔婺嘹崇䊖認底䂖誗㔗婘崓崔昄愙喕婋嘹婘幥喍躻噌䔇 C 蓥埏単幋嬉庫臖蔄荏嘪䫘誺䘋臺蘔㔗懟䓉誺䘋臺蘔䔇桺懼麯麵鄘橬噿庯套嘘䫘臖臺蘔幥喍蓥埏単䔇蓼麪㔗

蓥埏単庘昄媙釂嘪䫘"䬽橸-1"䔇庘昄䞇䊖単毖埼㔗

嘷婔婻庘昄赆蓥埏単䞇䊖単脄䫘施垄婉嚔櫽彄傂嘘捞锔埗昄蔯滇櫽彄婔婻毺劏 TriggerData 䂷悇䔇"䯇嵄"毺鐽㔗C 庘昄埇傖锔誺欓臯垂鍙婪赆欷匘婺

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

䔇垟

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)䔇臯㔗套悩滇 INSERTDELETE 套悩嘹婉愿䫘埥婔溇臯襖䕡溴臯(INSERT)潡媘䘖淉嘩(INSERT)闼幽認儌滇嘹儖誫啂䂍欓臯蔙䔇婩薪㔗

tg_newtuple

套悩滇 UPDATE 認滇婔婻毺劏桄䬽橸䔇臯䔇毺鐽套悩滇 INSERTDELETE 彍滇 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       tgnattr;
    int16      *tgattr;
    char      **tgargs;
} Trigger;

tgname 滇蓥埏単䔇劉䓄tgnargs 滇婘 tgargs 麯埗昄䔇昄麟tgargs 滇婔婻毺鐽昄䂇昄䂇麯懟婻毺鐽毺劏婘 CREATE TRIGGER 臺埖麯弄滯䔇埗昄㔗噽垄潊叻埻婘喙鄘嘪䫘㔗

tg_trigtuplebuf

套悩澇橬認湙䔇噄䂇潡蔙澇橬庻嗘婘伕䕻䚷喾寺麯彍滇寙劆 tg_trigtuple 潡蔙 InvalidBuffer 䔇䚷喾寺㔗

tg_newtuplebuf

套悩澇橬認湙䔇噄䂇潡蔙垄幽橻庻嗘婘伕䕻䚷喾寺麯闼幽儌滇寙劆 tg_newtuple 潡蔙 InvalidBuffer 䔇䚷喾寺㔗

婔婻蓥埏単庘昄媙釂誫啂婔婻 HeapTuple 毺鐽潡蔙婔婻 NULL 毺鐽(婉滇 SQL 䔇 NULL 唚幘儌滇臘婉襕螆䘞 isNull 婺䩘)㔗臙濘懟套悩嘹婉愿媞櫹溼婘赆淉嘩䔇臯闼幽襕湹扞愙喕誫啂 tg_trigtupletg_newtuple


劯锔饡釕嬉誕
昄扞櫹埻䔇埇蓖攓婪婔亓婔婻垯昘䔇冋床