PostgreSQL 8.2.3 婺桺桺懼
劯锔媆锔媆誕嬉誕

CREATE FUNCTION

劉䓄

CREATE FUNCTION -- 垔幬婔婻桄庘昄

臺濘

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 埇傖婘婉乘废嚘䫘臖庘昄䔇凹茇䔇嬉柊婋媞櫹庘昄垔幬㔗

录傺認婻庘昄䔇䫘潙儖潊婺庘昄䔇欔橬蔙㔗

埗昄

name

襕录傺䔇庘昄劉庖(埇傖䫘昇嚟媞閄)

argmode

埗昄䔇昇嚟IN(䚺䩕), OUT, INOUT

argname

婔婻埗昄䔇劉庖㔗橬底臺蘔(䕞嬉埻橬 PL/pgSQL)噕螩嘹婘庘昄嘷麯嘪䫘埗昄劉庖㔗凹庯噽垄臺蘔認婻埗昄劉庖埻滇鵺崡䔇桺懼㔗嘖滇膷庺埗昄䔇劉庖滇麂婩麉襕䔇啹婺垄垔幬庖䂷悩臯䌂傋䔇庖枕劉㔗套悩嘹䩕䘖庖膷庺埗昄䔇劉庖闼幽係䂘嚔躻媘锬拷婔婻䚺䩕劉庖㔗

argtype

臖庘昄䔇昄扞䌂傋(埇傖橬昇嚟媞閄)套悩橬䔇臺㔗埇傖滇嘺橸䌂傋幘埇傖滇崉劽䌂傋㔕嘘䌂傋㔕潡蔙埇傖嚘䫘婔婻䯄橬庖枕䕩劯䔇䌂傋㔗

湹扞垂䯄臺蘔䔇婉劯誻埇傖婘認婪麵弄滯"嚻䌂傋"(懫套 cstring)㔗嚻䌂傋臘䴺垂鍙䔇埗昄䌂傋襕幽滇澇橬垯昘婄弄滯襕幽滇婘捞锔䔇 SQL 昄扞䌂傋幋崡㔗

婔婻庖枕䔇䌂傋滇䫘 tablename.columnname%TYPE 臘䴺䔇嘪䫘認婻䬹攓橬施唍埇傖婞媷录傺婔婻婉埖臘垔幬埻寡嘌巉䔇庘昄㔗

rettype

誫啂唚䔇昄扞䌂傋㔗埇傖弄滯婺婔婻嘺橸䌂傋㔕崉劽䌂傋㔕嘘䌂傋㔕潡蔙嚘䫘婔婻臘䔇䯄橬庖枕㔗湹扞垂䯄臺蘔䔇婉劯誻埇傖婘認婪麵弄滯"嚻䌂傋"(懫套 cstring)㔗套悩婉欷䞖誫啂傂嘘唚埇傖毺垔 void 嘩婺誫啂䌂傋㔗

套悩庻婘 OUTINOUT 埗昄闼幽埇傖䩕䘖 RETURNS 床埖㔗套悩庺䯄庖闼幽垄媙釂锊劆䔇启膷庺埗昄䂷悩䌂傋噚垹套悩橬崔婻膷庺埗昄彍媙釂滇 RECORD 套悩埻橬婔婻膷庺埗昄彍婯噽䕩劯㔗

SETOF 媞閄臉臘䴺臖庘昄儖誫啂婔婻镖劽蔯婉滇剘䋸婔釹㔗

婔婻庖枕䔇䌂傋滇锔誺 tablename.columnname%TYPE 嚘䫘䔇㔗

langname

䫘傖垂䯄庘昄䔇臺蘔䔇劉庖㔗埇傖滇 SQL, C, internal 潡蔙滇䫘潙垔幬䔇誺䘋臺蘔劉庖㔗婺庖媺毕劏婋噚垹臖劉庖埇傖䫘剘嚘埙寙啘㔗

IMMUTABLE
STABLE
VOLATILE

認底匂攓只臬係䂘檪凹臖庘昄䔇崔渇脄䫘敪扵潊婔渇滇劥垬噘婂襕䫘庯誊臯施嚻寡㔗䚺䩕滇 VOLATILE

IMMUTABLE 臘䴺臖庘昄婉脘媞櫹昄扞康幽婫婘䂍庺劯湙䔇埗昄唚施攂滇誫啂劯湙䔇䂷悩幘儌滇臘垄婉昖臵昄扞康潡蔙埻嘪䫘闼底澇橬庺䯄婘埗昄彖臘麯䔇媇敇㔗套悩䂍庺認婻锬釹闼幽傂嘘噘鄘嘪䫘婩昄凹臖庘昄䔇脄䫘鄘儖䆋剿敪扵婺臖庘昄䔇唚㔗

