SPI_exec

Name

SPI_exec --  Creates an execution plan (parser+planner+optimizer) and executes a query. 実行計画(パーサ+プランナ+オプティマイザ)を生成し、問い合わせを 実行します。

Synopsis

SPI_exec(query, tcount)

入力

char *query

String containing query plan

問い合わせ計画を含む文字列。

int tcount

Maximum number of tuples to return

返されるタプルの最大数。

出力

int

SPI_OK_EXEC if properly disconnected
SPI_OK_EXEC 正しく実行できた場合。 ※文脈よりdisconnectedはexecutedと推測
SPI_ERROR_UNCONNECTED if called from an un-connected procedure
SPI_ERROR_UNCONNECTED 未接続なプロ シージャから呼び出された場合。
SPI_ERROR_ARGUMENT if query is NULL or tcount < 0.
SPI_ERROR_ARGUMENT 引数 query が NULL または tcount < 0 の場合。
SPI_ERROR_UNCONNECTED if procedure is unconnected.
SPI_ERROR_UNCONNECTED 未接続なプロ シージャから呼び出された場合。 ※同じものが上にもあります
SPI_ERROR_COPY if COPY TO/FROM stdin.
SPI_ERROR_COPY COPY TO/FROM stdin の場合。
SPI_ERROR_CURSOR if DECLARE/CLOSE CURSOR, FETCH.
SPI_ERROR_CURSOR DECLARE/CLOSE CURSOR, FETCH の場合。
SPI_ERROR_TRANSACTION if BEGIN/ABORT/END.
SPI_ERROR_TRANSACTION BEGIN/ABORT/ENDの場合
SPI_ERROR_OPUNKNOWN if type of query is unknown (this shouldn't occur).
SPI_ERROR_OPUNKNOWN 未知の問い合わせ タイプの場合。(これはあってはなりません。)

If execution of your query was successful then one of the following (non-negative) values will be returned:

問い合わせの実行が成功した場合、以下の(非負の)値のいずれかが返ります。

SPI_OK_UTILITY if some utility (e.g. CREATE TABLE ...) was executed
SPI_OK_UTILITY ユーティリティ(例えば CREATE TABLE ... )が実行された場合。
SPI_OK_SELECT if SELECT (but not SELECT ... INTO!) was executed
SPI_OK_SELECT ( SELECT ... INTO 以外!の)SELECTが実行された場合。
SPI_OK_SELINTO if SELECT ... INTO was executed
SPI_OK_SELINTO SELECT ... INTO が実行された場合。
SPI_OK_INSERT if INSERT (or INSERT ... SELECT) was executed
SPI_OK_INSERT INSERT(または、INSERT ... SELECT)が実行された場合。
SPI_OK_DELETE if DELETE was executed
SPI_OK_DELETE DELETE が実行された場合。
SPI_OK_UPDATE if UPDATE was executed
SPI_OK_UPDATE UPDATE が実行された場合。

説明

SPI_exec creates an execution plan (parser+planner+optimizer) and executes the query for tcount tuples.

SPI_exec は実行計画(パーサ+プランナ+オブティマイ ザ)を生成し、tcount タプ ル分の問い合わせを実行します。

使い方

This should only be called from a connected procedure. If tcount is zero then it executes the query for all tuples returned by the query scan. Using tcount > 0 you may restrict the number of tuples for which the query will be executed. For example,

接続済みのプロシージャからのみこの関数を呼び出して下さい。 tcount が 0 ならば、問い 合わせスキャンが返した全てのタプルについて問い合わせを行ないます。 tcount > 0とすることで、 問い合わせの対象となるタプルの数を制限することができます。例えば、

SPI_exec ("insert into table select * from table", 5);
will allow at most 5 tuples to be inserted into table. If execution of your query was successful then a non-negative value will be returned.
SPI_exec ("insert into table select * from table", 5);
では、最大 5 個のタプルをテーブルに挿入することができます。問い合 わせの実行が成功した場合、非負の値が返ります。

Note: You may pass many queries in one string or query string may be re-written by RULEs. SPI_exec returns the result for the last query executed.

1 個の文字列の中に複数の問い合わせを入れて渡したり、RULE システムによっ て複数の問い合わせに書き換えられる可能性のある文字列を渡しても構いませ ん。SPI_exec は最後に実行された問い合わせの結果を 返します。

The actual number of tuples for which the (last) query was executed is returned in the global variable SPI_processed (if not SPI_OK_UTILITY). If SPI_OK_SELECT returned and SPI_processed > 0 then you may use global pointer SPITupleTable *SPI_tuptable to access the selected tuples: Also NOTE, that SPI_finish frees and makes all SPITupleTables unusable! (See Memory management).

実際に(最後の)問い合わせの処理対象となったタプルの数は( SPI_OK_UTILITY でない限り)大域変数 SPI_processed に設定されます。もし SPI_OK_SELECT が返り、SPI_processed > 0 ならば、大域変数である SPITupleTable *SPI_tuptableというポインタを使っ て、選択されたタプルにアクセスできます。また、 SPI_finish はすべての SPITupleTable を開放しますの で、SPITupleTable は使用できなくなることに注意して下さい!(メモリ管理の 項を参照して下さい。)

SPI_exec may return one of the following (negative) values:

SPI_execは次の(負の)値を返す可能性があります。

SPI_ERROR_ARGUMENT if query is NULL or tcount < 0.
SPI_ERROR_ARGUMENT 引数 query が NULL または tcount < 0 の場合。
SPI_ERROR_UNCONNECTED if procedure is unconnected.
SPI_ERROR_UNCONNECTED 未接続なプロシージャの場合。
SPI_ERROR_COPY if COPY TO/FROM stdin.
SPI_ERROR_COPY COPY TO/FROM stdin の場合。
SPI_ERROR_CURSOR if DECLARE/CLOSE CURSOR, FETCH.
SPI_ERROR_CURSOR DECLARE/CLOSE CURSOR、FETCH の場合。
SPI_ERROR_TRANSACTION if BEGIN/ABORT/END.
SPI_ERROR_TRANSACTION BEGIN/ABORT/END の場合。
SPI_ERROR_OPUNKNOWN if type of query is unknown (this shouldn't occur).
SPI_ERROR_OPUNKNOWN 未知の問い合わせタイプの場合。(これはあってはいけません。)

アルゴリズム

SPI_exec は以下を行ないます。

Disconnects your procedure from the SPI manager and frees all memory allocations made by your procedure via palloc since the SPI_connect. These allocations can't be used any more! See Memory management.

ユーザ定義のプロシージャを SPI マネージャから切断し、 SPI_connect を実行してからそのプロシージャが palloc を使用して獲得したメモリを全て開放しま す。このメモリはもはや使用できません!メモリ管理の項を参照して下さ い。