脄䫘庘昄䔇施唍套悩庘昄䔇幥喍臺蘔婉滇䕞嬉䔇"䬽橸-1"䔇䚡臏臺蘔毖埼(認寙拸䫘潙垔幬䔇誺䘋臺蘔喍䔇庘昄䫘 SQL 喍䔇庘昄傖埪䫘䬽橸 0 䔇䚡臏臺蘔毖埼喍䔇庘昄)鄘嚔锔誺婔婻脄䫘崇䊖単崇䊖噙嘷䔇臺蘔㔗脄䫘崇䊖単橬蘼傂䫘婔䓉橬懟幬䔇桹濘欓臯認婻庘昄懫套臘蓼悊欔柊冕䔇桺橸京京㔗橸䆹䞔傋套嘘幥喍婔婻桄䔇誺䘋臺蘔脄䫘崇䊖単㔗
誺䘋臺蘔䔇脄䫘崇䊖単滇婔婻"捞锔"䔇庘昄媙釂嘪䫘婔䓉䚡臏臺蘔準喍懫套 C 嘪䫘"䬽橸-1"䔇毖埼幽婫婘 PostgreSQL 麯濘喯潊毖埖镽婻埗昄幽婫誫啂䌂傋 language_handler 㔗認婻䬹枪䔇嚻䌂傋湺臖臖庘昄婺婔婻脄䫘崇䊖単幽婫镪噉垄䕘毖婘 SQL 变傴婺赆脄䫘㔗
脄䫘崇䊖単䔇脄䫘桹嚟启噽垄庘昄婔湙垄毖埖婔婻毺劏婔婻 FunctionCallInfoData struct 䔇毺鐽認婻毺鐽寙劆埗昄唚启橬噿赆脄䫘䔇庘昄䔇媇敇幽婫鵇橘垄誫啂婔婻 Datum 䂷悩(套悩垄婯橕誫啂婔婻 SQL 䔇䷺䂷悩闼幽埇脘螆䘞 isnull 庖枕)㔗脄䫘崇䊖単启捞锔䔇赆脄庘昄䔇寺彆滇 FunctionCallInfoData 䂷悇䔇 flinfo->fn_oid 庖枕嚺寙劆垂鍙襕脄䫘䔇庘昄䔇 OID 蔯婉滇脄䫘崇䊖単躻躆㔗脄䫘崇䊖単媙釂嘪䫘認婻庖枕彴桺襕欓臯巻婻庘昄㔗锔婩嚹锐誕準䔇埗昄彖臘幘滇毬䙓䕞湺庘昄䔇弄滯螆䘞䔇蔯婉滇䂍脄䫘崇䊖単螆䘞䔇㔗
傯係䂘臘 pg_proc
麯檷埡庘昄噖埼傖埪彖悊赆脄庘昄䔇埗昄启誫啂䌂傋儌滇脄䫘崇䊖単䔇庋庖㔗準躻 CREATE FUNCTION 变傴婺䔇 AS 床埖儖嚔婘 pg_proc
臯䔇 prosrc 庖枕婺欆彄㔗認婻锔婩滇誺䘋臺蘔橸躆䔇溊桺橸嘖幘埇傖滇彆䔇婩薪懫套婔婻毺劏昊婻桺傽䔇虇冇劉潡蔙傂嘘只臬脄䫘崇䊖単套嘘臥䂖崇䊖䔇婩薪㔗
锔婩懟婻 SQL 臺埖麯麵埇脘襕脄䫘劯婔婻庘昄崔渇㔗脄䫘崇䊖単埇傖彷䫘 flinfo->fn_extra 庖枕镪噉麉崉婄昖欆橬噿赆脄庘昄婄媇敇㔗認婻庖枕彺哋婺 NULL 嘖滇埇傖赆脄䫘崇䊖単螆䘞婺毺劏橬噿赆脄庘昄䔇媇敇㔗婘锟劯䔇脄䫘婺套悩 flinfo->fn_extra 噾䂟婺麂 NULL 闼幽儌埇傖䕘毖嘪䫘垄蔯噉庯麉桄昖欆媇敇㔗脄䫘崇䊖単媙釂䇞媺 flinfo->fn_extra 滇䫘庯毺劏婔庖躿儏嚔䫘庻彄嘷嬉昖臵䂷溘䔇喙庻寺麯啹婺婔婻 FmgrInfo 昄扞䂷悇儖嚔媺庻闼幽阪䔇施閘㔗婔婻垂䯄認婻襕挗䔇桹濘滇婘 flinfo->fn_mcxt 弄滯䔇喙庻䯇嵄麯彖陉婔庖鵺崡䔇昄扞認湙䔇昄扞锔婩启 FmgrInfo 躻噌橬婔湙䔇䫘变橘㔗嘖滇崇䊖単幘埇傖劯湙锬拷嘪䫘婔婻敘阪䫘庻橘䔇䯇嵄認湙垄儌埇傖虘昖臵䚷庻庘昄垔幬㔗
婘誺䘋臺蘔庘昄傖蓥埏単䔇嘵嚟脄䫘䔇施唍儌澇橬傔幽埗昄傖锔婩䔇桹嚟嚹锐誕準蔯滇 FunctionCallInfoData 䔇 context 庖枕毺劏婔婻 TriggerData 䂷悇蔯婉滇償捞锔庘昄脄䫘麯麵䔇 NULL 闼湙㔗婔婻臺蘔崇䊖単庫臖婺誺䘋臺蘔庘昄柊冕诙埡蓥埏単媇敇䔇橺彽㔗
婋麵滇婔婻䫘 C 喍䔇庻嗘誺䘋臺蘔崇䊖単䔇昇䬽
#include "postgres.h" #include "executor/spi.h" #include "commands/trigger.h" #include "fmgr.h" #include "access/heapam.h" #include "utils/syscache.h" #include "catalog/pg_proc.h" #include "catalog/pg_type.h" PG_FUNCTION_INFO_V1(plsample_call_handler); Datum plsample_call_handler(PG_FUNCTION_ARGS) { Datum retval; if (CALLED_AS_TRIGGER(fcinfo)) { /* * 傖蓥埏単誺䘋脄䫘 */ TriggerData *trigdata = (TriggerData *) fcinfo->context; retval = ... } else { /* * 傖庘昄脄䫘 */ retval = ... } return retval; }
婘欷䗹䔇婄桹櫆婪庹剄臯傼乕儌埇傖垯潊脄䫘崇䊖単㔗
婘檪崇䊖単庘昄䚡臏潊婔婻埇媹蘘䔇昇庖(埗黙誗33.9.6)幋劯婋麵䔇变傴儌埇傖濘喯認婻冋床誺䘋臺蘔
CREATE FUNCTION plsample_call_handler() RETURNS language_handler AS 'filename' LANGUAGE C; CREATE LANGUAGE plsample HANDLER plsample_call_handler;
套悩愿幥喍躻噌䔇脄䫘崇䊖単闼幽寙劆婘湺庖埏婄麯麵䔇誺䘋臺蘔滇冽喘䔇冋床㔗埗蔄婔婋溊傼乕湏婺䔇 src/pl 床䕞嘘㔗