对PostgreSQL SPI例子的学习

[作者:技术者高健@博客园  mail: luckyjackgao@gmail.com ]

http://www.postgresql.org/docs/9.1/static/spi-examples.html

SPI 的例子里面没有说,是如何编译和部署的,我这里补充下:

编译与部署:

[root@localhost soft]# export LD_LIBRARY_PATH=/usr/local/pgsql/lib
[root@localhost soft]# echo $LD_LIBRARY_PATH
/usr/local/pgsql/lib


[root@localhost test]# cc -fpic -I/usr/local/pgsql/include/server/ -shared -o execq.so execq.c
[root@localhost test]# ls
execq.c  execq.so
[root@localhost test]# 



[root@localhost test]# cp execq.so /usr/local/pgsql/lib
[root@localhost test]# 

运行:

postgres=# create function execq(text,integer) returns integer as '/usr/local/pgsql/lib/execq.so','execq' language c strict;
CREATE FUNCTION
postgres=# 


postgres=# select execq('create table abc(id integer)',0);
 execq 
-------
0
(1 row)

postgres=# 


postgres=# insert into abc values( execq('insert into abc values(0)',0));
INSERT 0 1
postgres=# 



postgres=# insert into abc values( execq('insert into abc values(0)',0));
INSERT 0 1
postgres=# select * from abc;
 id 
----
0
1
(2 rows)

postgres=# 



postgres=# select execq('select * from abc',0);
INFO:  EXECQ:  0 
INFO:  EXECQ:  1 
 execq 
-------
2
(1 row)

postgres=# 



postgres=# select execq('insert into abc select id+2 from abc',1);
 execq 
-------
2
(1 row)

postgres=# select * from abc;
 id 
----
0
1
2
3
(4 rows)

postgres=# 



postgres=# select execq('select * from abc', 10);
INFO:  EXECQ:  0 
INFO:  EXECQ:  1 
INFO:  EXECQ:  2 
INFO:  EXECQ:  3 
 execq 
-------
4
(1 row)

postgres=# 

[作者:技术者高健@博客园  mail: luckyjackgao@gmail.com ]

 

原文地址:https://www.cnblogs.com/gaojian/p/2744195.html