CREATE [ OR REPLACE ] FUNCTION name ( [ [ argmode ] [ argname ] argtype [, ...] ] ) [ RETURNS rettype ] { LANGUAGE langname | IMMUTABLE | STABLE | VOLATILE | CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT | [ EXTERNAL ] SECURITY INVOKER | [ EXTERNAL ] SECURITY DEFINER | AS 'definition' | AS 'obj_file', 'link_symbol' } ... [ WITH ( attribute [, ...] ) ]
CREATE FUNCTION 垔幬婔婻桄䔇庘昄㔗CREATE OR REPLACE FUNCTION 儖襕幽录傺婔婻桄庘昄襕幽敪扵䯄橬䔇垔幬㔗
套悩寙劆庖婔婻昇嚟劉闼幽庘昄儌婘毺垔䔇昇嚟婺录傺㔗劥彍垄嚔婘嘷嬉昇嚟婺录傺㔗桄庘昄䔇劉庖婉脘启劯婔婻昇嚟婺䔇傂嘘婥橬劯湙埗昄䌂傋䔇庘昄劯劉㔗婉誺埗昄䌂傋婉劯䔇庘昄埇傖劯劉(認埆啔麉蘘)㔗
嘪䫘 CREATE OR REPLACE FUNCTION 敘桄婔婻䯄橬庘昄䔇垔幬㔗婉脘䫘認婻桹濘媞櫹婔婻庘昄䔇劉庖潡蔙埗昄䌂傋劥彍儌嚔录傺婔婻桄䔇庘昄㔗劯湙CREATE OR REPLACE FUNCTION 幘婉嚔噕螩嘹媞櫹婔婻䯄橬庘昄䔇誫啂䌂傋㔗襕啔認底庋愙嘹媙釂役鍴幽麉桄录傺庘昄㔗套悩嘪䫘 OUT 埗昄闼儌懟叿五鍴庖役鍴庘昄幋崡嘹婉脘媞櫹傂嘘 OUT 埗昄䔇䌂傋潡蔙劉庖㔗
套悩嘹役鍴䇽劯麉傺婔婻庘昄桄庘昄启斓庘昄儖滇婉劯䔇垂嘷嘹儌驔襕役鍴䯄橬嚘䫘庖蔕庘昄䔇蓇彍㔕蓖商㔕蓥埏単京京㔗嘪䫘 CREATE OR REPLACE FUNCTION 埇傖婘婉乘废嚘䫘臖庘昄䔇凹茇䔇嬉柊婋媞櫹庘昄垔幬㔗
录傺認婻庘昄䔇䫘潙儖潊婺庘昄䔇欔橬蔙㔗
襕录傺䔇庘昄劉庖(埇傖䫘昇嚟媞閄)
埗昄䔇昇嚟IN(䚺䩕), OUT, INOUT
婔婻埗昄䔇劉庖㔗橬底臺蘔(䕞嬉埻橬 PL/pgSQL)噕螩嘹婘庘昄嘷麯嘪䫘埗昄劉庖㔗凹庯噽垄臺蘔認婻埗昄劉庖埻滇鵺崡䔇桺懼㔗嘖滇膷庺埗昄䔇劉庖滇麂婩麉襕䔇啹婺垄垔幬庖䂷悩臯䌂傋䔇庖枕劉㔗套悩嘹䩕䘖庖膷庺埗昄䔇劉庖闼幽係䂘嚔躻媘锬拷婔婻䚺䩕劉庖㔗
臖庘昄䔇昄扞䌂傋(埇傖橬昇嚟媞閄)套悩橬䔇臺㔗埇傖滇嘺橸䌂傋幘埇傖滇崉劽䌂傋㔕嘘䌂傋㔕潡蔙埇傖嚘䫘婔婻䯄橬庖枕䕩劯䔇䌂傋㔗
湹扞垂䯄臺蘔䔇婉劯誻埇傖婘認婪麵弄滯"嚻䌂傋"(懫套 cstring)㔗嚻䌂傋臘䴺垂鍙䔇埗昄䌂傋襕幽滇澇橬垯昘婄弄滯襕幽滇婘捞锔䔇 SQL 昄扞䌂傋幋崡㔗
婔婻庖枕䔇䌂傋滇䫘 tablename.columnname%TYPE 臘䴺䔇嘪䫘認婻䬹攓橬施唍埇傖婞媷录傺婔婻婉埖臘垔幬埻寡嘌巉䔇庘昄㔗
誫啂唚䔇昄扞䌂傋㔗埇傖弄滯婺婔婻嘺橸䌂傋㔕崉劽䌂傋㔕嘘䌂傋㔕潡蔙嚘䫘婔婻臘䔇䯄橬庖枕㔗湹扞垂䯄臺蘔䔇婉劯誻埇傖婘認婪麵弄滯"嚻䌂傋"(懫套 cstring)㔗套悩婉欷䞖誫啂傂嘘唚埇傖毺垔 void 嘩婺誫啂䌂傋㔗
套悩庻婘 OUT 潡 INOUT 埗昄闼幽埇傖䩕䘖 RETURNS 床埖㔗套悩庺䯄庖闼幽垄媙釂锊劆䔇启膷庺埗昄䂷悩䌂傋噚垹套悩橬崔婻膷庺埗昄彍媙釂滇 RECORD 套悩埻橬婔婻膷庺埗昄彍婯噽䕩劯㔗
SETOF 媞閄臉臘䴺臖庘昄儖誫啂婔婻镖劽蔯婉滇剘䋸婔釹㔗
婔婻庖枕䔇䌂傋滇锔誺 tablename.columnname%TYPE 嚘䫘䔇㔗
䫘傖垂䯄庘昄䔇臺蘔䔇劉庖㔗埇傖滇 SQL, C, internal 潡蔙滇䫘潙垔幬䔇誺䘋臺蘔劉庖㔗婺庖媺毕劏婋噚垹臖劉庖埇傖䫘剘嚘埙寙啘㔗
認底匂攓只臬係䂘檪凹臖庘昄䔇崔渇脄䫘敪扵潊婔渇滇劥垬噘婂襕䫘庯誊臯施嚻寡㔗䚺䩕滇 VOLATILE 㔗
IMMUTABLE 臘䴺臖庘昄婉脘媞櫹昄扞康幽婫婘䂍庺劯湙䔇埗昄唚施攂滇誫啂劯湙䔇䂷悩幘儌滇臘垄婉昖臵昄扞康潡蔙埻嘪䫘闼底澇橬庺䯄婘埗昄彖臘麯䔇媇敇㔗套悩䂍庺認婻锬釹闼幽傂嘘噘鄘嘪䫘婩昄凹臖庘昄䔇脄䫘鄘儖䆋剿敪扵婺臖庘昄䔇唚㔗
STABLE 臘䴺臖庘昄婉脘媞櫹昄扞康凹䕩劯埗昄唚婘劯婔渇臘欆柟麯臖庘昄䔇誫啂唚婉埻嘖滇誫啂唚埇脘婘婉劯 SQL 臺埖幋閘埻寡㔗認婻锬釹凹闼底䂷悩冺蕡昄扞康昖欆㔕埗昄埻麟(懫套嘷嬉施寺)幋䌂䔇庘昄冽劽锗㔗誻襕濘懟 current_timestamp
庘昄斟滇䘿垔䔇啹婺垄傸䔇唚婘婔渇庋媇婺婉嚔埻寡㔗
VOLATILE 臘䴺臖庘昄唚䫔躿埇傖婘婔渇臘欆柟喙櫹埻啹溴婉嚔啔傂嘘嚻寡㔗埻橬冽儏䔇昄扞康庘昄婘認婻楗媕婪滇滷埻䔇婔底冋床滇 random(), currval(), timeofday() 㔗臙濘懟傂嘘橬嬇嘩䫘䔇庘昄鄘媙驔彖婺滷埻䌂剿嘪噽䂷悩䕩嘷橬蓇冋幘庫臖認湙認湙欉脘镪噉垄赆嚻寡婔婻冋床儌滇 setval() 㔗
敘崔䂖誗臙埗黙誗33.6㔗
CALLED ON NULL INPUT(䚺䩕)臘滯臖庘昄婘躻噌䔇昊底埗昄滇 NULL 䔇施唍誻滇埇傖毬䙓溼婩䔇桹嚟脄䫘㔗庘昄䔇嘩蔙媙釂蘘蘼演昖 NULL 傖埪䕩庫婄啔庺埉庫㔗
RETURNS NULL ON NULL INPUT 潡 STRICT 臘滯套悩垄䔇傂嘘埗昄滇 NULL 溴庘昄攂滇誫啂 NULL 㔗套悩弄滯庖認婻埗昄彍套悩庻婘 NULL 埗昄施婉嚔欓臯臖庘昄蔯埻滇躻媘啺螆婔婻 NULL 䂷悩㔗
SECURITY INVOKER(䚺䩕)臘滯臖庘昄儖婥五脄䫘垄䔇䫘潙䔇溄鍊欓臯㔗SECURITY DEFINER 弄滯臖庘昄儖傖录傺垄䔇䫘潙䔇溄鍊欓臯㔗
噿髞庖 EXTERNAL 䔇䕞䔇滇启 SQL 噚垹嘖滇垄滇埇锬䔇啹婺認婻䬹攓锗劽庯欔橬庘昄蔯婉備備崡鄘庘昄㔗
婔婻垔幬庘昄䔇庖严婾婩麟劆幬埡喿庯臺蘔㔗垄埇傖滇婔婻喙鄘庘昄劉庖㔕婔婻毺劏昊婻䕞湺桺傽䔇虇冇㔕婔婻 SQL 昖臵㔕婔婻誺䘋臺蘔桺橸㔗
認婻嘵嚟䔇 AS 床埖䫘庯婘庘昄䔇 C 溊桺傽麯䔇劉庖启 SQL 劉庖婉劯施埇媘攕媹蘘 C 臺蘔庘昄㔗庖严婾 obj_file 滇寙劆埇媘攕媹蘘凹茇䔇桺傽劉蔯 link_symbol 滇庘昄䔇鷆毖严埙幘儌滇臖庘昄婘 C 溊桺傽麯䔇劉庖㔗套悩䩕䘖庖鷆毖严埙闼幽儌啺螆垄启赆垔幬䔇 SQL 庘昄劯劉㔗
寖埾镖䘍䔇庘昄埇锬媇敇㔗婋麵䔇匂攓埇傖婘溴庺䯄
京昽庯 STRICT 潡 RETURNS NULL ON NULL INPUT
isCachable 滇 IMMUTABLE 䔇誺施䔇京昽䬷婉誺庺庯劏婋噚垹傉䇽毖埖垄㔗
匂攓劉滇崓償喍方噿䔇㔗
臙埗黙誗33.3诙埡敘崔噿庯幥喍庘昄䔇媇敇㔗
噕螩嘹儖垯昘䔇 SQL 䌂傋臺濘䫘庯膷噖埗昄启誫啂唚㔗婉誺橬底䌂傋弄滯䔇䂖誗(懫套 numeric 䌂傋䔇來庥嘘)滇䫌婋北庘昄垂䯄蘘蘼䔇幽婫嚔赆 CREATE FUNCTION 变傴旇旇婄劂毬(幘儌滇婉喉赆臖彆潡嚺彽)㔗
PostgreSQL 噕螩庘昄麉蘘幘儌滇埻襕膷噖埗昄婉劯庹婻婉劯䔇庘昄埇傖劯劉㔗婉誺欔橬庘昄䔇 C 劉庖媙釂婉劯幘儌滇臘嘹媙釂䂍庽麉蘘䔇 C 庘昄婉劯䔇 C 劉庖(懫套嘪䫘埗昄䌂傋嘩婺 C 劉庖䔇婔鄘彖)㔗
套悩婴婻庘昄劯劉幽婫膷噖埗昄䌂傋幘䕩劯闼幽儌螴婺認婴婻庘昄滇婔湙䔇媘䘖欔橬 OUT 埗昄㔗啹溴婋麵䔇弄滯滇喾仕䔇
CREATE FUNCTION foo(int) ... CREATE FUNCTION foo(int, out text) ...
套悩麉崉脄䫘 CREATE FUNCTION 幽婫鄘毺劏劯婔婻䕞湺桺傽闼幽臖桺傽埻媹蘘婔渇㔗襕麉桄媹蘘臖桺傽(埇脘滇婘嚔埏誺䘋婺)埇傖嘪䫘 LOAD 变傴㔗
嘪䫘 DROP FUNCTION 役鍴婔婻䫘潙垔幬庘昄㔗
锔婩嘪䫘䆯噄严䘯垔(埗黙 誗4.1.2.2)幥喍庘昄垔幬庖严婾蔯婉滇嘪䫘捞锔䔇剘嚘埙寙啘臺濘嚔敘桹冪㔗套悩婉嘪䫘䆯噄严䘯垔闼幽庘昄嘷麯麵䔇傂嘘剘嚘埙潡埉桩溹鄘媙釂䫘埯喍䔇桹濘锄锩㔗
襕脘垔幬庘昄䫘潙媙釂凹臖臺蘔橬 USAGE 溄鍊㔗
認麯滇婔底䞔剘䔇冋床䫘庯婞媷嘹嚔哋毯柇認婻变傴㔗敘崔媇敇启冋床埗黙誗33.3㔗
CREATE FUNCTION add(integer, integer) RETURNS integer AS 'select $1 + $2;' LANGUAGE SQL IMMUTABLE RETURNS NULL ON NULL INPUT;
彷䫘埗昄劉䫘 PL/pgSQL 躻嵂婔婻昘昄
CREATE OR REPLACE FUNCTION increment(i integer) RETURNS integer AS $$ BEGIN RETURN i + 1; END; $$ LANGUAGE plpgsql;
誫啂婔婻寙劆崔婻膷庺埗昄䔇螄嘘
CREATE FUNCTION dup(in int, out f1 int, out f2 text) AS $$ SELECT $1, CAST($1 AS text) || ' is text' $$ LANGUAGE SQL; SELECT * FROM dup(42);
嘹埇傖锔誺变劉滯䇞䔇崉劽䌂傋䔇螪濘喖阪婄幾劯湙䔇庋愙
CREATE TYPE dup_result AS (f1 int, f2 text); CREATE FUNCTION dup(int) RETURNS dup_result AS $$ SELECT $1, CAST($1 AS text) || ' is text' $$ LANGUAGE SQL; SELECT * FROM dup(42);