postgres 自定义函数

simpleest C function -return (a+b)
add_func.c 's content:
[wln@localhost postgres9.3]$ cat add_func.c
#include "postgres.h"
#include "fmgr.h"

PG_MODULE_MAGIC;

PG_FUNCTION_INFO_V1(add_ab);

Datum
add_ab(PG_FUNCTION_ARGS)
{
    int32 arg_a=PG_GETARG_INT32(0);
    int32 arg_b=PG_GETARG_INT32(1);

    PG_RETURN_INT32(arg_a+arg_b);
}

解释如下:
(1) #include  "postgres.h" : 该头文件包含写C代码的主要的定义和声明
(2) #include "fmgr.h" : 该头文件包含PG_* 宏定义
(3) PG_MODULE_MAGIC: This is a "magic block" defined in fmgr.h. This block
is used by the server to ensure that it does not load code compiled by a
different version of PostgreSQL, potentially crashing the server. It was
introduced in Version 8.2 of PostgreSQL. If you really need to write code
which can also be compiled for PostgreSQL versions before 8.2 you need to
put this between #ifdef PG_MODULE_MAGIC / #endif. You see this a lot in
samples available on the Internet, but you probably will not need to do it for
any new code. The latest pre-8.2 version became officially obsolete (that is
unsupported) in November 2010, and even 8.2 community support ended in
December 2011
(4). PG_FUNCTION_INFO_V1(add_ab) : This introduces the function to
PostgreSQL as Version 1 calling convention function. Without this line, it
will be treated as an old-style Version 0 function. (See the information box
following the Version 0 reference.)
(5). Datum: This is the return type of a C-language PostgreSQL function.
(6). add_ab(PG_FUNCTION_ARGS): The function name is add_ab and the rest are
its arguments. The PG_FUNCTION_ARGS definition can represent any number
of arguments and has to be present, even for a function taking no arguments
(7). int32 arg_a = PG_GETARG_INT32(0);: You need to use the PG_GETARG_
INT32() macro (or corresponding PG_GETARG_xxx() for
other argument types) to get the argument value.
(8). PG_RETURN_INT32(arg_a + arg_b);: Finally, you use the
PG_RETURN_() macro to build and return
a suitable return value.

Makefile 's content:
[wln@localhost postgres9.3]$ cat Makefile 
MODULES = add_func

PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS)

编译及测试
[wln@localhost postgres9.3]$ make
gcc -O2 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -g -fpic -I. -I. -I/home/wln/postgres9.3/install/include/server -I/home/wln/postgres9.3/install/include/internal -D_GNU_SOURCE   -c -o add_func.o add_func.c
add_func.c:9: 警告:‘add_ab’先前没有原型
gcc -O2 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -g -fpic -L/home/wln/postgres9.3/install/lib -Wl,--as-needed -Wl,-rpath,'/home/wln/postgres9.3/install/lib',--enable-new-dtags  -shared -o add_func.so add_func.o
[wln@localhost postgres9.3]$ ll
总计 21928
-rw-rw-r--  1 wln wln      231 07-12 09:20 add_func.c
-rw-rw-r--  1 wln wln     6844 07-12 09:40 add_func.o
-rwxrwxr-x  1 wln wln     8660 07-12 09:40 add_func.so
drwx------ 15 wln wln     4096 07-12 09:02 data
drwxrwxr-x  6 wln wln     4096 07-08 12:31 install
-rw-rw-r--  1 wln wln       95 07-12 08:58 Makefile
drwxrwxr-x  6 wln wln     4096 07-08 12:22 postgresql-9.3beta2
-rw-rw-r--  1 wln wln 22380613 2013-08-03 postgresql-9.3beta2.tar.gz
[wln@localhost postgres9.3]$ cp add_func.so install/lib/
[wln@localhost postgres9.3]$ psql -d postgres
postgres=# CREATE FUNCTION add(int,int)
RETURNS int
AS '/home/wln/postgres9.3/install/lib/add_func', 'add_ab'
LANGUAGE C STRICT;
CREATE FUNCTION
postgres=# select add(1,2);
 add 
-----
   3
(1 row)

postgres=# 


参考:
(1)《postgresql server programing》eight charpter "writing advanced functions in C "

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PostgreSQL是以加州大学伯克利分校计算机系开发的POSTGRES,现在已经更名为PostgreSQL. PostgreSQL支持大部分SQL标准并且提供了许多其它现代特性:复杂查询、外键、触发器、视图、事务完整性等。PostgreSQL 是一个免费的对象-关系数据库服务器(数据库管理系统),它在灵活的 BSD-风格许可证下发行。它提供了相对其他开放源代码数据库系统(比如 MySQL 和 Firebird),和专有系统(比如 Oracle、Sybase、IBM 的 DB2 和 Microsoft SQL Server)之外的另一种选择。事实上, PostgreSQL 的特性覆盖了 SQL-2/SQL-92 和 SQL-3/SQL-99,首先,它包括了可以说是目前世界上最丰富的数据类型的支持,其中有些数据类型可以说连商业数据库都不具备, 比如 IP 类型和几何类型等;其次,PostgreSQL 是全功能的自由软件数据库,很长时间以来,PostgreSQL 是唯一支持事务、子查询、多版本并行控制系统(MVCC)、数据完整性检查等特性的唯一的一种自由软件的数据库管理系统。 Inprise 的 InterBase 以及SAP等厂商将其原先专有软件开放为自由软件之后才打破了这个唯一。最后,PostgreSQL拥有一支非常活跃的开发队伍,而且在许多黑客的努力下,PostgreSQL 的质量日益提高。从技术角度来讲,PostgreSQL 采用的是比较经典的C/S(client/server)结构,也就是一个客户端对应一个服务器端守护进程的模式,这个守护进程分析客户端来的查询请求,生成规划树,进行数据检索并最终把结果格式化输出后返回给客户端。为了便于客户端的程序的编,由数据库服务器提供了统一的客户端 C 接口。而不同的客户端接口都是源自这个 C 接口,比如ODBC,JDBC,Python,Perl,Tcl,C/C++,ESQL等, 同时也要指出的是,PostgreSQL 对接口的支持也是非常丰富的,几乎支持所有类型的数据库客户端接口。这一点也可以说是 PostgreSQL 一大优点。本课程作为PostgreSQL数据库管理一,主要讲解以下内容: 1.     PostgreSQL 存储过程基本知识2.     PostgreSQL 用户自定义函数3.     PostgreSQL 控制结构4.     PostgreSQL 游标和存储过程5.     PostgreSQL 索引6.     PostgreSQL 视图7.     PostgreSQL 触发器8.     PostgreSQL 角色、备份和还原9.     PostgreSQL 表空间管理

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值