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

SPI_execute

庘昄劉

SPI_execute -- 欓臯婔溇变傴

寘傋

int SPI_execute(const char * command, bool read_only, long count)

柟誄

SPI_execute 欓臯弄滯䔇 SQL 变傴诙埡 count 臯㔗套悩 read_onlytrue 变傴媙釂滇埻臂䔇啹溴埇傖䘖冞鍉嘯婔底欓臯䔇嚔體㔗

認婻庘昄埻脘婘噾誂毖䔇誺䘋婺脄䫘㔗

套悩 count 滇镽彍婘变傴锗劽䔇欔橬臯婪欓臯㔗套悩 count 崓庯 0 闼幽变傴欓臯䔇臯昄儖赆鍊彽(冽償婔婻 LIMIT 床埖)㔗懫套

SPI_execute("INSERT INTO foo SELECT * FROM bar", false, 5);

儖埻噕螩橔崔 5 臯某噖臘婺㔗

嘹埇傖婘婔婻庖严婾麯嚹锐崔婻变傴㔗SPI_execute 誫啂橔劯欓臯䔇变傴䔇䂷悩㔗count 䔇鍊彽䋸䆋婄庫䫘庯懟婔婻变傴嘖滇婉嚔庫䫘庯蓇彍䫘潊䔇锊薟变傴㔗

套悩 read_onlyfalseSPI_execute 锐嵂变傴螇昄単幽婫婘欓臯庖严婾麯䔇懟婻变傴幋嬉螇䞖婔婻桄䔇媆䙓㔗套悩嘷嬉庋媇䔇锫䥂亓彆滇 SERIALIZABLE 認婻媆䙓垂鍙婪幽婉櫹埻嘖滇婘 READ COMMITTED 昇嚟麯認婻媆䙓敘桄噕螩懟婻变傴䩋彄噽垄嚔臺䔇庋媇柊庴䔇䂷悩㔗認湙垂鍙婪滇婺庖媞櫹昄扞康䔇变傴橬婔躘䔇臯婺㔗

套悩 read_onlytrueSPI_execute 幽婉敘桄媆䙓潡蔙变傴螇昄単幽婫垄毺噕螩䞔剘䔇 SELECT 变傴庺䯄婘变傴庖严婾麯㔗認婻变傴嘪䫘婺变啘䔇昖臵傺䆋蕙準䔇媆䙓欓臯㔗認婻欓臯昇嚟懫臂/喍昇嚟欓臯冖䘖冞庖底啹婺垄潽鍴庖懟婻变傴䔇婔底嚔體㔗幽婫垄誻噕螩彽嘩䩘溼䔇䘿垔庘昄啹婺锟劯䔇欓臯鄘儖嘪䫘劯婔婻媆䙓䂷悩麯婉嚔橬櫹埻㔗

锔婩婘劯婔婻嘪䫘 SPI 䔇庘昄麯晙溗埻臂启臂喍变傴滇婉滯捺䔇闼湙埇脘凚躘麂婩晙幌䔇臯婺啹婺埻臂䔇昖臵婉脘䩋彄傂嘘臂喍䔇昖臵啔䔇昄扞康敘桄㔗

(橔劯)婔溇变傴欓臯誫啂䔇䂷悩䔇垂鍙臯昄嚔櫆婘噘匔䔇埻麟 SPI_processed 麯㔗套悩庘昄䔇誫啂唚滇 SPI_OK_SELECT, SPI_OK_INSERT_RETURNING, SPI_OK_DELETE_RETURNING, SPI_OK_UPDATE_RETURNING 闼幽嘹埇傖嘪䫘噘匔毺鐽 SPITupleTable *SPI_tuptable 螪閞䂷悩臯㔗婔底垂䫘变傴(懫套 EXPLAIN)誻誫啂臯镖劽幽婫 SPI_tuptable 幘儖婘認䓉愙喕婋寙劆䂷悩㔗

䂷悇 SPITupleTable 滇認湙垔幬䔇

