如何在 postgres c 用户定义函数中使用 float8
how to use float8 in postgres c user define function
我刚接触postgres服务器编程,想实现double+1;
这是来自 https://www.postgresql.org/docs/9.6/static/xfunc-c.html.
的代码副本
#include "postgres.h"
#include <string.h>
#include "utils/geo_decls.h"
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
float8 *
add_one_float8(float8 *arg)
{
float8 *result = (float8 *) palloc(sizeof(float8));
*result = *arg + 1.0;//if replace *arg with 1.5, it return normal(2.5)
return result;
}
我成功创建了function.Everytimes我调用了函数,该部分被postgres杀死了。
然后我尝试用 float8 替换 float8 *arg 然后用语句调用函数
select add_one(1.5);
它return 1;
似乎 arg 是 0.And 然后我用下面的代码替换它
float8
add_one_float8(float8 arg)
{
if(arg>0){
return arg+2;
}else if(arg==0)
return arg+3;
else
return arg+4;
}
然后 return 2;
我不知道如何在 postgres 中处理 float8 或 double,有人知道如何修复它或我错过了什么必要的步骤吗?提前致谢。
对了,postgresql-9.5和9.6我都试了,结果是same.Platform是linux,centos7
如the documentation所述,您需要使用PG_GETARG_FLOAT8(0)
获取参数,PG_RETURN_FLOAT8
获取return结果。
我刚接触postgres服务器编程,想实现double+1; 这是来自 https://www.postgresql.org/docs/9.6/static/xfunc-c.html.
的代码副本#include "postgres.h"
#include <string.h>
#include "utils/geo_decls.h"
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
float8 *
add_one_float8(float8 *arg)
{
float8 *result = (float8 *) palloc(sizeof(float8));
*result = *arg + 1.0;//if replace *arg with 1.5, it return normal(2.5)
return result;
}
我成功创建了function.Everytimes我调用了函数,该部分被postgres杀死了。
然后我尝试用 float8 替换 float8 *arg 然后用语句调用函数
select add_one(1.5);
它return 1; 似乎 arg 是 0.And 然后我用下面的代码替换它
float8
add_one_float8(float8 arg)
{
if(arg>0){
return arg+2;
}else if(arg==0)
return arg+3;
else
return arg+4;
}
然后 return 2; 我不知道如何在 postgres 中处理 float8 或 double,有人知道如何修复它或我错过了什么必要的步骤吗?提前致谢。
对了,postgresql-9.5和9.6我都试了,结果是same.Platform是linux,centos7
如the documentation所述,您需要使用PG_GETARG_FLOAT8(0)
获取参数,PG_RETURN_FLOAT8
获取return结果。