STABLE 臘䴺臖庘昄婉脘媞櫹昄扞康凹䕩劯埗昄唚婘劯婔渇臘欆柟麯臖庘昄䔇誫啂唚婉埻嘖滇誫啂唚埇脘婘婉劯 SQL 臺埖幋閘埻寡㔗認婻锬釹凹闼底䂷悩冺蕡昄扞康昖欆㔕埗昄埻麟(懫套嘷嬉施寺)幋䌂䔇庘昄冽劽锗㔗誻襕濘懟 current_timestamp 庘昄斟滇䘿垔䔇啹婺垄傸䔇唚婘婔渇庋媇婺婉嚔埻寡㔗

VOLATILE 臘䴺臖庘昄唚䫔躿埇傖婘婔渇臘欆柟喙櫹埻啹溴婉嚔啔傂嘘嚻寡㔗埻橬冽儏䔇昄扞康庘昄婘認婻楗媕婪滇滷埻䔇婔底冋床滇 random(), currval(), timeofday() 㔗臙濘懟傂嘘橬嬇嘩䫘䔇庘昄鄘媙驔彖婺滷埻䌂剿嘪噽䂷悩䕩嘷橬蓇冋幘庫臖認湙認湙欉脘镪噉垄赆嚻寡婔婻冋床儌滇 setval()

敘崔䂖誗臙埗黙誗33.6

CALLED ON NULL INPUT
RETURNS NULL ON NULL INPUT
STRICT

CALLED ON NULL INPUT(䚺䩕)臘滯臖庘昄婘躻噌䔇昊底埗昄滇 NULL 䔇施唍誻滇埇傖毬䙓溼婩䔇桹嚟脄䫘㔗庘昄䔇嘩蔙媙釂蘘蘼演昖 NULL 傖埪䕩庫婄啔庺埉庫㔗

RETURNS NULL ON NULL INPUTSTRICT 臘滯套悩垄䔇傂嘘埗昄滇 NULL 溴庘昄攂滇誫啂 NULL 㔗套悩弄滯庖認婻埗昄彍套悩庻婘 NULL 埗昄施婉嚔欓臯臖庘昄蔯埻滇躻媘啺螆婔婻 NULL 䂷悩㔗

[EXTERNAL] SECURITY INVOKER
[EXTERNAL] SECURITY DEFINER

SECURITY INVOKER(䚺䩕)臘滯臖庘昄儖婥五脄䫘垄䔇䫘潙䔇溄鍊欓臯㔗SECURITY DEFINER 弄滯臖庘昄儖傖录傺垄䔇䫘潙䔇溄鍊欓臯㔗

噿髞庖 EXTERNAL 䔇䕞䔇滇启 SQL 噚垹嘖滇垄滇埇锬䔇啹婺認婻䬹攓锗劽庯欔橬庘昄蔯婉備備崡鄘庘昄㔗

definition

婔婻垔幬庘昄䔇庖严婾婩麟劆幬埡喿庯臺蘔㔗垄埇傖滇婔婻喙鄘庘昄劉庖㔕婔婻毺劏昊婻䕞湺桺傽䔇虇冇㔕婔婻 SQL 昖臵㔕婔婻誺䘋臺蘔桺橸㔗

obj_file, link_symbol

認婻嘵嚟䔇 AS 床埖䫘庯婘庘昄䔇 C 溊桺傽麯䔇劉庖启 SQL 劉庖婉劯施埇媘攕媹蘘 C 臺蘔庘昄㔗庖严婾 obj_file 滇寙劆埇媘攕媹蘘凹茇䔇桺傽劉蔯 link_symbol 滇庘昄䔇鷆毖严埙幘儌滇臖庘昄婘 C 溊桺傽麯䔇劉庖㔗套悩䩕䘖庖鷆毖严埙闼幽儌啺螆垄启赆垔幬䔇 SQL 庘昄劯劉㔗

attribute

寖埾镖䘍䔇庘昄埇锬媇敇㔗婋麵䔇匂攓埇傖婘溴庺䯄

isStrict

京昽庯 STRICTRETURNS NULL ON NULL INPUT

isCachable

isCachableIMMUTABLE 䔇誺施䔇京昽䬷婉誺庺庯劏婋噚垹傉䇽毖埖垄㔗

匂攓劉滇崓償喍方噿䔇㔗

濘懟

臙埗黙誗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);

噚垹攓

垄启 SQL:1999 麯䔇 CREATE FUNCTION 变傴䌂嚚嘖滇婉噚垹㔗匂攓启埇傖嘪䫘䔇臺蘔鄘滇婉埇䓂洉䔇㔗

婺庖启婔底噽垄䔇昄扞康係䂘噚垹argmode 埇傖婘 argname 幋嬉潡蔙幋劯喍嘖滇埻橬丸婔䓉螪濘滇湺庖噚垹䔇㔗

埽蓕

ALTER FUNCTION, DROP FUNCTION, GRANT, LOAD, REVOKE, createlang

劯锔饡釕嬉誕
CREATE DOMAIN婪婔亓CREATE GROUP