typedef struct
{
    MemoryContext tuptabcxt;    /* 䂷悩臘䔇喙庻䯇嵄 */
    uint32      alloced;        /* 彖陉䔇 vals 昄䕞 */
    uint32      free;           /* ䷺閾䔇 vals 昄䕞 */
    TupleDesc   tupdesc;        /* 臯柟誄严 */
    HeapTuple  *vals;           /* 臯 */
} SPITupleTable;

vals 滇婔婻毺劏昄扞臯䔇䔇毺鐽昄䂇(橬昽螄嘘䔇昄䕞䫌 SPI_processed 䂍庺)㔗tupdesc 滇婔婻臯柟誄严嘹埇傖嚹锐䂍 SPI 庘昄崇䊖認底昄扞臯㔗tuptabcxt, alloced, free 滇 SPI 䔇喙鄘庖枕幽麂䂍 SPI 脄䫘蔙嘪䫘䔇㔗

SPI_finish 麪櫆欔橬婘嘷嬉誺䘋婺彖陉䔇 SPITupleTable 㔗套悩嘹噾䂟崇䊖垯䬹垔䔇䂷悩臘闼幽埇傖敘斷婄麪櫆垄桹濘滇脄䫘 SPI_freetuptable

埗昄

const char * command

寙劆襕欓臯䔇变傴䔇庖严婾

bool read_only

true 䫘庯埻臂䔇欓臯

long count

崇䊖潡誫啂䔇橔崓臯昄

誫啂唚

套悩变傴欓臯潊媘闼幽誫啂婋彖唚幋婔(麂蘘昄)

SPI_OK_SELECT

套悩欓臯庖婔婻 SELECT 嘖婉滇 SELECT INTO

SPI_OK_SELINTO

套悩欓臯庖婔溇 SELECT INTO

SPI_OK_INSERT

套悩欓臯庖婔溇 INSERT

SPI_OK_DELETE

套悩欓臯庖婔溇 DELETE

SPI_OK_UPDATE

套悩欓臯庖婔溇 UPDATE

SPI_OK_INSERT_RETURNING

套悩欓臯庖婔溇 INSERT RETURNING

SPI_OK_DELETE_RETURNING

套悩欓臯庖婔溇 DELETE RETURNING

SPI_OK_UPDATE_RETURNING

套悩欓臯庖婔溇 UPDATE RETURNING

SPI_OK_UTILITY

套悩欓臯庖婔溇垂䫘变傴(懫套 CREATE TABLE)

埏䫘髍臇施誫啂婋彖蘘昄唚幋婔

SPI_ERROR_ARGUMENT

套悩 commandNULLcount 償庯 0

SPI_ERROR_COPY

套悩嚕商誕臯 COPY TO stdoutCOPY FROM stdin

SPI_ERROR_CURSOR

套悩嚕商誕臯 DECLARE, CLOSE, FETCH

SPI_ERROR_TRANSACTION

套悩嚕商脄䫘傂嘘庋媇淉井变傴(BEGIN, COMMIT, ROLLBACK, SAVEPOINT, PREPARE TRANSACTION, COMMIT PREPARED, ROLLBACK PREPARED 潡垄傸䔇埻䓉)

SPI_ERROR_OPUNKNOWN

变傴䌂傋橻䘖(婉庫臖埏䫘)

SPI_ERROR_UNCONNECTED

套悩傯婔婻橻誂毖䔇誺䘋婺脄䫘

濘懟

SPI_execute, SPI_exec, SPI_execute_plan, SPI_execp 庘昄媞櫹 SPI_processedSPI_tuptable (埻滇婔婻毺鐽婉滇䂷悇䔇喙垹)㔗套悩嘹驔襕虘轪劯麵䔇脄䫘螪閞 SPI_execute 潡蔙䕩噿庘昄䔇䂷悩臘闼幽驔襕檪認婴婻噘匔媷媺庻彄婔婻匔鄘誺䘋埻麟婺㔗


劯锔饡釕嬉誕
SPI_pop婪婔亓SPI_exec