懟婻庘昄鄘橬婔婻滷崌攓亓彆(VOLATILE, STABLE, IMMUTABLE)㔗套悩 CREATE FUNCTION 变傴澇橬滯䇞弄滯评䘘䔇臺VOLATILE 儌滇䚺䩕㔗滷崌攓评䘘滇䂍嚻寡単䔇婔婻噿庯庘昄臯婺䔇欪臺
VOLATILE 埇傖啔傂嘘庋愙寙拸媞櫹昄扞康㔗垄埇傖婘嘪䫘劯湙埗昄脄䫘施誫啂婉劯䔇䂷悩㔗嚻寡単凹認湙䔇庘昄婉啔傂嘘啺螆㔗婔婻嘪䫘滷崌庘昄䔇昖臵婘驔襕昄扞唚䔇施唍懟渇鄘麉桄螇䞖庘昄䔇唚㔗
STABLE 庘昄婉嚔媞櫹昄扞康幽婫媺臕婘劯婔婻昖臵䔇䯇嵄麯䂍庺䕩劯埗昄䔇愙喕婋嚔䂍庺䕩劯䔇䂷悩㔗認婻评䘘噕螩嚻寡単婘婔婻昖臵麯檪崔婻庘昄脄䫘嚻寡潊婔婻㔗䬹彆滇婘䘵嚘欆柟䔇溇傽臘膆嚟麯麵寙劆認湙䔇庘昄滇垬噘䔇㔗啹婺䘵嚘欆柟埻螇䞖婔渇懫膄唚蔯婉滇懟臯婔渇㔗婘䘵嚘欆柟溇傽麯嘪䫘婔婻 VOLATILE 庘昄滇麂濘䔇㔗
IMMUTABLE 庘昄婉嚔媞櫹昄扞康幽婫媺臕婘傂嘘愙喕婋劯湙䔇埗昄愩誩誫啂劯湙䔇䂷悩㔗認婻评䘘噕螩嚻寡単婘昖臵脄䫘庘昄䔇施唍鵇噽檪庘昄螇䞖潊婔婻婩麟埗昄㔗懫套䌂嚚 SELECT ... WHERE x = 2 + 2 䔇昖臵埇傖䞔寡潊 SELECT ... WHERE x = 4 啹婺婘媹濘淉嘩严婋北䔇庘昄滇湺螄婺 IMMUTABLE 䔇㔗
婺庖橔嘿䔇嚻寡䂷悩庫臖儘埇脘嘪䫘橔婖湚䔇滷崌攓评䘘湺螄嘹䔇庘昄㔗
傂嘘橬嬇嘩䫘䔇庘昄鄘媙釂湺螄婺 VOLATILE 認湙凹垄䔇脄䫘儌婉嚔赆嚻寡㔗剿嘪婔婻庘昄澇橬嬇嘩䫘嘖垄䔇昄唚埇脘婘婔婻昖臵麯櫹埻闼幽幘媙釂湺螄婺 VOLATILE 冋套 random(), currval(), timeofday() 庘昄㔗
婘闼底䞔剘䔇蓇彐劯鷸婪欓臯䔇庴庐昖臵婪STABLE 启 IMMUTABLE 澇橬傔幽寺彆庘昄滇婘蓇彐嚔哋施欓臯誻滇婘昖臵嚔哋施欓臯䔇噞彆幽婉崓㔗嘖滇套悩蓇彐赆媺庻幽婫劯準赆麉䫘闼噞彆埇儌崓庖㔗套悩檪婔婻庘昄湺螄婺 IMMUTABLE 蔯垄垂鍙婪埽婉滇闼幽儌嚔凚躘婘锟劯嘪䫘噽蓇彐䔇施唍䫘婪婔婻婉垯昘䔇昄唚㔗套悩婘嘪䫘鵇噽庖崺喘臺埖潡蔙嘪䫘婔䓉䚷喾蓇彐䔇庘昄臺蘔(懫套 PL/pgSQL)闼幽劯悩埇脘冽婖麉㔗
啹婺 MVCC 䔇媆䙓臯婺(埗黙䆹12)婔婻埻寙劆 SELECT 变傴䔇庘昄埇傖垬噘婄湺螄婺 STABLE 剿嘪垄欔锬拷䔇臘埇脘嚔赆噽垄幽埏昖臵媞櫹幘婔湙㔗PostgreSQL 儖嚔婘欓臯 STABLE 庘昄施婺脄䫘垄䔇昖臵傺䆋媆䙓啹溴垄婘臖昖臵䔇䫘庻橘喙鄘嚔䩋彄婔躘䔇昄扞康蓖商㔗誻襕毺庺䔇滇current_timestamp
庘昄斟鄘滇䘿垔䔇啹婺垄傸䔇昄唚婘婔婻庋媇麯滇婉櫹埻䔇㔗
劯湙䔇媆䙓臯婺幘䫘庯 IMMUTABLE 庘昄麯麵䔇 SELECT 变傴㔗锔婩婘婔婻 IMMUTABLE 庘昄麯锬拷婔婻昄扞康䔇臘滇婉滯捺䔇啹婺套悩臘䔇喙垹櫹埻闼幽認䓉婉埻攓儌儖櫹埻㔗婉誺PostgreSQL 幽婉䥕溵嘹認湙啔㔗
婔婻婩蓕䔇髍臇滇檪婔婻庘昄湺螄婺 IMMUTABLE 蔯垂鍙婪認婻庘昄䔇䂷悩冺蕡昊婻陉䘞埗昄㔗懫套婔婻淉嘩施閘潿䔇庘昄埇脘橬冺蕡庯 timezone 螆䘞䔇䂷悩㔗婺庖垬噘蔄荏認湙䔇庘昄庫臖湺螄婺 STABLE 㔗
㔊濘懟㔏婘 PostgreSQL 8.0 幋嬉襕挗 STABLE 启 IMMUTABLE 庘昄婉脘媞櫹昄扞康認婻亥溘幽橻䫌係䂘嚺彽㔗䬽橸 8.0 锔誺襕挗認䌂庘昄婉脘寙劆 SELECT 幋崡䔇 SQL 变傴準嚺彽認婻亥溘㔗婉誺認幽啔幽婉滇垯噘黾嚹䔇剺亓啹婺認湙䔇庘昄傉䇽埇傖脄䫘闼底埇脘媞櫹昄扞康䔇 VOLATILE 庘昄㔗套悩嘹認幽啔䔇臺儖嚔埏䯄 STABLE 潡 IMMUTABLE 幽婉嚔蓬凘彄赆垄脄䫘䔇庘昄凹昄扞康欔啔䔇媞櫹㔗