PostgreSQL 8.2.3 婺桺桺懼
劯锔媆锔䆹40. PL/Python - Python 誺䘋臺蘔媆誕嬉誕

40.3. 昄扞康螪閞

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.errorplpy.fatal 垂鍙婪檕庺婔婻 Python 嚗婩套悩嘹澇橬托诙認婻嚗婩彍嚔嚹携彄脄䫘䔇昖臵凚躘嘷嬉庋媇潡蔙床庋媇锔庺㔗raise plpy.ERROR(msg)raise plpy.FATAL(msg) 彖彆京昽庯脄䫘 plpy.errorplpy.fatal 㔗噽垄庘昄埻䫘潊婉劯嚻噽亓䔇媇敇㔗䬹垔嚻噽亓䔇媇敇滇挺檖䂍垵潙䆇㔕喍彄橉媇単斖媖㔕誻滇婴傽庋鄘啔滇䫌 log_min_messagesclient_min_messages 陉䘞埗昄毓彽䔇㔗埗黙䆹17诙埡敘崔媇敇㔗

埥崡plpy 昇庖柊冕婴婻庘昄埆 executeprepare 㔗拪婔婻昖臵庖严婾启婔婻埇锬䔇鍊彽埗昄脄䫘 plpy.execute 埇傖誊臯臖昖臵幽婫䂷悩誫啂彄婔婻䂷悩凹茇麯㔗䂷悩凹茇傪䩘婔婻彖臘潡蔙婔婻庖噩凹茇㔗䂷悩凹茇埇傖锔誺臯埙启庖枕劉準螪閞㔗垄橬婋麵認底鵺崡䔇桹濘nrows 誫啂臖昖臵誫啂䔇臯昄蔯 statusSPI_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)诙埡認埖臺䔇劆幬㔗婺庖脘虘庘昄脄䫘橬昽婄嘪䫘認婻䬹䗹驔襕嘪䫘愩幙庻嗘庖噩 SDGD 幋婔(埗黙誗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;

劯锔饡釕嬉誕
蓥埏単庘昄婪婔亓橉媇単䚡䘋毖埼