This section contains a very simple example of SPI usage. The
C function <function>execq</function> takes an SQL command as its
first argument and a row count as its second, executes the command
using <function>SPI_exec</function> and returns the number of rows
that were processed by the command. You can find more complex
examples for SPI in the source tree in
<filename>src/test/regress/regress.c</filename> and in the
<xref linkend="contrib-spi"/> module.
本節には、SPIを使用する非常に簡単な例があります。
C関数execqは1つ目の引数としてSQLコマンドを、2つ目の引数として行数を取り、SPI_execコマンドを実行し、そのコマンドで処理された行数を返します。
SPIのより複雑な例はソースツリー内のsrc/test/regress/regress.cとspiモジュールにあります。
#include "postgres.h"
#include "executor/spi.h"
#include "utils/builtins.h"
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(execq);
Datum
execq(PG_FUNCTION_ARGS)
{
char *command;
int cnt;
int ret;
uint64 proc;
/* Convert given text object to a C string */
/* 与えられたテキストオブジェクトをC文字列に変換 */
command = text_to_cstring(PG_GETARG_TEXT_PP(0));
cnt = PG_GETARG_INT32(1);
SPI_connect();
ret = SPI_exec(command, cnt);
proc = SPI_processed;
/*
* If some rows were fetched, print them via elog(INFO).
* 何らかの行が取り出された場合は、行をelog(INFO)を使用して表示
*/
if (ret > 0 && SPI_tuptable != NULL)
{
SPITupleTable *tuptable = SPI_tuptable;
TupleDesc tupdesc = tuptable->tupdesc;
char buf[8192];
uint64 j;
for (j = 0; j < tuptable->numvals; j++)
{
HeapTuple tuple = tuptable->vals[j];
int i;
for (i = 1, buf[0] = 0; i <= tupdesc->natts; i++)
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %s%s",
SPI_getvalue(tuple, tupdesc, i),
(i == tupdesc->natts) ? " " : " |");
elog(INFO, "EXECQ: %s", buf);
}
}
SPI_finish();
pfree(command);
PG_RETURN_INT64(proc);
}
This is how you declare the function after having compiled it into a shared library (details are in <xref linkend="dfunc"/>.): 以下は、コンパイルし共有ライブラリ(36.10.5を参照)を作成した後で、関数を宣言する方法です。
CREATE FUNCTION execq(text, integer) RETURNS int8
AS 'filename'
LANGUAGE C STRICT;
Here is a sample session: 以下はセッションの例です。
=> SELECT execq('CREATE TABLE a (x integer)', 0);
execq
-------
0
(1 row)
=> INSERT INTO a VALUES (execq('INSERT INTO a VALUES (0)', 0));
INSERT 0 1
=> SELECT execq('SELECT * FROM a', 0);
INFO: EXECQ: 0 <lineannotation>-- inserted by execq</lineannotation>
INFO: EXECQ: 1 <lineannotation>-- returned by execq and inserted by upper INSERT</lineannotation>
INFO: EXECQ: 0 -- execqによって挿入された
INFO: EXECQ: 1 -- execqによって返され、上位のINSERTによって挿入された
execq
-------
2
(1 row)
=> SELECT execq('INSERT INTO a SELECT x + 2 FROM a RETURNING *', 1);
INFO: EXECQ: 2 <lineannotation>-- 0 + 2, then execution was stopped by count</lineannotation>
INFO: EXECQ: 2 -- 0 + 2、それから実行はカウントによって止められた
execq
-------
1
(1 row)
=> SELECT execq('SELECT * FROM a', 10);
INFO: EXECQ: 0
INFO: EXECQ: 1
INFO: EXECQ: 2
execq
-------
3 <lineannotation>-- 10 is the max value only, 3 is the real number of rows</lineannotation>
3 -- 10は最大値を示すのみで、3が実際の行数
(1 row)
=> SELECT execq('INSERT INTO a SELECT x + 10 FROM a', 1);
execq
-------
3 <lineannotation>-- all rows processed; count does not stop it, because nothing is returned</lineannotation>
3 -- すべての行が処理された。何も返されないので、カウントでは止まらない
(1 row)
=> SELECT * FROM a;
x
----
0
1
2
10
11
12
(6 rows)
=> DELETE FROM a;
DELETE 6
=> INSERT INTO a VALUES (execq('SELECT * FROM a', 0) + 1);
INSERT 0 1
=> SELECT * FROM a;
x
---
1 <lineannotation>-- 0 (no rows in a) + 1</lineannotation>
1 -- 0 (aには行がない) + 1
(1 row)
=> INSERT INTO a VALUES (execq('SELECT * FROM a', 0) + 1);
INFO: EXECQ: 1
INSERT 0 1
=> SELECT * FROM a;
x
---
1
2 <lineannotation>-- 1 (there was one row in a) + 1</lineannotation>
2 -- 1 (aには1行ある) + 1
(2 rows)
<lineannotation>-- This demonstrates the data changes visibility rule.</lineannotation>
<lineannotation>-- execq is called twice and sees different numbers of rows each time:</lineannotation>
-- これはデータ変更に関する可視性規則を説明する。
-- execqは2回呼ばれ、各回で異なる行数を見る。
=> INSERT INTO a SELECT execq('SELECT * FROM a', 0) * x FROM a;
INFO: EXECQ: 1 <lineannotation>-- results from first execq</lineannotation>
INFO: EXECQ: 1 -- 1回目のexecqの結果
INFO: EXECQ: 2
INFO: EXECQ: 1 <lineannotation>-- results from second execq</lineannotation>
INFO: EXECQ: 1 -- 2回目のexecqの結果
INFO: EXECQ: 2
INFO: EXECQ: 2
INSERT 0 2
=> SELECT * FROM a;
x
---
1
2
2 <lineannotation>-- 2 rows * 1 (x in first row)</lineannotation>
6 <lineannotation>-- 3 rows (2 + 1 just inserted) * 2 (x in second row)</lineannotation>
2 -- 2 行 * 1 (最初の行のx)
6 -- 3 行 (2 + ちょうど挿入された1) * 2 (2行目のx)
(4 rows)