This example of SPI usage demonstrates the visibility rule. There are more complex examples in in src/test/regress/regress.c and in contrib/spi.

このSPIを使った例は可視性規則を説明しています。 src/test/regress/regress.c と contrib/spi に、もっと複雑な例があ ります。

This is a very simple example of SPI usage. The procedure execq accepts an SQL-query in its first argument and tcount in its second, executes the query using SPI_exec and returns the number of tuples for which the query executed:

これは非常に簡単な SPI を使った例です。execq プロシージャは第一引数 として SQL 問い合わせを、第二引数として tcount をとり、SPI_exec を 使って問い合わせを実行し、問い合わせの対象となったタプルの数を返しま す。


#include "executor/spi.h"	/* this is what you need to work with SPI */

int execq(text *sql, int cnt);

int
execq(text *sql, int cnt)
{
	int ret;
	int proc = 0;
	
	SPI_connect();
	
	ret = SPI_exec(textout(sql), cnt);
	
	proc = SPI_processed;
	/*
	 * If this is SELECT and some tuple(s) fetched -
	 * returns tuples to the caller via elog (NOTICE).
	 */
	if ( ret == SPI_OK_SELECT && SPI_processed > 0 )
	{
		TupleDesc tupdesc = SPI_tuptable->tupdesc;
		SPITupleTable *tuptable = SPI_tuptable;
		char buf[8192];
		int i;
		
		for (ret = 0; ret < proc; ret++)
		{
			HeapTuple tuple = tuptable->vals[ret];
			
			for (i = 1, buf[0] = 0; i <= tupdesc->natts; i++)
				sprintf(buf + strlen (buf), " %s%s",
					SPI_getvalue(tuple, tupdesc, i),
					(i == tupdesc->natts) ? " " : " |");
			elog (NOTICE, "EXECQ: %s", buf);
		}
	}

	SPI_finish();

	return (proc);
}
#include "executor/spi.h"	/* SPIを使うためにはこれが必要です */

int execq(text *sql, int cnt);

int
execq(text *sql, int cnt)
{
	int ret;
	int proc = 0;
	
	SPI_connect();
	
	ret = SPI_exec(textout(sql), cnt);
	
	proc = SPI_processed;
	/*
         * SELECT問い合わせで、複数のタプルが取得された場合
         * elog(NOTICE)を使って、タプルを呼び出し元に返します。
	 */
	if ( ret == SPI_OK_SELECT && SPI_processed > 0 )
	{
		TupleDesc tupdesc = SPI_tuptable->tupdesc;
		SPITupleTable *tuptable = SPI_tuptable;
		char buf[8192];
		int i;
		
		for (ret = 0; ret < proc; ret++)
		{
			HeapTuple tuple = tuptable->vals[ret];
			
			for (i = 1, buf[0] = 0; i <= tupdesc->natts; i++)
				sprintf(buf + strlen (buf), " %s%s",
					SPI_getvalue(tuple, tupdesc, i),
					(i == tupdesc->natts) ? " " : " |");
			elog (NOTICE, "EXECQ: %s", buf);
		}
	}

	SPI_finish();

	return (proc);
}

Now, compile and create the function:

ここでコンパイルを行ない、以下のように関数を作成します。

create function execq (text, int4) returns int4 as '...path_to_so' language 'c';
vac=> select execq('create table a (x int4)', 0);
execq
-----
    0
(1 row)

vac=> insert into a values (execq('insert into a values (0)',0));
INSERT 167631 1
vac=> select execq('select * from a',0);

NOTICE:EXECQ:  0 <<< inserted by execq
NOTICE:EXECQ:  0 <<< execq によって挿入されたもの


NOTICE:EXECQ:  1 <<< value returned by execq and inserted by upper INSERT
NOTICE:EXECQ:  1 <<< execq によって返されたものと上の INSERT によって挿入
                     されたもの

execq
-----
    2
(1 row)

vac=> select execq('insert into a select x + 2 from a',1);
execq
-----
    1
(1 row)

vac=> select execq('select * from a', 10);
NOTICE:EXECQ:  0 

NOTICE:EXECQ:  1 


NOTICE:EXECQ:  2 <<< 0 + 2, only one tuple inserted - as specified
NOTICE:EXECQ:  2 <<< 指定された 0 + 2という値の1つのタプルのみが挿入
                     されます。
 
  ※2つ上の式でいう引数sql内のx+2と、引数tcountの値1 という指定のため
  最初のx(=0)により0+2という値になります。


execq
-----
 

    3            <<< 10 is max value only, 3 is real # of tuples
    3            <<< 10は最大値を示すだけです。実際のタプル数は3となります。
(1 row)

vac=> delete from a;
DELETE 3
vac=> insert into a values (execq('select * from a', 0) + 1);
INSERT 167712 1
vac=> select * from a;
x
-

1                <<< no tuples in a (0) + 1
1                <<< a テーブルにタプルが存在しない状態(0) + 1 を行なった結果
 
  ※ deleteの結果2つ上のexecpの結果は0となり、0+1をa に挿入することになります
(1 row)

vac=> insert into a values (execq('select * from a', 0) + 1);
NOTICE:EXECQ:  0 
INSERT 167713 1
vac=> select * from a;
x
-
1
 

2                <<< there was single tuple in a + 1
2                <<< a テーブルに既存の 1 つのタプル + 1
(2 rows)


--   This demonstrates data changes visibility rule:
--   これはデータ変更に関する可視性規則を説明します。

vac=> insert into a select execq('select * from a', 0) * x from a;
NOTICE:EXECQ:  1 
NOTICE:EXECQ:  2 
NOTICE:EXECQ:  1 
NOTICE:EXECQ:  2 
NOTICE:EXECQ:  2 
INSERT 0 2
vac=> select * from a;
x
-
1
2

2                <<< 2 tuples * 1 (x in first tuple)
6                <<< 3 tuples (2 + 1 just inserted) * 2 (x in second tuple)
(4 rows)             ^^^^^^^^ 
                     tuples visible to execq() in different invocations
2                <<< タプル数 2 * (1 番目のタプルの x の値である) 1
6                <<< タプル数 3 ( 2 + 挿入されたばかりの 1) * ( 2 番目のタプルの値である)2 
(4 rows)             ^^^^^^^^ 
                     execq()の異なる呼び出しで見ることができるタプル
 ※
  2 つ上の SQL では、2 (呼び出した時のタプル数)回 execq() が呼び出さ
  れるが、2 回目の呼び出しの時には、1 回目の呼び出しの結果挿入された
  タプルも可視となるので、その結果は 3 となります。