PL/Python 臺蘔昇庖躻媘膷噖婔婻埆 plpy 䔇 Python 昇庖㔗婘認婻昇庖麯䔇庘昄启婩麟埇傖婘 Python 傼乕麯傖 plpy.foo 䔇劉庖诙冖㔗䕞嬉plpy 垂䯄庖庘昄 plpy.debug(msg), plpy.log(msg), plpy.info(msg), plpy.notice(msg), plpy.warning(msg), plpy.error(msg), plpy.fatal(msg) 㔗plpy.error
启 plpy.fatal
垂鍙婪檕庺婔婻 Python 嚗婩套悩嘹澇橬托诙認婻嚗婩彍嚔嚹携彄脄䫘䔇昖臵凚躘嘷嬉庋媇潡蔙床庋媇锔庺㔗raise plpy.ERROR(msg) 启 raise plpy.FATAL(msg) 彖彆京昽庯脄䫘 plpy.error
启 plpy.fatal
㔗噽垄庘昄埻䫘潊婉劯嚻噽亓䔇媇敇㔗䬹垔嚻噽亓䔇媇敇滇挺檖䂍垵潙䆇㔕喍彄橉媇単斖媖㔕誻滇婴傽庋鄘啔滇䫌 log_min_messages 启 client_min_messages 陉䘞埗昄毓彽䔇㔗埗黙䆹17诙埡敘崔媇敇㔗
埥崡plpy 昇庖柊冕婴婻庘昄埆 execute
启 prepare
㔗拪婔婻昖臵庖严婾启婔婻埇锬䔇鍊彽埗昄脄䫘 plpy.execute
埇傖誊臯臖昖臵幽婫䂷悩誫啂彄婔婻䂷悩凹茇麯㔗䂷悩凹茇傪䩘婔婻彖臘潡蔙婔婻庖噩凹茇㔗䂷悩凹茇埇傖锔誺臯埙启庖枕劉準螪閞㔗垄橬婋麵認底鵺崡䔇桹濘nrows
誫啂臖昖臵誫啂䔇臯昄蔯 status
滇 SPI_execute()
䔇誫啂埻麟㔗䂷悩凹茇埇脘赆媞櫹㔗
懫套
rv = plpy.execute("SELECT * FROM my_table", 5)
誫啂準躻 my_table 婺橔崔 5 臯㔗套悩 my_table 橬婔婻 my_column 庖枕闼幽嘹埇傖䫘婋麵䔇桹濘螪閞垄
foo = rv[i]["my_column"]
丸庯婻庘昄plpy.prepare
婺婔婻昖臵庖崺昖臵蓇彐㔗垄滇婥婔婻昖臵庖严婾启婔婻埗昄䌂傋彖臘(套悩嘹婘臖昖臵麯橬嚘䫘埻麟)脄䫘䔇懫套
plan = plpy.prepare("SELECT last_name FROM my_users WHERE first_name = $1", [ "text" ])
text 滇嘹儖嘩婺 $1 嚹锐䔇埻麟䔇䌂傋㔗婘庖崺婔婻臺埖幋劯嘹䫘庘昄 plpy.execute
誊臯垄㔗
rv = plpy.execute(plan, [ "name" ], 5)
丸婬婻埗昄滇鍊彽滇埇锬䔇㔗
婘嘹䫘 PL/Python 昇庖庖崺婔婻螇彐䔇施唍臖螇彐滇躻媘媺庻䔇㔗臙黙臂 SPI 桺懼(䆹41)诙埡認埖臺䔇劆幬㔗婺庖脘虘庘昄脄䫘橬昽婄嘪䫘認婻䬹䗹驔襕嘪䫘愩幙庻嗘庖噩 SD 潡 GD 幋婔(埗黙誗40.1)㔗懫套
CREATE FUNCTION usesavedplan() RETURNS trigger AS $$ if SD.has_key("plan"): plan = SD["plan"] else: plan = plpy.prepare("SELECT 1") SD["plan"] = plan # 庘昄䂷悩 $$ LANGUAGE plpythonu;